FFMPEG 与复杂过滤器连接后重新编码视频

FFMPEG 与复杂过滤器连接后重新编码视频

我有一个用于 FFMPEG 的 bash 脚本,它接受两个输入Awoo.mkvAwoo1.mkv

./ffmpeg -y \
    -i Awoo.mkv \
    -i Awoo2.mkv \

使用复杂的过滤器连接这两个文件,这是因为将来不同编解码器的文件也将被连接。此过滤器创建两个输出[outv][outa]

-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]" \
-map '[outv]' -map '[outa]' \

然后使用各种设置将视频重新编码为两个单独的流。流 v:0 和 a:0 为 1080,流 v:1 和 a:1 为 720。

-c:v:0 libx264 -b:v:0 4000k -s:v:0 1920x1080 -r:v:0 30 -g:v:0 90 -preset:v:0 superfast -tune:v:0 zerolatency -level:v:0 4 -pix_fmt:v:0 yuv420p -c:a:0 aac -b:a:0 512k \
-c:v:1 libx264 -b:v:1 2500k -s:v:1 1280x720  -r:v:1 30 -g:v:1 90 -preset:v:1 superfast -tune:v:1 zerolatency -level:v:1 4 -pix_fmt:v:1 yuv420p -c:a:1 aac -b:a:1 256k \

最终输出为 HLS,因此包含一些详细信息

-hls_time 6 \
-hls_list_size 2 \
-hls_flags delete_segments \
-f hls -master_pl_name "${name}.m3u8" \

然后映射重新编码的输出流,此时有两个不同质量设置的输出。

-map 0:v -map 0:a -map 0:v -map 0:a \

然后定义变体流映射以将两个输出流都包含到 m3u8 播放列表中。

-var_stream_map "v:0,a:0 v:1,a:1" \

最后是有关 HLS 的一些额外细节。

-hls_segment_filename "sequence/${name}_%v-%d.ts" \
"sequence/${name}_%v.m3u8"

这是整个脚本。

name=gw

./ffmpeg -y \
    -i Awoo.mkv \
    -i Awoo2.mkv \
    -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]" \
    -map '[outv]' -map '[outa]' \
    -c:v:0 libx264 -b:v:0 4000k -s:v:0 1920x1080 -r:v:0 30 -g:v:0 90 -preset:v:0 superfast -tune:v:0 zerolatency -level:v:0 4 -pix_fmt:v:0 yuv420p -c:a:0 aac -b:a:0 512k  \
    -c:v:1 libx264 -b:v:1 2500k -s:v:1 1280x720  -r:v:1 30 -g:v:1 90 -preset:v:1 superfast -tune:v:1 zerolatency -level:v:1 4 -pix_fmt:v:1 yuv420p -c:a:1 aac -b:a:1 256k  \
    -hls_time 6 \
    -hls_list_size 2 \
    -hls_flags delete_segments \
    -f hls -master_pl_name "${name}.m3u8" \
    -map 0:v -map 0:a -map 0:v -map 0:a \
    -var_stream_map "v:0,a:0 v:1,a:1" \
    -hls_segment_filename "sequence/${name}_%v-%d.ts" \
    "sequence/${name}_%v.m3u8"

我知道,即使没有连接过滤器和单一输入,这个脚本也能正常工作,但我无法弄清楚如何将连接过滤器准确地融入其中,尤其是使用映射命令以及它是如何工作的。

如果我尝试在当前状态下运行该脚本,我会收到错误:

[hls @ 0x6a30180] Unable to find mapping variant stream
av_interleaved_write_frame(): Cannot allocate memory

这是整个输出:

ffmpeg version 4.0.3-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-6 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Input #0, matroska,webm, from 'Awoo.mkv':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    ENCODER         : Lavf56.40.101
  Duration: 00:02:24.35, start: 0.000000, bitrate: 1114 kb/s
    Stream #0:0(und): Video: h264 (Main), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 29 fps, 29 tbr, 1k tbn, 58 tbc (default)
    Metadata:
      CREATION_TIME   : 2017-01-27 05:02:53
      LANGUAGE        : und
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:02:24.310000000
    Stream #0:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      LANGUAGE        : eng
      DURATION        : 00:02:24.348000000
Input #1, matroska,webm, from 'Awoo2.mkv':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    ENCODER         : Lavf56.40.101
  Duration: 00:02:24.35, start: 0.000000, bitrate: 1114 kb/s
    Stream #1:0(und): Video: h264 (Main), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 29 fps, 29 tbr, 1k tbn, 58 tbc (default)
    Metadata:
      CREATION_TIME   : 2017-01-27 05:02:53
      LANGUAGE        : und
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:02:24.310000000
    Stream #1:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      LANGUAGE        : eng
      DURATION        : 00:02:24.348000000
