Search code examples
phpffmpeg

Merging two videos via PHP


So, I'm trying to merge two files using PHP and I've taken a look at the FFMpeg library, yet I cannot manage to merge them. Here's what I've got so far:

$ffmpeg = FFMpeg\FFMpeg::create();
$vid1 = $ffmpeg->open('videos/vid1.mp4');
$vid2 = $ffmpeg->open('videos/vid2.mp4');

How do I stitch together $vid1 and $vid2 and save them?

Thanks


Solution

  • You can concatenate files directly by using shell_exec().

    See FFmpeg Concatenate

    Usage:

    shell_exec( "G:/ffmpeg/ffmpeg.exe -f concat -safe 0 -i G:/ffmpeg/concatenate.txt -c copy G:/ffmpeg/phpconcatenated.mp4" )
    

    concatenate.txt includes names of files you want to merge.

    # Example concatenate.txt
    file 'G:/ffmpeg/input.mp4'
    file 'G:/ffmpeg/input_2.mp4'