How to concatenate two or more videos with different dimensions in FFMpeg?
I have two videos I would like to concatenate.
- First Video Dimensions = 480x640
- Second Video Dimensions = 640x480
I tried concatenating them and received an error. I've included the full error below.
ffmpeg -i 682_segment_0.mp4 -i 682_segment_1.mp4 -filter_complex \
"[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" -ar 44100 -vcodec libx264 \
-strict -2 -acodec aac 682.mp4
ffmpeg version 2.8.8-0ubuntu0.16.04.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.2) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --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-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '682_segment_0.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.40.101
Duration: 00:00:19.30, start: 0.000000, bitrate: 335 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x640 [SAR 1:1 DAR 3:4], 219 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 120 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '682_segment_1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.40.101
Duration: 00:00:09.97, start: 0.000000, bitrate: 568 kb/s
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 460 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, 3.0, fltp, 104 kb/s (default)
Metadata:
handler_name : SoundHandler
[Parsed_concat_0 @ 0x202dea0] Input link in1:v0 parameters (size 640x480, SAR 1:1) do not match the corresponding output link in0:v0 parameters (480x640, SAR 1:1)
[Parsed_concat_0 @ 0x202dea0] Failed to configure output pad on Parsed_concat_0
Error configuring complex filters.
Invalid argument
I did a bit of checking, and it appears I need to resize one of the videos to be the same size as the other video before I could concatenate it. I'm not sure if that is correct, but I resized it using ffmpeg.
ffmpeg -i 682_segment_0.mp4 -vf scale=640:480 -strict -2 682_segment_0b.mp4
After resizing the 480x640 video to 640x480 I tried the concat command again. And got another error. (I've included the final error here, but not the whole output.)
Input #0 Video Stream: 640x480 [SAR 9:16 DAR 3:4]
Input #1 Video Stream: 640x480 [SAR 1:1 DAR 4:3]
[Parsed_concat_0 @ 0x22cefe0] Input link in1:v0 parameters (size 640x480, SAR 1:1) do not match the corresponding output link in0:v0 parameters (640x480, SAR 9:16)
[Parsed_concat_0 @ 0x22cefe0] Failed to configure output pad on Parsed_concat_0
I found that resizing the DAR allowed me to concat the two videos, but the resized portion of the completed video now has the wrong aspect ratio. I was hoping to basically have "black" on the sides with my resize without cropping the video.
I attached an example here:
Resize DAR command that worked but made aspect ratios wrong:
ffmpeg -i 682_segment_0.mp4 -ar 44100 -vcodec libx264 \
-strict -2 -acodec aac -s 360x480 682_segment_0e.mp4
I am wondering if I should try doing the concat and rescale at the same time, but I am not sure how to do so.
Solution 1:
From the concat filter documentation:
All corresponding streams must have the same parameters in all segments; the filtering system will automatically select a common pixel format for video streams, and a common sample format, sample rate and channel layout for audio streams, but other settings, such as resolution, must be converted explicitly by the user.
You will have to scale and pad (or crop) to make 480x640 fit into 640x480:
ffmpeg -i 480.mp4 -i 640.mp4 -filter_complex \
"[0:v]scale=640:480:force_original_aspect_ratio=decrease,pad=640:480:(ow-iw)/2:(oh-ih)/2[v0]; \
[v0][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
Your
ffmpeg
is old: you should really consider updating to a build from the current git master branch. The easiest way to do that is to just download a binary.If you're scripting this then you should "conform" each input to common parameters using fps, format/aformat, and scale filters to avoid ffmpeg from choosing unexpected and undesired "common formats".
Also see Resizing videos with
ffmpeg
to fit into static sized player.