0

I am trying to generate the images from mp4 video file with ffmpeg.

I only want the first 3 second of every video in the file.

I have the following command..

 $videoFile = $mp4File; //I have many mp4 files

 $ffmpeg ="/usr/ffmpeg";

 $image_path = $videoFile;

 $ALL_PLACE_WIDTH = 300;
 $ALL_PLACE_HEIGHT = 220;

 $image_cmd = " -r 1 -r 0.03 -s ".$ALL_PLACE_WIDTH."x".$ALL_PLACE_HEIGHT." -f image2 ";
 $dest_path = 'project/image-%01d.png';

 $str_command= $ffmpeg  ." -i " . $image_path . $image_cmd .$dest_path;
 shell_exec($str_command);

I am trying to generate firt 3 second image of every mp4 file but my script will genearte a lot of images and it create images across entire mp4 file.

Can I restrict the script to get only the first 3 seconds images only? Thanks a lot!

FlyingCat
  • 13,288
  • 34
  • 109
  • 184

1 Answers1

1

If I understand you correctly you want one image from three seconds in.

Change:

$image_cmd = " -r 1 -r 0.03 -s ".$ALL_PLACE_WIDTH."x".$ALL_PLACE_HEIGHT." -f image2 ";

to:

$image_cmd = " -vf scale=".$ALL_PLACE_WIDTH.":-1 -frames:v 1 -ss 3 ";

What's different?

  • -r and -f image2 are not required.

  • The scale filter can automatically adjust the height or width with -1 in relation to your given value so aspect is preserved. This avoids stretching or squishing that could result from an absolute resize. This means that $ALL_PLACE_HEIGHT = 220; is not needed.

  • Use -frames:v 1 to output one frame.

  • -ss is the offset time, or the time that you want the image from.

llogan
  • 87,794
  • 21
  • 166
  • 190