phpffmpegffmpeg-php

How to trim a video by 4 fragments to 4 seconds using the PHP-FFMpeg?


How to trim a video using the PHP-FFMpeg? I need to implement the following FFMpeg command:

ffmpeg -i 7207783801bb.mp4 -filter_complex \
"[0:v]trim=start=10:end=11,setpts=PTS-STARTPTS[a]; \
 [0:v]trim=start=20:end=21,setpts=PTS-STARTPTS[b]; \
  [a][b]concat[c]; \
 [0:v]trim=start=30:end=31,setpts=PTS-STARTPTS[d]; \
 [0:v]trim=start=40:end=41,setpts=PTS-STARTPTS[f]; \
   [d][f]concat[g]; \
 [c][g]concat[out1]" -map [out1] 7207783801bbout.mp4

When I use the following code:

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('7207783801bb.mpg');
$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(10), FFMpeg\Coordinate\TimeCode::fromSeconds(11));
$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(20), FFMpeg\Coordinate\TimeCode::fromSeconds(21));
$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(31));
$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(40), FFMpeg\Coordinate\TimeCode::fromSeconds(41));
$video->save(new FFMpeg\Format\Video\X264(), '7207783801bbout.mp4');

then I get only 40 seconds of video. And I need to get 10-11, 20-21, 30-31, 40-41 seconds using the PHP-FFMpeg. Only 4 seconds.


Solution

  • I this case FFMpeg\Media\AdvancedMedia can be used

    $ffmpeg = FFMpeg\FFMpeg::create();
    $video = $ffmpeg->openAdvanced(['7207783801bb.mp4']);
    $video->filters()
        ->custom('[0:v]', 'trim=start=10:end=11,setpts=PTS-STARTPTS', '[a]')
        ->custom('[0:v]', 'trim=start=20:end=21,setpts=PTS-STARTPTS', '[b]')
        ->custom('[a][b]', 'concat', '[c]')
        ->custom('[0:v]', 'trim=start=30:end=31,setpts=PTS-STARTPTS', '[d]')
        ->custom('[0:v]', 'trim=start=40:end=41,setpts=PTS-STARTPTS', '[f]')
        ->custom('[d][f]', 'concat', '[g]')
        ->custom('[c][g]', 'concat', '[out1]')
    ;
    $video
        ->map(['[out1]'], new \FFMpeg\Format\Video\X264(), '7207783801bbout.mp4')
        ->save();