I'm trying to change video dimensions using ffmpeg. For example, user is uploading video 1280*960.
And i need to get video 640*480 in return.
I tried this code and it works
exec($ffmpegPath." -i ".$srcFile." -ar 22050 -ab 32 -f flv -s 640x480".$destFile,$tmp);
but this method d't keep width/height balance of video (i.e. video 5000*480 becomes 640*480 and looks compressed from left and right).
In case of this dimensions (5000*480) i need to get video on exit like 640*61 i mean fixed height is not suitable for me.
I look over the internet and found this topics https://superuser.com/questions/201051/resize-videos-with-different-widths-to-a-fixed-height-preserving-aspect-ratio-wi http://delogics.blogspot.com/2011/11/ffmpeg-maintain-aspect-ratio-with-fixed.html
but neither of them works for me (ffmpeg even not works with this code, maybe i've made mistake when adding theirs code parts in my ffmpeg string)
Help me to improve my code pls
Thanks
If the ffmpeg
supports video filters then you can use scale
filter and resize the video to 640 pixel width and proportionally changed height as the following:
exec($ffmpegPath." -i ".$srcFile." -ar 22050 -ab 32 -f flv -vf scale=640:ih*640/iw ".$destFile,$tmp);
I've had this problem myself and I ended up writing my own function (it's in C++).
QSize fitTo(QSize originalSize, QSize fitTo)
{
int width = originalSize.width();
int height = originalSize.height();
int fitToWidth = fitTo.width();
int fitToHeight = fitTo.height();
if (fitToWidth*height/width/2*2 <= fitToHeight)
{
height = fitToWidth*height/width/2*2;
width = fitToWidth;
}
else
{
width = fitToHeight*width/height/2*2;
height = fitToHeight;
}
return QSize(width, height);
}
Here "/2*2" is not redundant, it ensures that the number is even. Ffmpeg doesn't accept odd numbers for width or height.
I'm not to fluent in ffmpeg, but I can't seem to find a way to do that. I believe you can do it in mencoder instead, or you can try to get the width and height of the video and calculate the desired height yourself.
ob_start();
passthru('ffmpeg -i "'. $srcFile . '" 2>&1');
$output = ob_get_clean();
ob_end_clean();
preg_match('/(\d+)x(\d+)/', $output, $matches);
$width = $matches[1];
$height = $matches[2];
$ratio = $width/640;
$newwidth = 640;
$newheight = $height/$ratio;