I have a tube site and im using this function to generate thumbs from video file
$thumbwidth = 240; //thumb width
$thumbheight = 180; //thumb height
$imagick_command = "-modulate 110,102,100 -sharpen 1x1 -enhance";
shell_exec("$ffmpeg_path -ss $first -i \"".$row[file]."\" -vcodec mjpeg -vframes 1 -an -f rawvideo -s ".$thumbwidth."x".$thumbheight." \"$image\"");
shell_exec("/usr/local/bin/mogrify $imagick_command $image");
Here is thumbs result, this image is just what i need, no border and etc..
but sometimes depending on video i have thumbs like this
Whats the best way to remove this black space from thumbs, but need to keep thumb size 240x180
You need to:
Resize the image with ffmpeg keeping the aspect ratio, so that it doesn't add any borders. Which is -vf scale=".$thumbwidth.":trunc(ow/a/2)*2
below.
Resize the image to the exact size you want. Which is -resize ".$thumbwidth."x".$thumbheight."\!
below.
So the new set of commands should look like:
$thumbwidth = 240; //thumb width
$thumbheight = 180; //thumb height
$imagick_command = "-modulate 110,102,100 -sharpen 1x1 -enhance -resize ".$thumbwidth."x".$thumbheight."\!";
shell_exec("$ffmpeg_path -ss $first -i \"".$row[file]."\" -vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale=".$thumbwidth.":trunc(ow/a/2)*2 \"$image\"");
shell_exec("/usr/local/bin/mogrify $imagick_command $image");
Tested on ffmpeg with a build from last Sept with the parameters set as actual values to make it easier to read:
ffmpeg -ss 1 -i GOPR9876.MP4 -vcodec mjpeg -vframes 1 -an -f rawvideo -vf scale=240:trunc\(ow/a/2\)*2 "foo.jpg"
Use the ffmpeg scale filter to create a thumbnail overflowing the area of 240x180 pixels.
Imagine you have a video of 320x256 pixels. To get a width of 240 you need to scale with with a factor 0.75. To get a height of 180 you need to scale with 0.70. If you take the maximum of two factors you will get the thumbnail with a size 240x192, overflowing the target area without any black borders.
Next do the central crop to get the perfect 240x180 thumbnail, removing 6px up and down.
If the video height was larger than its width, the math is the same. After resize you will have a thumbnail with height 180 and somewhat larger width, and the crop will take the central part of it making it perfect.
With ffmpeg, you can use scale and crop filters in a filter chain:
-vf scale='iw*max(240/iw\, 180/ih):-1', crop=240:180`
You should use escapeshellarg()
when creating a command line string from foreign inputs. Here is the PHP code you can use:
<?php
// static configuration - safe
$thumbwidth = 240;
$thumbheight = 180;
$ffmpeg_path = 'ffmpeg';
// foreign input - unsafe
$first = '00:00:30';
$row = array('file' => '/home/goran/File Name.avi');
$image = '/home/goran/File Name.jpg';
$cmd = sprintf('%s ' .
'-ss %s -i %s -vcodec mjpeg -vframes 1 -an -f rawvideo ' .
'-vf scale=\'iw*max(%d/iw\, %d/ih):-1\', crop=%d:%d '.
'%s',
$ffmpeg_path,
escapeshellarg($first), escapeshellarg($row['file']),
$thumbwidth, $thumbheight, $thumbwidth, $thumbheight,
escapeshellarg($image));
shell_exec($cmd);