在现有视频之前和之后连接图像

时间:2018-01-30 10:12:42

标签: ffmpeg

我正在使用ffmpeg生成视频幻灯片。并希望在生成的视频文件之前和之后添加一个图像作为intro-outro幻灯片cocatenate。

这是我正在尝试运行的代码

ffmpeg -y -f concat -loop 1 -t 5 -i /path_to/intro.png -i /path_to/video.mp4 -loop 1 -t 5 -i /path_to/outro.png /path_to/output.mp4

但这是错误

Line 1: unknown keyword '�PNG'
/path_to/intro.png: Invalid data found when processing input

使用base64代码生成图像

$outro_data = 'data:image/png;base64,iVBOPgAAAAUdklEQVR4nO3d+ZdU5ZnA8fwZBqNA...';
list($type, $outro_data) = explode(';', $outro_data);
list(, $outro_data)      = explode(',', $outro_data);
file_put_contents($output_outro_image_path, base64_decode($outro_data));

示例图片: enter image description here

  

编辑2

错误日志

ffmpeg version 3.2.2-2~16.04.york0 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 5.4.1 (Ubuntu 5.4.1-4ubuntu1~16.04.york1) 20161202
  configuration: --prefix=/usr --extra-version='2~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, png_pipe, from 'in1.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgba(pc), 640x400 [SAR 2835:2835 DAR 8:5], 25 fps, 25 tbr, 25 tbn, 25 tbc
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'out_60.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
  Duration: 00:00:25.00, start: 0.000000, bitrate: 22 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x400 [SAR 1:1 DAR 8:5], 16 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #2, png_pipe, from 'in4.png':
  Duration: N/A, bitrate: N/A
    Stream #2:0: Video: png, rgba(pc), 640x400 [SAR 2835:2835 DAR 8:5], 25 fps, 25 tbr, 25 tbn, 25 tbc
Input #3, lavfi, from 'anullsrc':
  Duration: N/A, start: 0.000000, bitrate: 705 kb/s
    Stream #3:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
Stream specifier ':a' in filtergraph description [0]crop=iw:400[v0];[v0][3][1:v][1:a][2][3]concat=n=3:v=1:a=1[vv][a];[vv]format=yuv420p[v] matches no streams.

执行命令

ffmpeg -loop 1 -t 5 -i in1.png -i 1514273842_1106877394.mp4 -loop 1 -t 5 -i in4.png -f lavfi -t 5 -i anullsrc -filter_complex "[0]crop=iw:400[v0];[v0][3][1:v][1:a][2][3]concat=n=3:v=1:a=1[vv][a];[vv]format=yuv420p[v]" -map "[v]" -map "[a]" output_concat.mp4

2 个答案:

答案 0 :(得分:0)

concat过滤器

您可以使用concat filter

ffmpeg -loop 1 -t 5 -i intro.png -i video.mp4 -loop 1 -t 5 -i outro.png -f lavfi -t 5 -i anullsrc -filter_complex "[0][3][1:v][1:a][2][3]concat=n=3:v=1:a=1[vv][a];[vv]format=yuv420p[v]" -map "[v]" -map "[a]" output.mp4
  • 优点是您可以在一行中执行所有操作,并且可以使用各种过滤器匹配每个输入的参数。

  • 缺点是您重新编码video.mp4

concat demuxer

或者,从符合video.mp4参数的图像制作视频。您没有提供video.mp4的任何详细信息,因此我无法提供一个交钥匙示例。然后使用concat demuxer

ffmpeg -f concat -i input.txt -c copy output.mp4

input.txt包含:

file 'intro.mp4'
file 'video.mp4'
file 'outro.mp4'
  • 优点是video.mp4是流复制而不是重新编码,整个过程可能比concat过滤方法更快。

  • 缺点是如果输入文件参数不匹配,则更容易出现用户错误。

答案 1 :(得分:0)

{
    "-loop",
    " 1",
    "-framerate",
    "24",
    "-t",
    "5",
    "-i",
    imag1.jpg,
    "-i",
    video.mp4,
    "-loop",
    " 1",
    "-framerate",
    "24",
    "-t",
    "5",
    "-i",
    image2.jpg,
    "-f",
    "lavfi",
    "-t",
    "0.1",
    "-i",
    "anullsrc=channel_layout=stereo:sample_rate=44100",
    "-filter_complex",
    "[2]scale=480:360,setsar=1[im];[1]scale=480:360,setsar=1[vid];[0]scale=480:360,setsar=1[im]; [im][3][vid][1][im][3] concat=n=3:v=1:a=1",
    filePath
};