Stream mapping:
  Stream #0:0 (h264) -> concat:in0:v0 (graph 0)
  Stream #0:1 (vorbis) -> concat:in0:a0 (graph 0)
  Stream #1:0 (h264) -> concat:in1:v0 (graph 0)
  Stream #1:1 (vorbis) -> concat:in1:a0 (graph 0)
  concat:out:v0 (graph 0) -> Stream #0:0 (libx264)
  concat:out:a0 (graph 0) -> Stream #0:1 (aac)
  Stream #0:0 -> #0:2 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:3 (vorbis (native) -> aac (native))
  Stream #0:0 -> #0:4 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:5 (vorbis (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x6a35bc0] using SAR=1/1
[libx264 @ 0x6a35bc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x6a35bc0] profile High, level 4.0, 4:2:0, 8-bit
[libx264 @ 0x6a35bc0] 264 - core 157 r2935 545de2f - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=1 deblock=1:0:0 analyse=0x3:0x3 me=dia subme=1 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=4 lookahead_threads=4 sliced_threads=1 slices=4 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=1 keyint=90 keyint_min=9 scenecut=40 intra_refresh=0 rc=abr mbtree=0 bitrate=2500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[libx264 @ 0x6a02980] using SAR=1/1
[libx264 @ 0x6a02980] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x6a02980] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 0x6a02980] 264 - core 157 r2935 545de2f - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[libx264 @ 0x6b72640] using SAR=1/1
[libx264 @ 0x6b72640] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x6b72640] profile High, level 4.0, 4:2:0, 8-bit
[libx264 @ 0x6b72640] 264 - core 157 r2935 545de2f - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=1 deblock=1:0:0 analyse=0x3:0x3 me=dia subme=1 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=4 lookahead_threads=4 sliced_threads=1 slices=4 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=1 keyint=90 keyint_min=9 scenecut=40 intra_refresh=0 rc=abr mbtree=0 bitrate=4000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[hls @ 0x6a30180] Opening 'sequence/gw_0-0.ts' for writing
[hls @ 0x6a30180] Opening 'sequence/gw_1-0.ts' for writing
Output #0, hls, to 'sequence/gw_%v.m3u8':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    encoder         : Lavf58.12.100
    Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 4000 kb/s, 30 fps, 90k tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.18.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/4000000 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 512 kb/s
    Metadata:
      encoder         : Lavc58.18.100 aac
    Stream #0:2(und): Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 2500 kb/s, 30 fps, 90k tbn, 30 tbc (default)
    Metadata:
      CREATION_TIME   : 2017-01-27 05:02:53
      LANGUAGE        : und
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:02:24.310000000
      encoder         : Lavc58.18.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/2500000 buffer size: 0 vbv_delay: -1
    Stream #0:3(eng): Audio: aac (LC), 44100 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      LANGUAGE        : eng
      DURATION        : 00:02:24.348000000
      encoder         : Lavc58.18.100 aac
    Stream #0:4(und): Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 29 fps, 29 tbn, 29 tbc (default)
    Metadata:
      CREATION_TIME   : 2017-01-27 05:02:53
      LANGUAGE        : und
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:02:24.310000000
      encoder         : Lavc58.18.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:5(eng): Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      LANGUAGE        : eng
      DURATION        : 00:02:24.348000000
      encoder         : Lavc58.18.100 aac
frame=   41 fps=0.0 q=18.0 q=17.0 q=0.0 size=N/A time=00:00:01.46 bitrate=N/A du
[hls @ 0x6a30180] Unable to find mapping variant stream
av_interleaved_write_frame(): Cannot allocate memory

答案1

我想我已经找到了一个解决方案,我添加了一个执行完全相同操作的额外过滤器并且仅映射了过滤器输出:

name=gw

./ffmpeg -y \
    -i Awoo.mkv \
    -i Awoo2.mkv \
    -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1:unsafe=1:[outv][outa]" \
    -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1:unsafe=1:[outv][outa]" \
    -c:v:0 libx264 -b:v:0 4000k -s:v:0 1920x1080 -r:v:0 30 -g:v:0 90 -preset:v:0 superfast -tune:v:0 zerolatency -level:v:0 4 -pix_fmt:v:0 yuv420p -c:a:0 aac -b:a:0 512k  \
    -c:v:1 libx264 -b:v:1 2500k -s:v:1 1280x720  -r:v:1 30 -g:v:1 90 -preset:v:1 superfast -tune:v:1 zerolatency -level:v:1 4 -pix_fmt:v:1 yuv420p -c:a:1 aac -b:a:1 256k  \
    -map [outv] -map [outa] -map [outv] -map [outa] \
    -hls_time 6 \
    -hls_list_size 2 \
    -hls_flags delete_segments \
    -hls_segment_filename "sequence/${name}_%v-%d.ts" \
    -master_pl_name "${name}.m3u8" \
    -var_stream_map "v:0,a:0 v:1,a:1" \
    -f hls "sequence/${name}_%v.m3u8"

如果有人知道这将导致任何问题、潜在的开销或任何改进,我都会很高兴听到。

相关内容