Merge and Concat Multiple Audio and Video files using FFMPEG

纵饮孤独 提交于 2021-02-11 13:52:47

问题


I have a script at present that results in several audio-only and video-only files.

My goal is to take these and then concat each video clip whilst merging in the audio-clips.

I thought this would be easy but I have not been able to find a good example to work from.

FFMPEG.org suggests that the movie/amovie input syntax could be the best option.. but am craving more doco. FFMPEG's information on the topic is @ https://www.ffmpeg.org/ffmpeg-filters.html#Examples-124. It suggests that to "...Concatenate two parts, handling audio and video separately" we should be able to use the "(a)movie sources" and includes this example:

movie=part1.mp4, scale=512:288 [v1] ; amovie=part1.mp4 [a1] ;
movie=part2.mp4, scale=512:288 [v2] ; amovie=part2.mp4 [a2] ;
[v1] [v2] concat [outv] ; [a1] [a2] concat=v=0:a=1 [outa]

I have tried this myself without any success:

-f lavfi
-i amovie=/clips/1-0.mp3 [a0];
   movie=/clips/1-1-overlayed.mp4 [v1];
   amovie=/clips/1-2.mp3 [a2];
   movie=/clips/1-3-overlayed.mp4 [v3];
   amovie=/clips/1-4.mp3 [a4];
   movie=/clips/1-5-overlayed.mp4 [v5];
   [v1] [v3] [v5] concat=n=3 "[outv]" ; [a0] [a2] [a4] concat=n=3:v=0:a=1 "[outa]"
-map "[outv]"
-map "[outa]"
output.mp4

gives:

"[outa]": Invalid argument

Any ideas on how to get this working? (doesn't have to use the same movie/amovie constructs, my main requirement is that I have an array of items and need to produce a video from them, that is a concat of each video, and a merge of each audio.

...

When I use regular concat, I get this error:

[mp3 @ 0x7fd200003a00] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '/clips/1-0.mp3':
  Duration: 00:00:04.27, start: 0.000000, bitrate: 32 kb/s
    Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/clips/1-1-overlayed.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:00:04.28, start: 0.000000, bitrate: 28 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 3:4 DAR 4:3], 24 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
[mp3 @ 0x7fd20080ac00] Estimating duration from bitrate, this may be inaccurate
Input #2, mp3, from '/clips/1-2.mp3':
  Duration: 00:00:14.38, start: 0.000000, bitrate: 32 kb/s
    Stream #2:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Input #3, mov,mp4,m4a,3gp,3g2,mj2, from '/clips/1-3-overlayed.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:00:14.36, start: 0.000000, bitrate: 530 kb/s
    Stream #3:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 528 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
[mp3 @ 0x7fd20100d200] Estimating duration from bitrate, this may be inaccurate
Input #4, mp3, from '/clips/1-4.mp3':
  Duration: 00:00:02.78, start: 0.000000, bitrate: 32 kb/s
    Stream #4:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Input #5, mov,mp4,m4a,3gp,3g2,mj2, from '/clips/1-5-overlayed.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:00:02.80, start: 0.000000, bitrate: 34 kb/s
    Stream #5:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 3:4 DAR 4:3], 29 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Stream specifier ':a' in filtergraph description [0:a][1:v][2:a][3:v][4:a][5:v]concat=n=3:v=1:a=1[v][a] matches no streams.

The input (via NodeJS wrapper) looks like this:

FFmpeg {
  options:
   [ '-hide_banner',
     '-y',
     '-i',
     '/clips/1-0.mp3',
     '-i',
     '/clips/1-1-overlayed.mp4',
     '-i',
     '/clips/1-2.mp3',
     '-i',
     '/clips/1-3-overlayed.mp4',
     '-i',
     '/clips/1-4.mp3',
     '-i',
     '/clips/1-5-overlayed.mp4',
     '-filter_complex',
     '[0:a][1:v][2:a][3:v][4:a][5:v]concat=n=3:v=1:a=1[v][a]',
     '-map',
     '[v]',
     '-map',
     '[a]',
     '/projects/1.mp4' ],
  outputFilename: '' }

回答1:


No need for (a)movie or two concats. Just list each input normally:

ffmpeg -i input-a.mp4 -i input-a.mp3 -i input-b.mp4 -i input-b.mp3 -filter_complex "[0:v][1:a][2:v][3:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" output.mp4


来源:https://stackoverflow.com/questions/53855319/merge-and-concat-multiple-audio-and-video-files-using-ffmpeg

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