Jass Jass - 1 year ago 46
PHP Question

get image from video without specifying height

I am using following code for extracting image from Video.

shell_exec('echo "Y" | ffmpeg -itsoffset -4 -i "'.base_path().'/assets/videos/'.$file_name.'.mp4" -vcodec mjpeg -vframes 1 -an -f rawvideo -s 1170x300 "'.base_path().'/assets/videos/thumbnail/'.$file_name.'.jpg"');

Here I have given static height and because of that image is stretching So I don't want to specify height so is there any way it adjust height automatically by maintaining quality of image.

I have tried following code as well but did not work

shell_exec('echo "Y" | ffmpeg -itsoffset -4 -i "'.base_path().'/assets/videos/'.$file_name.'.mp4" -vcodec mjpeg -vframes 1 -an -f rawvideo -s 1170x-1 "'.base_path().'/assets/videos/thumbnail/'.$file_name.'.jpg"');

Can anyone tell me solution for this?

Answer Source

First up, if echo "Y" | is intended to overwrite an existing thumbnail without prompting, there is a -y flag to ffmpeg which does this.

Now, assuming you want the image to be no larger than 1,170 pixels in either dimension, you can replace your -s parameter with a call to the scale video filter and appropriate arguments.

    'ffmpeg -y -itsoffset -4  -i "'.base_path().'/assets/videos/'.$file_name.'.mp4"'
    . ' -vcodec mjpeg -vframes 1 -an -f rawvideo'
    . ' -filter:v "scale=iw*min(min(1170/iw\,1170/ih)\,1):ih*min(min(1170/iw\,1170/ih)\,1)"'
    . ' "'.base_path().'/assets/videos/thumbnail/'.$file_name.'.jpg"');

This doesn't scale up videos that are smaller than 1,170 pixels in both directions. If you'd prefer to scale up these thumbnails, replace scale=iw*min(min(1170/iw\,1170/ih)\,1):ih*min(min(1170/iw\,1170/ih)\,1) with scale=iw*min(1170/iw\,1170/ih):ih*min(1170/iw\,1170/ih).

If the image should always have a width of 1,170 pixels even if this makes it taller than 1,170 pixels high, instead use scale=1170:ih*1170/iw.

And finally, to reduce the width to 1,170 pixels only if it's wider, scale=min(iw\,1170):min(ih\,ih*1170/iw)