1

I need to Merge an audio (.mp3) and video (.mov) file and output an mp4 file using FFMpeg PHP library.

The following works:

$ffmpeg = \FFMpeg\FFMpeg::create([
    'ffmpeg.binaries'  => 'C:/ffmpeg/bin/ffmpeg.exe',
    'ffprobe.binaries' => 'C:/ffmpeg/bin/ffprobe.exe'
]);

/* Open video file */
$video = $ffmpeg->open('sample.mov');

/* Resize video */
$video
    ->filters()
    ->resize(new \FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();

/* Take screenshot from video */
$video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('screen.jpg');

However it's not quite what I need. The following command line code is exactly what I'm looking for but I can't figure out how to convert it into the same format as the above ($ffmpeg version).

exec("ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4");

Question: Anyone know how to convert the exec into PHP above?

Thanks guys.

Donal.Lynch.Msc
  • 3,365
  • 12
  • 48
  • 78

2 Answers2

0

Can you try with full paths

$cmd = 'C:\\ffmpeg\\bin\\ffmpeg.exe -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4';
exec($cmd, $output)

Looks like its not implemented in PHP-FFMPEG package, you have to do it that way.

https://github.com/PHP-FFMpeg/PHP-FFMpeg/issues/346#issuecomment-292701054

flakerimi
  • 2,580
  • 3
  • 29
  • 49
  • thanks very much, it outputs the file now but actually there is no sound.Tried with .mov and also .mp4. Both are transcoded to mp4 but I'm not hearing any sound with them.. – Donal.Lynch.Msc May 15 '20 at 18:16
  • you have to play with commands, I just helped to run commands. Check ffmped documentation for more combinations. – flakerimi May 18 '20 at 13:39
  • 1
    try this: ffmpeg -i input.mp4 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4 – flakerimi May 18 '20 at 13:43
0

There is not an explicit PHP-FFMPEG function, but it is possible to build your custom command.

Here an example to generate an MP4 video merging a PNG image and an MP3 file.

$ffmpeg = \FFMpeg\FFMpeg::create();
$advancedMedia = $ffmpeg->openAdvanced(['image.png', 'audio.mp3']);
$advancedMedia->map([], new \FFMpeg\Format\Video\X264('aac', 'libx264'), 'output.mp4')->save();

More details in the "AdvancedMedia" paragraph of the documentation.

madbob
  • 456
  • 1
  • 6
  • 13