1

I want to marge four videos in to one as show below. I have achieve the correct output given all the videos have the same resolution. But I get an undesirable result with different resolution. See below for more details.

i need ffmpeg code to get to merge 4 videos of different resolution in a grid format

ffmpeg.exe -i "1.avi" -vf "[in] scale=iw:ih, pad=2*iw:ih [left];movie=2.avi, scale=iw:ih [right]; [left][right] overlay=main_w/2:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=iw:ih, pad=2*iw:ih [left];movie=4.avi, scale=iw:ih [right]; [left][right] overlay=main_w/2:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"

Code above gives the follow output.

ffmpeg -i 1.avi

Input #0, avi, from '1.avi':
  Metadata:
    encoder         : Lavf55.44.100
  Duration: 00:00:03.10, start: 0.000000, bitrate: 773 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 640
x480 [SAR 1:1 DAR 4:3], 782 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

ffmpeg -i 2.avi

Input #0, avi, from '2.avi':
  Metadata:
    encoder         : Lavf55.44.100
  Duration: 00:00:03.10, start: 0.000000, bitrate: 773 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 640
x480 [SAR 1:1 DAR 4:3], 782 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

ffmpeg -i 3.avi

Input #0, avi, from '3.avi':
  Metadata:
    encoder         : Lavf55.44.100
  Duration: 00:00:03.10, start: 0.000000, bitrate: 699 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 640
x480 [SAR 1:1 DAR 4:3], 705 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

ffmpeg -i 4.avi

Input #0, avi, from '4.avi':
  Metadata:
    encoder         : Lavf55.44.100
  Duration: 00:00:03.10, start: 0.000000, bitrate: 724 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p,          
640x480 [SAR 1:1 DAR 4:3], 731 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

For Example

But the problem arises when I rotate one of the videos and merge them.

ffmpeg -i 1.avi (480x640) Only change

Input #0, avi, from '1.avi':
Metadata:
encoder         : Lavf55.44.100
Duration: 00:00:03.10, start: 0.000000, bitrate: 961 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 
480x640 [SAR 1:1 DAR 3:4], 976 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

enter image description here.

Please tell me what I need to change to get the desired result(Shown Below). Thanks enter image description here

1
  • Please show the complete console output from one of the commands. You omitted the build and configure options which are always informative.
    – llogan
    Jul 6, 2015 at 17:30

1 Answer 1

0

Figured it out given the videos are 640X480 and rotated ones are 480X640. Here are four scenarios. Hope it helps someone in the future. :)

{filenumber}={angle}

1=90 2=0 3=0 4=0

ffmpeg.exe -i "1.avi" -vf "[in] scale=0:480 , pad=width=640+640:height=480:x=(640-480)/2:y=0:color=red [left];movie=2.avi, scale=640:480, pad=width=640:height=480:x=0:y=0:color=red [right]; [left][right] overlay=640:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=0:0 , pad=width=640+640:height=480:x=0:y=0:color=red [left];movie=4.avi, scale=0:0, pad=width=640:height=480:x=0:y=0:color=red [right]; [left][right] overlay=640:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"
Pause

2=0 1=90 3=0 4=0

ffmpeg.exe -i "2.avi" -vf "[in] scale=0:0 , pad=width=640+640:height=480:x=0:y=0:color=red [left];movie=1.avi, scale=0:480 , pad=width=640:height=480:x=(640-480)/2:y=0:color=red [right]; [left][right] overlay=640:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=iw:ih, pad=2*iw:ih [left];movie=4.avi, scale=iw:ih [right]; [left][right] overlay=main_w/2:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"
Pause

90 90 0 0

ffmpeg.exe -i "2.avi" -vf "[in] scale=0:480 , pad=width=640+640:height=480:x=(640-480)/2:y=0:color=red [left];movie=1.avi, scale=0:480 , pad=width=640:height=480:x=(640-480)/2:y=0:color=red [right]; [left][right] overlay=640:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=iw:ih, pad=2*iw:ih [left];movie=4.avi, scale=iw:ih [right]; [left][right] overlay=main_w/2:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"
Pause

90 90 90 90

ffmpeg.exe -i "1.avi" -vf "[in] scale=0:0 , pad=width=480+480:height=640:x=0:y=0:color=red [left];movie=2.avi, scale=0:0 , pad=width=480:height=640:x=0:y=0:color=red [right]; [left][right] overlay=480:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=0:0 , pad=width=480+480:height=640:x=0:y=0:color=red [left];movie=4.avi, scale=0:0 , pad=width=480:height=640:x=0:y=0:color=red [right]; [left][right] overlay=480:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"
Pause

90 90 0 90

ffmpeg.exe -i "1.avi" -vf "[in] scale=0:0 , pad=width=480+480:height=640:x=0:y=0:color=red [left];movie=2.avi, scale=0:0 , pad=width=480:height=640:x=0:y=0:color=red [right]; [left][right] overlay=480:0 [out]" -b:v 768k "Output1.mp4"
ffmpeg.exe -i "3.avi" -vf "[in] scale=480:0 , pad=width=480+480:height=640:x=0:y=(640-480)/2:color=red [left];movie=4.avi, scale=0:0 , pad=width=480:height=640:x=0:y=0:color=red [right]; [left][right] overlay=480:0 [out]" -b:v 768k "Output2.mp4"
ffmpeg.exe -i "Output1.mp4" -vf "pad=iw:2*ih [top]; movie=Output2.mp4 [bottom]; [top][bottom] overlay=0:main_h/2" "Output_Stacked.mp4"
Pause
2
  • There is possibly an easier method, but I couldn't confirm that it would work for you since you didn't provide the complete console output.
    – llogan
    Jul 7, 2015 at 5:27
  • Will this not work with any video files? Jul 7, 2015 at 5:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .