简体   繁体   中英

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.

From the manual pages

Clip

The clip filter takes two parameters:

$start, an instance of FFMpeg\Coordinate\TimeCode, specifies the start point of the clip

$duration, optional, an instance of FFMpeg\Coordinate\TimeCode, specifies the duration of the clip

(Emphasis mine)

So your second parameter needs to be 1 instead of (for example) 11 as it is the clip duration...

$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(10),
       FFMpeg\Coordinate\TimeCode::fromSeconds(1));

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM