get image from video without specifying height - php

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?

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.
shell_exec(
'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)

Related

FFMPEG - Palettegen for animations is not working

I have a script taken from the correct answer at https://superuser.com/questions/1002562/convert-multiple-images-to-a-gif-with-cross-dissolve, but I am getting no results.
Even copying the code word for word does not work for me.
However, if I run $ffmpeg = shell_exec("ffmpeg -i images/image001.jpg -vf palettegen palette_test.png"); on a single image, it works fine. But because I need it for an animation, I need to create a pallete for all images.
Unless of course, on each image in the GIF sequence I can load a new palette?
$q = "ffmpeg -f image2 -framerate 0.5 -i ../view/client/files/".$dir."/%*.jpg -i palettePro.png -lavfi paletteuse -y .." . $save_path . '/' . $filename . " -report";
Palettegen can analyze and generate a matching frame sequence with per-frame palette.
ffmpeg -i images/image%03d.jpg -vf palettegen=stats_mode=single palette_%03d.png
And then
ffmpeg -framerate 0.5 -i images/image%03d.jpg -framerate 0.5 -i palette_%03d.png -lavfi paletteuse=new=1 -y $filename

ffmpeg video thumbnail size

I'm generating thumbnails like so:
exec(ffmpeg -itsoffset -4 -i '.$path.' -vcodec mjpeg -vframes 1 -an -f rawvideo -s 300x300 '.$other_path);
Is it possible to not specify the exact size of output image and set it to 100%?
You can simply remove the -s 300x300 option, and it will note resize.

ffmpeg php get frame / image after half time of the video or after 40 seconds

hello all i am having this code in my php and i want to get an image after 50% of the duration of the video and also get the duration of the video in a variable i have installed ffmpeg in my php and computer
the page has following code
require 'vendor/autoload.php';
//ececute ffmpeg generate mp4
exec('ffmpeg -i '.$uploadfile.' -f mp4 -s 896x504 '.$new_flv.'');
//execute ffmpeg and create thumb
exec('ffmpeg -i '.$uploadfile.' -f mjpeg -vframes 71 -s 768x432 -an '.$new_image_path.'');
i want an image after 50% of the duration of the video and also store the video duration in a variable
please give me some suggestions i am stuck over here
First you need to get the video duration in second
and then you need to multiply total second to 0.5 to get the half.
Try this one:
$total_sec = exec("ffprobe -loglevel error -show_streams inputFile.mp4 | grep duration | cut -f2 -d=");
$total_half_sec = $total_sec * 0.5;
exec("ffmpeg -i source.mp4 -f mjpeg -vframes $total_half_sec -s 768x432 -an destination.jpg");
For more info here https://www.ffmpeg.org/ffmpeg-all.html

ffmpeg generate thumbnail with aspect ratio, but X seconds into video

So this command works for generating a thumbnail 5 seconds into the video, with a size of 300x300:
$cmd = '/usr/local/bin/ffmpeg -i '.$this->getUploadRootDir().'/'.$fname.' -ss 00:00:05 -f image2 -vframes 1 -s 300x300 '.$this->getUploadRootDir().'/thumb_'.$output;
However, I want to keep the aspect ratio so I changed my code to this:
$cmd = "/usr/local/bin/ffmpeg -i ".$this->getUploadRootDir()."/".$fname." -ss 00:00:5 -f image2 scale='min(300\, iw):-1' ".$this->getUploadRootDir()."/thumb_".$output;
The above code correctly sizes the image, however it's of the first frame in the video.. I need the thumbnail to be 5 seconds in. Any help would be much appreciated.
You can do that with a command line similar to:
ffmpeg -i inputVideo -vf scale='min(300,iw)':-1 -ss 00:00:05 -f image2 -vframes 1 thumbnail.jpg
So in your script, add -vf (video filter) before scale and reorder input and output parameters like below:
$cmd = "/usr/local/bin/ffmpeg -i ".$this->getUploadRootDir()."/".$fname." -vf scale='min(300\, iw):-1' -ss 00:00:5 -f image2 -vframes 1 ".$this->getUploadRootDir()."/thumb_".$output;
#alexbuisson has the correct answer, but just to confirm as I'm playing with ffmpeg today:
This works for me:
ffmpeg -i "my_video.mp4" -ss 00:00:05 -f image2 -vf scale="min(300\, iw):-1" -vframes 1 "capture.jpg"
I get one Jpeg 300 px wide with the correct image ratio.
So adapted to your code this becomes:
$cmd = "/usr/local/bin/ffmpeg -i ".$this->getUploadRootDir()."/".$fname." -ss 00:00:05 -f image2 -vf scale='min(300\, iw):-1' ".$this->getUploadRootDir()."/thumb_".$output;

How to take pictures of the video through mplayer or ffmpeg to php?

How to take pictures of the video through mplayer or ffmpeg to php?
try,
exec("ffmpeg -i $video_file_path -an
-y -f mjpeg -ss 00:02:00 -vframes 1 $image_path")
Assuming ffmpeg is installed on your server, you can use the following code to output the frame at exactly 2 minutes to a JPEG file:
function vidtojpeg($video_filename, $dimensions) {
exec("ffmpeg -i $video_filename -an -ss 00:01:59 -t 00:00:01 -r 1 -y -s $dimensions video%d.jpg");
}
In this function, the $video_filename parameter is self-explanatory. The $dimensions parameter accepts width and height of the outputted images in this format: WIDTHxHEIGHT. For example: 320x480 would be an acceptable parameter.
Converting the video to frames and getting the required frames based on timings can help. Try this : ffmpeg -i video.flv -r 25 -vcodec png -pix_fmt rgb32 %d.png
You can manipulate the formats and bitrate(-r) for getting the required frame in proper format.

Categories