FFmpeg - 单独转码每个片段

FFmpeg - 单独转码每个片段

我正在尝试将视频分割成 GOP(非固定大小),然后分别对视频的每个部分(GOP)进行转码。我正在尝试使用 HLS(HTTP 流协议),因此这些视频块是 MPEG2-TS(.ts)。

看来分割视频工作正常,我创建了清单 (.m3u8) 文件,它在 VLC 上运行良好。但是,在对视频进行转码后,似乎在片段转换上产生了问题,出现了某种延迟(灰屏)。

这是我用来分割视频的命令

ffmpeg.exe -i videotest.mp4 -vcodec copy -reset_timestamps 1 -map 0 -hls_list_size 0  out.m3u8

此命令将视频分成 10 个部分(.ts 文件)并生成清单文件(.m3u8)

这是输出:

ffmpeg version N-81045-g450cf40 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.4.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-
version3 --disable-w32threads --enable-dxva2 --enable-libmfx --enable-
nvenc --enable-avisynth --enable-bzlib --enable-libebur128 --enable-
fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-
libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-
libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-
libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-
libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-
librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr -
-enable-libspeex --enable-libtheora --enable-libtwolame --enable-
libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx
--enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 
--enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --
enable-decklink --enable-zlib
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 50.100 / 57. 50.100
libavformat    57. 42.100 / 57. 42.100
libavdevice    57.  0.102 / 57.  0.102
libavfilter     6. 47.100 /  6. 47.100
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100
libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videotest.mp4':
 Metadata:
   major_brand     : mp42
   minor_version   : 512
   compatible_brands: isomiso2avc1mp41
   creation_time   : 2015-09-03 15:02:55
   title           : Fantastic Four: Rise of the Silver Surfer - Theatrical Trailer
   artist          : 20th Century Fox
   date            : 2007
   encoder         : HandBrake 0.10.2 2015061100
   genre           : Trailer
 Duration: 00:00:25.07, start: 0.000000, bitrate: 1884 kb/s
   Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x544 [SAR 1:1 DAR 40:17], 1742 kb/s, 23.90 fps, 23.98 tbr, 90k tbn, 180k tbc (default)
  Metadata:
    creation_time   : 2015-09-03 15:02:55
    handler_name    : VideoHandler
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 135 kb/s (default)
  Metadata:
    creation_time   : 2015-09-03 15:02:55
    handler_name    : Stereo
[hls @ 000000000127c340] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
  Last message repeated 1 times
Output #0, hls, to 'out.m3u8':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    genre           : Trailer
    title           : Fantastic Four: Rise of the Silver Surfer - Theatrical Trailer
    artist          : 20th Century Fox
    date            : 2007
    encoder         : Lavf57.42.100
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x544 [SAR 1:1 DAR 40:17], q=2-31, 1742 kb/s, 23.90 fps, 23.98 tbr, 90k tbn, 90k tbc (default)
    Metadata:
      creation_time   : 2015-09-03 15:02:55
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2015-09-03 15:02:55
      handler_name    : Stereo
      encoder         : Lavc57.50.100 aac
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored bitrate=N/A speed=29.3x    
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
frame=  599 fps=0.0 q=-1.0 Lsize=N/A time=00:00:25.02 bitrate=N/A speed=  29x    

video:5333kB audio:314kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[aac @ 000000000127f620] Qavg: 17473.217

到目前为止,如果我运行 m3u8 文件,它可以正常工作。

这是我用来对视频的每个片段进行编码的命令

ffmpeg.exe -i segment0.ts -vcodec libx264 -s 640:480 -map 0 outputEncoded0.ts

我使用脚本为每个段运行此命令:

#/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for file in /home/pi/Desktop/videoExamples/videosTranscode/*;
    do 
    echo "doing stuff"

    filename="${file##*/}"

    filename="${filename%.*}"


    ffmpeg.exe -i $file -vcodec libx264 -s 640:480 -map 0 $filename.ts

done

IFS=SAVEIFS

输出如下

ffmpeg version N-81045-g450cf40 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.4.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-
version3 --disable-w32threads --enable-dxva2 --enable-libmfx --enable-
nvenc --enable-avisynth --enable-bzlib --enable-libebur128 --enable-
fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-
libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-
libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-
libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-
libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-
librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr -
-enable-libspeex --enable-libtheora --enable-libtwolame --enable-
libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx 
--enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 
--enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --
enable-decklink --enable-zlib
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 50.100 / 57. 50.100
libavformat    57. 42.100 / 57. 42.100
libavdevice    57.  0.102 / 57.  0.102
libavfilter     6. 47.100 /  6. 47.100
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100
libpostproc    54.  0.100 / 54.  0.100
Input #0, mpegts, from 'out0.ts':
  Duration: 00:00:05.69, start: 1.503789, bitrate: 215 kb/s
  Program 1 
    Metadata:
      service_name    : Fantastic Four: Rise of the Silver Surfer - Theatrical Trailer
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B),     yuv420p(tv, bt709), 1280x544 [SAR 1:1 DAR 40:17], 23.98 tbr, 90k tbn, 180k tbc
    Stream #0:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 11 kb/s
