Search code examples
ffmpegffmpeg-php

How to merge two .mp4 videos of different resolutions


I need to embed videos on youtube via the API, and to make the process a little more practical I want to merge my vignette with the videos before posting on youtube. However, the videos have different resolutions and different characteristics, so the solution I found leaves the final video with audio or streaming problems.

I ended up finding the following solution, which works well, but is extremely slow, see:

$code1 = 'ffmpeg -i vinheta.mp4 -qscale 0 vinheta.mpg';
$code2 = 'ffmpeg -i movie2.mp4 -qscale 0 movie2.mpg';
$code3 = 'cat vinheta.mpg movie2.mpg | ffmpeg -f mpeg -i - -qscale 0 -vcodec mpeg4';

system($code1);
system($code2);
system($code3);

This solution works well and without breaks, but it really takes a long time, some video takes up to 2 hours to convert, and after that the video is extremely heavy

I've researched it in several places, tested all kinds of code and this was the only one I found that works without leaving the final video with audio or video problems regardless of the resolution.

Another interesting solution is as follows:

$code1 = "ffmpeg -i vinheta.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1.ts";
$code2 = "ffmpeg -i 4.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2.ts";
// now join
$code3 = "ffmpeg -i \"concat:temp1.ts|temp2.ts\" -c copy -bsf:a aac_adtstoasc output.mp4";

system($code1);
system($code2);
system($code3);

The output file works perfectly fine. But only on my computer. When I send it to youtube, the audio of the second video keeps chopping

Is there any simpler and lighter way to do this without breaking my final video?


Solution

  • regarding the second code that was chopping the audio on youtube, I managed to solve it just by changing the codec. So now it's working perfectly fine as follows:

    $code1 = "ffmpeg -i vinheta.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1.ts";
    $code2 = "ffmpeg -i 4.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2.ts";
    // now join
    $code3 = "ffmpeg -i \"concat:temp1.ts|temp2.ts\" -c copy -acodec aac output.mp4";
    
    system($code1);
    system($code2);
    system($code3);