ffmpeg copyts to preserve timestamp

强颜欢笑 提交于 2019-12-20 21:48:20

问题


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

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

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


回答1:


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

  • 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 sets a default Mux decode delay value of 0.7 seconds. This is set in the ffmpeg_opt.c

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;

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.

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

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.



来源:https://stackoverflow.com/questions/29527882/ffmpeg-copyts-to-preserve-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!