ffmpeg 复制以保留时间戳
Posted
技术标签:
【中文标题】ffmpeg 复制以保留时间戳【英文标题】:ffmpeg copyts to preserve timestamp 【发布时间】:2015-06-14 04:36:46 【问题描述】:我正在尝试修改 HLS 段传输流,并使用 ffmpeg 保留其开始时间。但是,即使提到了 -copyts,输出也不会保留输入文件的 start_time 值。这是我的命令行:
ffmpeg -i fileSequence1.ts -i x.png -filter_complex '[0:v][1:v]overlay[out]' -map '[out]' -map 0:1 -acodec copy -vsync 0 -vcodec libx264 -streamid 0:257 -streamid 1:258 -copyts -profile:v baseline -level 3 output.ts
start_time 值持续延迟约 2 秒。
/Users/macadmin/>ffmpeg -y -v verbose -i fileSequence0.ts -map 0:0 -vcodec libx264 -copyts -vsync 0 -async 0 output.ts
ffmpeg version 2.5.3 Copyright (c) 2000-2015 the FFmpeg developers
built on Mar 29 2015 21:31:57 with Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libfreetype --enable-libvorbis --enable-libvpx --enable-libass --enable-ffplay --enable-libfdk-aac --enable-libopus --enable-libquvi --enable-libx265 --enable-nonfree --enable-vda
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
[h264 @ 0x7fa93b800000] Current profile doesn't provide more RBSP data in PPS, skipping
Last message repeated 2 times
[mpegts @ 0x7fa93a80da00] max_analyze_duration 5000000 reached at 5000000 microseconds
Input #0, mpegts, from 'fileSequence0.ts':
Duration: 00:00:09.65, start: 9.952111, bitrate: 412 kb/s
Program 1
Stream #0:0[0x101]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 640x360 (640x368), 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x102]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 101 kb/s
[graph 0 input from stream 0:0 @ 0x7fa93a5229c0] w:640 h:360 pixfmt:yuv420p tb:1/90000 fr:25/1 sar:0/1 sws_param:flags=2
[libx264 @ 0x7fa93b800c00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fa93b800c00] profile High, level 3.0
[mpegts @ 0x7fa93b800600] muxrate VBR, pcr every 2 pkts, sdt every 200, pat/pmt every 40 pkts
Output #0, mpegts, to 'output.ts':
Metadata:
encoder : Lavf56.15.102
Stream #0:0: Video: h264 (libx264), yuv420p, 640x360, q=-1--1, 25 fps, 90k tbn, 25 tbc
Metadata:
encoder : Lavc56.13.100 libx264
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[NULL @ 0x7fa93b800000] Current profile doesn't provide more RBSP data in PPS, skipping
Last message repeated 1 times
frame= 87 fps=0.0 q=28.0 size= 91kB time=00:00:11.40 bitrate= 65.0kbits/[NULL @ 0x7fa93b800000] Current profile doesn't provide more RBSP data in PPS, skipping
frame= 152 fps=151 q=28.0 size= 204kB time=00:00:14.00 bitrate= 119.4kbits/[NULL @ 0x7fa93b800000] Current profile doesn't provide more RBSP data in PPS, skipping
frame= 224 fps=148 q=28.0 size= 306kB time=00:00:16.88 bitrate= 148.5kbits/No more output streams to write to, finishing.
frame= 240 fps=125 q=-1.0 Lsize= 392kB time=00:00:19.52 bitrate= 164.6kbits/s
video:334kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 17.347548%
Input file #0 (fileSequence0.ts):
Input stream #0:0 (video): 240 packets read (360450 bytes); 240 frames decoded;
Input stream #0:1 (audio): 0 packets read (0 bytes);
Total: 240 packets (360450 bytes) demuxed
Output file #0 (output.ts):
Output stream #0:0 (video): 240 frames encoded; 240 packets muxed (342204 bytes);
Total: 240 packets (342204 bytes) muxed
[libx264 @ 0x7fa93b800c00] frame I:3 Avg QP:15.08 size: 7856
[libx264 @ 0x7fa93b800c00] frame P:81 Avg QP:21.03 size: 2807
[libx264 @ 0x7fa93b800c00] frame B:156 Avg QP:23.40 size: 585
[libx264 @ 0x7fa93b800c00] consecutive B-frames: 11.7% 2.5% 7.5% 78.3%
[libx264 @ 0x7fa93b800c00] mb I I16..4: 57.4% 17.5% 25.1%
[libx264 @ 0x7fa93b800c00] mb P I16..4: 8.0% 8.2% 1.0% P16..4: 30.5% 11.3% 4.6% 0.0% 0.0% skip:36.4%
[libx264 @ 0x7fa93b800c00] mb B I16..4: 0.1% 0.1% 0.0% B16..8: 34.6% 2.7% 0.2% direct: 1.3% skip:60.9% L0:47.3% L1:49.1% BI: 3.6%
[libx264 @ 0x7fa93b800c00] 8x8 transform intra:42.2% inter:73.3%
[libx264 @ 0x7fa93b800c00] coded y,uvDC,uvAC intra: 26.2% 43.0% 6.8% inter: 5.4% 8.5% 0.1%
[libx264 @ 0x7fa93b800c00] i16 v,h,dc,p: 46% 26% 6% 21%
[libx264 @ 0x7fa93b800c00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 50% 14% 23% 1% 2% 6% 1% 3% 1%
[libx264 @ 0x7fa93b800c00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 40% 32% 10% 3% 3% 4% 2% 5% 2%
[libx264 @ 0x7fa93b800c00] i8c dc,h,v,p: 48% 23% 26% 3%
[libx264 @ 0x7fa93b800c00] Weighted P-Frames: Y:1.2% UV:0.0%
[libx264 @ 0x7fa93b800c00] ref P L0: 71.5% 10.7% 14.8% 2.9% 0.1%
[libx264 @ 0x7fa93b800c00] ref B L0: 95.5% 4.0% 0.5%
[libx264 @ 0x7fa93b800c00] ref B L1: 96.8% 3.2%
[libx264 @ 0x7fa93b800c00] kb/s:285.17
----------- FFProbe source video
/Users/macadmin>ffprobe fileSequence0.ts
ffprobe version 2.5.3 Copyright (c) 2007-2015 the FFmpeg developers
built on Mar 29 2015 21:31:57 with Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libfreetype --enable-libvorbis --enable-libvpx --enable-libass --enable-ffplay --enable-libfdk-aac --enable-libopus --enable-libquvi --enable-libx265 --enable-nonfree --enable-vda
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Input #0, mpegts, from 'fileSequence0.ts':
Duration: 00:00:09.65, start: 9.952111, bitrate: 412 kb/s
Program 1
Stream #0:0[0x101]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 640x360, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x102]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 101 kb/s
------ FFPROBE result video
/Users/macadmin>ffprobe output.ts
ffprobe version 2.5.3 Copyright (c) 2007-2015 the FFmpeg developers
built on Mar 29 2015 21:31:57 with Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libfreetype --enable-libvorbis --enable-libvpx --enable-libass --enable-ffplay --enable-libfdk-aac --enable-libopus --enable-libquvi --enable-libx265 --enable-nonfree --enable-vda
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Input #0, mpegts, from 'output.ts':
Duration: 00:00:09.60, start: 11.400000, bitrate: 334 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 640x360, 25 fps, 25 tbr, 90k tbn, 50 tbc
如何确保输出文件具有相同的 start_time?谢谢。
【问题讨论】:
我想补充一点,即使是简单的流复制也会导致 start_time 值延迟约 1.4 秒:示例:fmpeg -i input.ts -acodec copy -vcodec copy -copyts output.ts 我确实尝试过 -vsync 0 ,但没有运气。同样的延迟仍然存在。 pastebin.com/QA19NLvv 我能够通过使用“itsoffset”标志添加负偏移来解决此问题。 【参考方案1】:我决定深入挖掘源代码以了解发生了什么。
程序的整体 start_time 是通过使用文件中每个单独流的呈现时间戳 (PTS) 来计算的。
ffmpeg 将默认的 Mux 解码延迟值设置为 0.7 秒。这是在 ffmpeg_opt.c 中设置的
如果未设置“copyts”标志(在 libavformat/mpegtsenc.c 中),此延迟值将添加到每个数据包的 PTS 值:
if (ts->copyts < 1)
if (pts != AV_NOPTS_VALUE)
pts += delay;
if (dts != AV_NOPTS_VALUE)
dts += delay;
在这种情况下,编码器将延迟值加倍:
const uint64_t delay =
av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
因此,每个数据包的 PTS 额外增加了 1.4 秒。如果提供了“copyts”,则会将其添加到输入流中的时间戳值中。
可以使用“muxdelay”标志覆盖此延迟;在这种情况下,延迟设置为零。
现在,为什么是 0.7?为什么在 mpeg ts 的情况下两次?我希望我知道答案,大概它在规范中的某个地方。但就目前而言,这是必须的。
【讨论】:
可能会在 ffmpeg-user ML 上询问? 通过在所有-i
输入之后添加-muxdelay 0
,ffprobe -i input -show_streams -unit
报告的start_time
没有添加默认延迟。以上是关于ffmpeg 复制以保留时间戳的主要内容,如果未能解决你的问题,请参考以下文章