如何使用 ffmpeg 调整 mpeg 2 ts 开始时间?

How to adjust mpeg 2 ts start time with ffmpeg?

我正在编写简单的 HLS(Http Live Streaming)java 服务器来直播(真正直播,而不是点播)屏幕显示 + 语音。我不断获取大量图像帧和音频样本作为我服务的输入,并生成 mpeg 2 ts 文件 + m3u8 播放列表网页作为输出。工作流程如下:

  1. 在特定时间段内收集(缓冲)源视频帧和音频
  2. 将一系列视频帧转换为 h.264 编码视频文件
  3. 将音频样本转换为 mp3 音频文件
  4. 使用 ffmpeg 命令将它们合并到 .ts 文件

    ffmpeg -i audio.mp3 -i video.mp4 -f mpegts -c:a copy -c:v copy -vprofile main -level:v 4.0 -vbsf h264_mp4toannexb -flags -global_header segment.ts
    
  5. 在 m3u8 播放列表上发布几个 .ts 文件。

问题是在播放第一段后导致播放列表中断。 VLC 记录以下错误:

freetype error: Breaking unbreakable line
ts error: libdvbpsi (PSI decoder): TS discontinuity (received 0, expected 4) for PID 17
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 0
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 4096
core error: ES_OUT_SET_(GROUP_)PCR is called too late (pts_delay increased to 1000 ms)
core error: ES_OUT_RESET_PCR called
core error: Could not convert timestamp 185529572000
ts error: libdvbpsi (PSI decoder): TS discontinuity (received 0, expected 4) for PID 17
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 0
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 4096
core error: ES_OUT_SET_(GROUP_)PCR is called too late (jitter of 8653 ms ignored)
core error: Could not get display date for timestamp 0
core error: Could not convert timestamp 185538017000
core error: Could not convert timestamp 185538267000
core error: Could not convert timestamp 185539295977
...

我想原因是段的开始时间不属于一个流,但是一旦添加新块就不可能连接和重新分段(使用ffmepg -f segment)整个流。尝试将 #EXT-X-DISCONTINUITY 标签作为 suggested here 添加到播放列表,但没有帮助。当我 ffprobe 他们时,我得到:

Input #0, mpegts, from '26.ts':
Duration: 00:00:10.02, start: 1.876978, bitrate: 105 kb/s
Program 1
Metadata:
  service_name    : Service01
  service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 640x640, 4 fps, 4 tbr, 90k tbn, 8 tbc
Stream #0:1[0x101]: Audio: mp3 ([3][0][0][0] / 0x0003), 48000 Hz, mono, s16p, 64 kb/s   

Duration: 00:00:10.02, start: 1.876978, bitrate: 105 kb/s 行中的起始值对于所有段而言大致相等。 当我从可用的经过验证的工作播放列表(如 http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/appleman.m3u8)中检查段时,它们每个段都有不同的起始值,例如:

Input #0, mpegts, from 'segm150518140104572-424570.ts':
Duration: 00:00:06.17, start: 65884.808689, bitrate: 479 kb/s
Program 257
Stream #0:0[0x20]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 320x180 [SAR 1:1 DAR 16:9], 30 fps, 29.97 tbr, 90k tbn, 60 tbc
Stream #0:1[0x21]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 115 kb/s
Stream #0:2[0x22]: Data: timed_id3 (ID3  / 0x20334449)

和下一个

Input #0, mpegts, from 'segm150518140104572-424571.ts':
Duration: 00:00:06.22, start: 65890.814689, bitrate: 468 kb/s
Program 257
Stream #0:0[0x20]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 320x180 [SAR 1:1 DAR 16:9], 30 fps, 29.97 tbr, 90k tbn, 60 tbc
Stream #0:1[0x21]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 124 kb/s
Stream #0:2[0x22]: Data: timed_id3 (ID3  / 0x20334449)

区别在于segm150518140104572-424571.ts的开始时间等于segm150518140104572-424570.ts的开始时间+持续时间。

如何用ffmpeg调整这个起始值?或者也许我的整个方法是错误的?不幸的是,我无法在互联网上找到使用 ffmepg 实现的实时(非点播)视频服务的工作示例。

它不仅仅是时间戳,还是连续性指标。所以设置开始时间不会解决你的问题。您必须编码为单个流。

我已经用 here 中描述的方法回答了我自己的问题。 可以使用 -f segment 格式的 -initial_offset 参数调整时间戳:

ffmpeg -i in.ts -vcodec copy -acodec copy -f segment -initial_offset 10 -segment_format mpegts out%d.ts

对于每个新段,我计算必要的 -initial_offset 作为所有先前段长度的总和。

可以设置“-segment_time20”只输出一个ts文件

fmpeg.exe -i 0.mp3 -c copy -bsf:v aac_adtstoasc -f segment -segment_time 20 -initial_offset 10 -segment_format mpegts out%d.ts