Innovative High Performance Web Hosting Solutions.
 Switching hosts?
Vexxhost makes switching hosts quick and easy. We'll even move your files and your databases from your old host to our servers for free.
Contact us web hosting
Happy Customers
I initially join vexxhost for their ultra competitive pricing and all inclusive packages. The test came though when I required specific configuration to my domain for some of my custom scripts to run. I classed this as low priority as I was only in testing phase, and for the price I pay, I didn't expect a great rush from their techies. I can't tell you how much I was surprised when not only did they get back to me in a matter of hours, but they were able to make server changes on a permanent basis, purely at my request. Great service, great response and great outcome! Well done vexxhost!

Aaron H.
web hosting
comodo

How to convert/encode files to FLV using FFMPEG & PHP
Posted: May 20, 2007 at 11:29 pm

Need help using specific FFMPEG-PHP functions or some programming help? Let our experienced technicians help you debug your problem for FREE. Sign up today and post at our online community: FFMPEG-PHP Help Forums

Good News: Customers of VEXXHOST Web Hosting can now automatically install PHPmotion and VidiScript instantly from cPanel, they do not need to do any ffmpeg configuration or ffmpeg installation, they simply enter a username and a password and the software will be ready and online in seconds.

So, as I’ve written in an earlier article on how to install FFMPEG on your server, while there are those who probably use a “YouTube Clone” script, there might be those who want to create their own using FFMPEG & PHP. FLV is the most widely used type of codec that runs on most Flash players.

So, let’s get started, there are actually a few steps into converting a file to FLV which are shown below

Flowchart

1. Send the script to FFMPEG-PHP and get it’s info
So, before doing any of this, you should make sure that your file has been uploaded to somewhere and you have the full path to it. (You can’t use what you have in “memory”, so you’ll have to look on how to upload a file, once you got that and have the path of the file, we’ll start our script to invoke FFMPEG-PHP and get the file’s resolution. What we mainly need is the width, height & FPS (frame per second) so that we can tell FFMPEG about. I’ll be using the clock.avi located in every windows system.

We’ll start out our code with getting our variables:

// Set our source file
$srcFile = "/path/to/clock.avi";
$destFile = "/path/to/clock.flv";
$ffmpegPath = "/path/to/ffmpeg";
$flvtool2Path = "/path/to/flvtool2";

// Create our FFMPEG-PHP class
$ffmpegObj = new ffmpeg_movie($srcFile);

// Save our needed variables
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();

Also, the width/height has to be multiples of two so I have created a function that makes it a multiple of two:

function makeMultipleTwo ($value)
{
$sType = gettype($value/2);

if($sType == "integer")
{
return $value;
} else {
return ($value-1);
}
}

2. Send the script to FFMPEG for encoding
Here is where the fun starts, executing it and telling FFMPEG where to place it later. Let’s see on how our command will consist and what it will be made of. We’ll see what quality settings we will have to set.

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv

That’s generally how to convert a video.avi to video.flv with the audio sampling at 22050 & audio bit rate at 32, with the size 320×240. While I suggest the values above for audio as they are the most compressed, but we’ll use the old audio settings for better quality.

$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();

Now we have pretty much most of the values ready for our compression, however, we need to call flvtool2 to get our Meta information. Steps 4 and 5 in the diagram work simultaneously with this one.

What we do is make flvtool2 run at the same time as FFMPEG so we’ll pipe it into the command which means our general command is

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv | flvtool2 -U stdin video.flv

Now, we have a kind of complete command, let’s make our final code!

<?php
// Set our source file
$srcFile = "/path/to/clock.avi";
$destFile = "/path/to/clock.flv";
$ffmpegPath = "/path/to/ffmpeg";
$flvtool2Path = "/path/to/flvtool2";
// Create our FFMPEG-PHP class
$ffmpegObj = new ffmpeg_movie($srcFile);
// Save our needed variables
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();
// Call our convert using exec()
exec($ffmpegPath . " -i " . $srcFile . " -ar " . $srcAR . " -ab " . $srcAB . " -f flv -s " . $srcWidth . "x" . $srcHeight . " " . $destFile . " | " . $flvtool2Path . " -U stdin " . $destFile);
// Make multiples function
function makeMultipleTwo ($value)
{
$sType = gettype($value/2);
if($sType == "integer")
{
return $value;
} else {
return ($value-1);
}
}
?>

Comments



Post a comment

Copyright © 2005-2009 vexxhost web hosting.
All prices are in USD unless otherwise indicated.