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



May 25th, 2007 at 10:10 am
[…] the Vexxblog, there’s this new tutorial that shows how, with FFMPEG and PHP, to convert and encode files to the FLV format. So, as I’ve […]
May 26th, 2007 at 9:01 am
[…] Source […]
May 26th, 2007 at 11:44 am
[…] (antes Macromedia) flash para reproducir un archivo de ese tipo en reproductores flash swf. En Vexxblog han publicado un tutorial para poder construirnos un script en php que permite pasar un video a éste formato, […]
May 27th, 2007 at 6:28 pm
this is probably not the type of script you would want to run on the same server as your web site, as ffmpeg does take up a lot of cpu and memory resources when processing, or at least, run it in the background so as not to make the user wait for it. nice tutorial though, maybe you can add something on converting to 3gp, for mobile users?
May 30th, 2007 at 3:33 am
// Make multiples function
function makeMultipleTwo ($value)
{
return ($value % 2) ? $value - 1 : $value;
}
May 30th, 2007 at 3:51 am
Cool script…I have it working, mostly. I am setting this up for mobile users and converting 3gp to flv. I’m getting flv files that are slowwwwed down…not sure why. Both audio and video are synched but a bit slow. Any ideas? Not sure if it’s on the input side or the output side as I haven’t tried any input type aside from 3gp. Files from Samsung, Nokia, and Motorola are all having the same slow output effect, though. Thanks for posting this.
June 6th, 2007 at 8:17 am
Great tutorial. Thanks. I tried to change the php file to convert mp3 to flv, cause that’s basically what I need ffmpeg for. Can anyone help?
Thanks
June 7th, 2007 at 11:33 am
[…] it out : http://vexxhost.com/blog/2007/05/20/how-to-convertencode-files-to-flv-using-ffmpeg-php/ ffmpeg» flash» flv» how to» php» tech» […]
June 21st, 2007 at 10:31 am
Or better yet….
function makeMultipleTwo($value) {
return $value - ($value % 2);
}
June 27th, 2007 at 11:26 pm
Cool
June 28th, 2007 at 1:43 am
Where’s the ffmpeg_movie object class?
July 1st, 2007 at 2:44 pm
The source code, discussion group and more is at http://ffmpeg-php.sourceforge.net/
Best Regards
Claudio Heidel
http://www.heidel.com.ar
July 10th, 2007 at 10:04 am
hello, who can show me the url of ffmpeg binary for linux server?
I want to upload it to my web site in
http://www.AmazeBrowser.com/flvdownload
July 17th, 2007 at 10:23 pm
I got the error
Fatal error: Class ‘ffmpeg_movie’ not found in /home/xxxxxx/public_html/flv/index.php on line 16
Whatz wrong ? Plz help
July 19th, 2007 at 5:19 am
Hi, Im currently developing a site that has this functionality. Im’ not so familiar with the commands so I used your sample above in converting videos to flv. The videos are alright, they does play in our player. But I’m having problem with the metadata. The player’s progress doesnt move when playing and it doesnt display the length of video which is taken from the video’s metadata. please help me with this.
August 1st, 2007 at 5:16 pm
I know I wrote it, however http://www.phpclasses.org/browse/package/3747.html provides an easy way to use ffmpeg to convert and extract video/images through a php5 class.
August 21st, 2007 at 6:49 am
How to convert a Vidio to FLV in PHP
August 29th, 2007 at 5:31 am
You should say Flvtool2 REQUIRES Ruby!
October 5th, 2007 at 9:20 am
[…] utiliza el formato de video de flash *.flv para mostrar los videos en la web, este artículo te indica cómo convertir paso a paso archivos de video a *.flv, por medio de un script en PHP que […]
October 7th, 2007 at 6:59 pm
great!!!
November 13th, 2007 at 1:08 pm
Its Great!
But I want all PHP scripts or Classes
That make me easy to use
November 13th, 2007 at 1:10 pm
I need Complete Documentation
November 21st, 2007 at 8:43 pm
Fatal error: Class ‘ffmpeg_movie’ not found
what happened !??
December 18th, 2007 at 2:39 am
I am trying to convert other video format to flv.. ffmpeg generating flv file but file does not play in flv player.. Can anybody know possible cause???
Above i found a good php class for conversion.. Thanks buggedcom for sharing this class..
January 19th, 2008 at 10:50 pm
[…] Pada blog Vexxblog juga terdapat sebuah artikel yang membahas pembuatan skrip PHP yang dapat mengubah video menjadi format FLV (artikel) […]
January 27th, 2008 at 7:01 pm
I get this error
Fatal error: Cannot instantiate non-existent class: ffmpeg_movie in /path/to/my/website.com/convert.php on line 8
i reviewed the code and it never created the class ffmpeg_movie
can someone help me fix this?
February 19th, 2008 at 7:45 am
para los que no les ande este ejemplo de aca prueben asi:
$srcFile = dirname(__FILE__) . “/tests/test_media/test.avi”;
$destFile = dirname(__FILE__) . “/tests/test_media/test.flv”;
$ffmpegPath = “/usr/local/bin/ffmpeg”;
$flvtool2Path = “/usr/bin/flvtool2″;
// load extension
$extension = “ffmpeg”;
$extension_soname = $extension . “.” . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . “/” . $extension_soname;
if (!extension_loaded($extension)) {
dl($extension_soname) or die(”Can’t load extension $extension_fullname\n”);
}
March 7th, 2008 at 11:54 pm
Fatal error: Class ‘ffmpeg_movie’ not found !!!
Am getting this error???
Anybody here to help me!!!
March 13th, 2008 at 12:11 am
I have a binary format php file i want to see the code to editable php format? how can i do this
March 17th, 2008 at 12:02 am
Hi,
But i couldn’t convert the files of size >1MB…
Same code as above…
NOt a little change… HOsting in dreamhost..
Any help from here…
March 27th, 2008 at 3:52 am
Any one know how record a vedio from a camera using ffmpeg-php,
Please helps
March 31st, 2008 at 7:11 am
and how i can reproduce the .flv using mplayer? have an example script?
April 7th, 2008 at 6:25 am
Hi guys.
Quick Question. Will this work without FLVTool 2. I am trying to follow your tutorial by taking out the FLV tool bits and no joy thus far. Can anyone help me. I have FFMPEG installed on my server but as of yet cannot get it to work.
I changed
$vid = exec($ffmpegPath . ” -i ” . $srcFile . ” -ar ” . $srcAR . ” -ab ” . $srcAB . ” -f mp3 -s ” . $srcWidth . “x” . $srcHeight . ” ” . $destFile);
if (!$vid)
{
echo “Failed”;
}
and am unsure if I am doing it right. Any help would be great.
April 7th, 2008 at 6:27 am
Also is the ffmpeg path pointing to the ffmpeg.php file or to the extenstion.
April 9th, 2008 at 9:56 am
This tutorial is great. I got what i want thanks for such a good tutorial …
April 12th, 2008 at 8:36 pm
hi…how about if there is space at filename instead?!
ex: my video.avi
thanks.
April 17th, 2008 at 7:32 am
Thanks for the tutorial! But … How come my output file is 0 kb? Any idea? Thanks!
May 3rd, 2008 at 7:46 am
I used this code for conversion but I found error at line 9
Please help on this error ”
Fatal error: Class ‘ffmpeg_movie’ not found in /home/imran/public_html/convert.php on line 9″
May 9th, 2008 at 5:17 am
it doesn’t work at all the code
May 10th, 2008 at 12:49 am
I have my site on a shared host,and i dont belive that i will be abale to install ffmpeg on my server.
How to overcome this..
May 23rd, 2008 at 1:30 am
[…] How to convert/encode files to FLV using FFMPEG & PHP (tags: php) […]
May 23rd, 2008 at 3:10 am
Hello Friends,
I am using ffmpeg to create thumbnail, and it works great … BUT THE ISSUE is with converting the avi file to flv… it create the .flv file but it has null content …
Please help me I have stuck here form last 2 days … Any help will be greatly appreciated …
Thanks in advance ,
Justin
May 24th, 2008 at 1:58 pm
[…] : vexxblog Blog Archive How to convert/encode files to FLV using FFMPEG & PHP : vexxblog Blog Archive Installing FFMPEG – The easy way! […]
May 25th, 2008 at 12:17 am
where is class script ?
I got this error
Fatal error: Cannot instantiate non-existent class: ffmpeg_movie in /home/tijua1/public_html/flash_upload.php on line 11
June 12th, 2008 at 7:30 pm
Can any one whats wrong with my code, it is not showing errors, but dont work
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);
}
}
?>
June 12th, 2008 at 7:31 pm
Sorry, how to target the path for ffmpeg and flvtool2 (an example would be good)
July 22nd, 2008 at 11:48 am
[…] the web there’s tons of materials on how to use this software. Eg. at vexxblog (How to convert/encode files to FLV and how to install ffmpeg […]
July 29th, 2008 at 10:54 am
Fatal error: Class ‘ffmpeg_movie’ not found !!
Me too
July 29th, 2008 at 10:55 am
did I miss that post?
July 30th, 2008 at 6:08 am
If you get the Error “Fatal error: Class ‘ffmpeg_movie’ not found” you can try the following:
The thing is that you have to load ffmpeg-php as an extension. On the top of your code add.
$extension_soname = “ffmpeg.so”;
//try the full path if it won’t work that way
if(!extension_loaded(”ffmpeg”)) {
dl($extension_soname) or error(”Can’t load extension
$extension_fullname”,true);
}
I hope this will help you.
Michael
August 28th, 2008 at 4:29 am
Why you do not try to use free FFlib.NET? See http://www.intuitive.sk/fflib/ :o)