使用以下方式手动定义输出流-map

使用以下方式手动定义输出流-map

我用它-filter_complex来制作由多张图片组成的视频,并将它们淡入淡出。这个方法很好用。现在我想为视频添加音轨,但命令失败了。我该如何为正在生成的视频文件添加音频?

以下是我正在做的但没有效果的事情:

ffmpeg \
-i sunlight.mp3 \
-loop 1 -t 1 -i 1.jpg \
-loop 1 -t 1 -i 2.jpg \
-loop 1 -t 1 -i 3.jpg \
-loop 1 -t 1 -i 4.jpg \
-loop 1 -t 1 -i 5.jpg \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; \
[4:v][3:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b4v]; \
[0:v][b1v][1:v][b2v][2:v][b3v][3:v][b4v][4:v]concat=n=9:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4

以下是 ffmpeg 的输出:

    ffmpeg version 3.1.3 Copyright (c) 2000-2016 the FFmpeg developers
        built with Apple LLVM version 7.3.0 (clang-703.0.31)
        configuration: --prefix=/usr/local/Cellar/ffmpeg/3.1.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --disable-lzma --enable-vda
        libavutil      55. 28.100 / 55. 28.100
        libavcodec     57. 48.101 / 57. 48.101
        libavformat    57. 41.100 / 57. 41.100
        libavdevice    57.  0.101 / 57.  0.101
        libavfilter     6. 47.100 /  6. 47.100
        libavresample   3.  0.  0 /  3.  0.  0
        libswscale      4.  1.100 /  4.  1.100
        libswresample   2.  1.100 /  2.  1.100
        libpostproc    54.  0.100 / 54.  0.100
    Input #0, mp3, from 'sunlight.mp3':
        Metadata:
            encoder         : Lavf57.41.100
        Duration: 00:02:05.68, start: 0.025057, bitrate: 128 kb/s
            Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
            Metadata:
                encoder         : Lavc57.48
    Input #1, image2, from '1.jpg':
        Duration: 00:00:00.04, start: 0.000000, bitrate: 42613 kb/s
            Stream #1:0: Video: mjpeg, yuvj444p(pc, bt470bg/unknown/unknown), 1200x900 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
    Input #2, image2, from '2.jpg':
        Duration: 00:00:00.04, start: 0.000000, bitrate: 44551 kb/s
            Stream #2:0: Video: mjpeg, yuvj444p(pc, bt470bg/unknown/unknown), 1200x900 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
    Input #3, image2, from '3.jpg':
        Duration: 00:00:00.04, start: 0.000000, bitrate: 40433 kb/s
            Stream #3:0: Video: mjpeg, yuvj444p(pc, bt470bg/unknown/unknown), 1200x900 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
    Input #4, image2, from '4.jpg':
        Duration: 00:00:00.04, start: 0.000000, bitrate: 57157 kb/s
            Stream #4:0: Video: mjpeg, yuvj444p(pc, bt470bg/unknown/unknown), 1200x900 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
    Input #5, image2, from '5.jpg':
        Duration: 00:00:00.04, start: 0.000000, bitrate: 51745 kb/s
            Stream #5:0: Video: mjpeg, yuvj444p(pc, bt470bg/unknown/unknown), 1200x900 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
    Stream specifier ':v' in filtergraph description [1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; [2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; [3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; [4:v][3:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b4v]; [0:v][b1v][1:v][b2v][2:v][b3v][3:v][b4v][4:v]concat=n=9:v=1:a=0,format=yuv420p[v] matches no streams.

当我运行不带该命令时,-i sunlight.mp3它运行正常。

答案1

您有两种方法可以尝试:

使用以下方式手动定义输出流-map

我通常建议手动映射您的流,而不是依赖默认的流选择行为,这样您就不会得到意外的结果,但这要求您:

  1. 标记过滤器输出,就像你对[v]
  2. 使用-map

例子:

ffmpeg \
-loop 1 -t 1 -i 1.jpg \
-loop 1 -t 1 -i 2.jpg \
-loop 1 -t 1 -i 3.jpg \
-loop 1 -t 1 -i 4.jpg \
-loop 1 -t 1 -i 5.jpg \
-i sunlight.mp3 \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; \
[4:v][3:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b4v]; \
[0:v][b1v][1:v][b2v][2:v][b3v][3:v][b4v][4:v]concat=n=9:v=1:a=0,format=yuv420p[v]" -map "[v]" -map 5:a -shortest out.mp4

流说明符您在使用的价值观过滤图在你的-map选项取决于您的输入的顺序。

使用默认的流选择行为

由于最终输出仅包含一个视频流和一个音频流,因此您可以省略-map输出标签([v]),而只需使用默认流选择行为。如果您正在编写脚本,这可能是更易于使用的方法。

ffmpeg \
-loop 1 -t 1 -i 1.jpg \
-loop 1 -t 1 -i 2.jpg \
-loop 1 -t 1 -i 3.jpg \
-loop 1 -t 1 -i 4.jpg \
-loop 1 -t 1 -i 5.jpg \
-i sunlight.mp3 \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; \
[4:v][3:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b4v]; \
[0:v][b1v][1:v][b2v][2:v][b3v][3:v][b4v][4:v]concat=n=9:v=1:a=0,format=yuv420p" -shortest out.mp4

答案2

您的脚本有两个基本错误:

  1. 在第一个过滤链中(参见的第一行filter_complex),您将其指定0:v为输入流。但正如 ffmpeg 输出所示:Stream specifier ':v' in filtergraph description ... matches no streams.文件中没有视频流0。请注意,您发送了一个mp3文件作为第一个输入。将其作为最后一个输入文件并...

  2. -map 5:a在输出文件名前添加一个。您指定视频流[v]作为唯一的输出流。使用-map选项时,您必须使用该选项定义所有输出流。

相关内容