1

I want to merge multiple videos side by side in one video with PHP-FFmpeg, is there anyone help me about that?

public function GENERATE_VIDEO()
{
    require_once APPPATH . '/third_party/vendor/autoload.php';
    $ffmpeg = FFMpeg\FFMpeg::create([
        'ffmpeg.binaries'  => APPPATH . '/third_party/ffmpeg',
        'ffprobe.binaries'  => APPPATH . '/third_party/ffprobe',
    ]);;
    $inputs = array(
        '../output/01.webm',
        '../output/02.webm',
    );
    $advancedMedia = $ffmpeg->openAdvanced($inputs);
    $ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener());
    $ffmpeg->getFFMpegDriver()->on('debug', function ($message) {
        echo $message . "\n";
    });
    $advancedMedia = $ffmpeg->openAdvanced($inputs);
    $advancedMedia->filters()
        ->custom('[0]', 'overlay=:x=000:y=000', '[output0]')
        ->custom('[output0][1]', 'overlay=:x=058:y=000', '[output1]');
    $advancedMedia
        ->map(array('[output1]'), new FFMpeg\Format\Video\WebM(), '../output/output.webm')
        ->save();
}

Above code is working actually, but there is something wrong with custom filters, it's saving only one input.

1 Answers1

1

Use hstack filter instead of overlay filter if you want side-by-side.

Change:

    ->custom('[0]', 'overlay=:x=000:y=000', '[output0]')
    ->custom('[output0][1]', 'overlay=:x=058:y=000', '[output1]');

To:

    ->custom('[0][1]', 'hstack', '[output1]');
  • Both inputs must be the same height. If heights are different then add the scale, scale2ref, crop, or pad filters.

  • If you wanted to use overlay instead you need to pad the first video with the pad filter to provide space for the second video. But this method is slower and more complicated than just using hstack.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks for your answer, but that's not what I need, I want to use X-Y however, I cant use hstack there will be more video than two. – Prokfantasmist Dec 08 '20 at 18:59
  • 1
    @Prokfantasmist As the linked documentation states, hstack can accept multiple inputs. Such as `[0][1][2][3][4][5]hstack=inputs=6`. – llogan Dec 08 '20 at 19:01
  • I've edited the code i have, but still not working $advancedMedia->filters()->custom('[0][1][2][3]', 'hstack=inputs=4', '[V]'); – Prokfantasmist Dec 08 '20 at 19:07
  • @Prokfantasmist 1) Error messages are always better than just saying it's "not working". I can try to provide a bunch of guesses, but it's easiest if I know what the actual problem is. 2) Because you're basically just using the ffmpeg cli tool, and this is essentially just a ffmpeg cli quesiton, get it working in command-line first. Once you know the command works only then should you attempt to script it. Right now the PHP is just noise that is in the way. – llogan Dec 08 '20 at 19:10
  • How can I add scale for multiple inputs? – Prokfantasmist Dec 08 '20 at 22:48
  • 1
    @Prokfantasmist `ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex "[0:v]scale=-1:720[left];[1:v]scale=-1:720[right];[left][right]hstack" output.mp4` – llogan Dec 08 '20 at 22:51
  • One more question; I need to merge output.m4 two or more times merge as vertically. – Prokfantasmist Dec 10 '20 at 14:30
  • 1
    @Prokfantasmist See the [vstack](https://ffmpeg.org/ffmpeg-filters.html#vstack) filter and [Vertically or horizontally stack (mosaic) several videos using ffmpeg?](https://stackoverflow.com/a/33764934/) for more examples. – llogan Dec 10 '20 at 17:26