ffmpeg video thumbnail size - php

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.

Related

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?
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)

How to rotate thumbnail image in ffmpeg

I am using this code for my thumbnail image
<?php
$ffmpeg = '/usr/bin/ffmpeg';
$second = 2;
$video = 'video/123.mp4';
$image = 'thumb/123.jpg';
$command = "$ffmpeg -itsoffset -$second -i $video -vcodec mjpeg -vframes 1 -an -f rawvideo -s 175x150 $image";
its giving me the thumbnail image BUT i want to rotate that thumbnail image. I already tried transpose and rotate but nothing happen even i lost the thumbnail image also.
Tried solutions but nothing happen :-
$command ="$ffmpeg -itsoffset -$second -i $video -vcodec mjpeg -vframes 1 -vf 'transpose=1' -an -f rawvideo -s 175x150 $image";
and
$command ="$ffmpeg -itsoffset -$second -i $video -vcodec mjpeg -vframes 1 -vf 'rotate=PI/2' -an -f rawvideo -s 175x150 $image";
Please help me.....

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;

Error while Creating Videos from images using ffmpeg

I am trying to create videos from images sequence. I tried all the below code given in $ff_command variable.
$path = dirname(__FILE__);
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg video.mpg";
$ff_command = "ffmpeg -r 10 -b 1800 -i {$path}/img%03d.jpg test1800.mpg";
$ff_command = "ffmpeg -i {$path}/img%03d.jpg -s 1280x720 -aspect 16:9 -r 24 -vb 20M teste.mp4";
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg -vcodec mpeg4 -b 800k video.avi";
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg -vcodec libx264 -b 800k video.avi";
Most of these commands are creating a video file. But when I open that video nothing comes or first image is coming for a split second and nothing after that
Please let me know what I am doing wrong here
I noticed that video is too short to see. Is it possible to increase the video duration so that we can see all the images?

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