ffmpeg复制保存时间戳 [英] ffmpeg copyts to preserve timestamp

查看:143
本文介绍了ffmpeg复制保存时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改HLS段传输流,并使用ffmpeg保留其开始时间.但是,即使提到了-copyts,输出也不会保留输入文件的start_time值.这是我的命令行:

I am trying to modify an HLS segment transport stream, and preserve its start time with ffmpeg. However the output does not preserve the input file's start_time value, even if -copyts is mentioned. Here's my command line:

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秒.

The start_time value is delayed about 2 seconds consistently.

/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?谢谢.

How do I ensure that output file has the same start_time? Thanks.

推荐答案

我决定在源代码中进行一些挖掘,以了解正在发生的事情.

I decided to dig a little in the source to understand what's happening.

  • 使用文件中每个单独流的显示时间戳(PTS)计算程序的整体开始时间.

  • The overall start_time of the program is calculated by the using the presentation time stamp(PTS) of each individual stream in the file.

ffmpeg将默认的Mux解码延迟值设置为0.7秒.这是在ffmpeg_opt.c

ffmpeg sets a default Mux decode delay value of 0.7 seconds. This is set in the ffmpeg_opt.c

如果未设置"copyts"标志(在libavformat/mpegtsenc.c中),则将此延迟值添加到每个数据包的PTS值中:

This delay value is added to the PTS value of each packet, if the "copyts" flag is not set(in libavformat/mpegtsenc.c):

if (ts->copyts < 1) {
    if (pts != AV_NOPTS_VALUE)
        pts += delay;
    if (dts != AV_NOPTS_VALUE)
        dts += delay;
}

在这种情况下,编码器将延迟值加倍:

In this case, the encoder doubles the delay value :

const uint64_t delay = 
    av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;

因此,每个数据包的PTS增加了1.4秒的时间.如果提供了"copyts",则将其添加到输入流的时间戳值中.

Therefore, an extra 1.4 seconds get added to each packet's PTS. If "copyts" is provided, this is added to the timestamp value from the input stream.

可以使用"muxdelay"标志来覆盖此延迟;在这种情况下,延迟设置为零.

It is possible to override this delay using the "muxdelay" flag; in that case the delay gets set to zero.

现在,为什么是0.7?为什么要对mpeg ts进行两次?我希望我知道答案,大概是规范中的某个地方.但就目前而言,这是必须要做的.

Now, why 0.7? why twice in the case of mpeg ts ?I wish I know the answers, presumably it's there in the spec somewhere. But for now, this would have to do.

这篇关于ffmpeg复制保存时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