fixme:msvcrt:MSVCRT__wsopen_s : pmode 0x01b6 ignored
[libx264 @ 0000000001244ae0] using SAR=30/17
[libx264 @ 0000000001244ae0] using cpu capabilities: MMX2 SSE2Fast     SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0000000001244ae0] profile High, level 3.0
[mpegts @ 00000000012433e0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
    Last message repeated 1 times
Output #0, mpegts, to 'outputEncoded0.ts':
  Metadata:
    encoder         : Lavf57.42.100
    Stream #0:0: Video: h264 (libx264), yuv420p, 640x480 [SAR 30:17     DAR 40:17], q=-1--1, 23.98 fps, 90k tbn, 23.98 tbc
    Metadata:
      encoder         : Lavc57.50.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp2, 48000 Hz, stereo, s16, 384 kb/s
    Metadata:
      encoder         : Lavc57.50.100 mp2
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> mp2 (native))
Press [q] to stop, [?] for help
frame=  116 fps=0.0 q=28.0 size=     174kB time=00:00:04.90 bitrate=     291.0kbits/frame=  135 fps=0.0 q=-1.0 Lsize=     344kB time=00:00:05.60 bitrate= 503.0kbits/s speed=7.82x    
video:28kB audio:263kB subtitle:0kB other streams:0kB global     headers:0kB muxing overhead: 18.113007%
[libx264 @ 0000000001244ae0] frame I:1     Avg QP:15.64  size: 16539
[libx264 @ 0000000001244ae0] frame P:36    Avg QP:13.14  size:   201
[libx264 @ 0000000001244ae0] frame B:98    Avg QP:18.22  size:    52
[libx264 @ 0000000001244ae0] consecutive B-frames:  3.0%  0.0%  2.2%     94.8%
[libx264 @ 0000000001244ae0] mb I  I16..4: 21.8% 65.8% 12.3%
[libx264 @ 0000000001244ae0] mb P  I16..4:  0.7%  1.9%  0.0%  P16..4:      2.1%  0.2%  0.1%  0.0%  0.0%    skip:95.0%
[libx264 @ 0000000001244ae0] mb B  I16..4:  0.0%  0.2%  0.0%  B16..8:  1.7%  0.0%  0.0%  direct: 0.0%  skip:98.1%  L0:47.1% L1:52.9% BI: 0.0%
[libx264 @ 0000000001244ae0] 8x8 transform intra:71.6% inter:84.1%
[libx264 @ 0000000001244ae0] coded y,uvDC,uvAC intra: 17.1% 18.0% 7.8% inter: 0.1% 0.1% 0.0%
[libx264 @ 0000000001244ae0] i16 v,h,dc,p: 61% 33%  4%  2%
[libx264 @ 0000000001244ae0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 47%  9% 41%  1%  0%  1%  0%  1%  1%
[libx264 @ 0000000001244ae0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 51% 20%  5%  2%  3%  4%  4%  3%  7%
[libx264 @ 0000000001244ae0] i8c dc,h,v,p: 81% 10%  9%  1%
[libx264 @ 0000000001244ae0] Weighted P-Frames: Y:5.6% UV:5.6%
[libx264 @ 0000000001244ae0] ref P L0: 79.4%  1.1% 12.6%  6.9%
[libx264 @ 0000000001244ae0] ref B L0: 52.6% 40.8%  6.6%
[libx264 @ 0000000001244ae0] ref B L1: 84.0% 16.0%
[libx264 @ 0000000001244ae0] kb/s:40.72    

出现错误是从一个片段转换到下一个片段时出现延迟。还有另一个错误,有时会发生,第一个片段播放正常,但当第二个片段开始时,播放速度非常快,并且所有其他片段似乎同时播放。

以下是我为此示例使用和创建的文件,以及原始视频和创建的清单: https://drive.google.com/open?id=0B2kwpgNH6c0AMy0zemxuUk84clU

我是 FFmpeg 的新手,也是论坛的新手,所以如果我做错了什么,请告诉我。

有人能帮助我吗?

答案1

我找到了解决这个问题的方法。

FFmpeg 有一个标志(copyts),根据文档: “不处理输入时间戳,但保留其值并尝试对其进行清理”。

这个标志解决了我在帖子中描述的问题,使得片段转换正常。

然而,分割和转码过程会导致帧的播放时间延迟(0.7 秒)。令人惊讶的是,当我们处理 .ts 文件时,此延迟会加倍,这意味着第一帧将延迟 1.4 秒才能播放。

更多信息请点击这里:https://stackoverflow.com/questions/29527882/ffmpeg-copyts-to-preserve-timestamp?answertab=active#tab-top

这个问题的解决方案也是使用另一个属性(muxdelay),它设置最大解复用解码延迟。如果我们将此属性设置为 0,则 0.7 秒的延迟将不复存在。

这意味着我的命令现在看起来像这样:

拆分:

ffmpeg.exe -i $inputVideo -vcodec copy -acodec copy -f segment -muxdelay 0 -segment_list out.m3u8 out%d.ts

转码

ffmpeg -i $segmentInput -vcodec libx264 -acodec copy -s 640:480 -copyts -muxdelay 0 $outputFilename.ts

由于某种原因,分段仍然会产生非常小的延迟(我用 2 个小视频(30 秒和 2 分钟)进行了测试,延迟为 0.08-0.1 秒,我不确定为什么会发生这种情况,我现在无法解决这个问题。如果有人找到原因,请给我发消息或在这里回答(我不知道是否允许)。

无论如何,这解决了发布的问题,我希望我能有所帮助。

相关内容