How to convert/encode files to FLV using FFMPEG & PHP

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

  • http://www.intuitive.sk izero

    Why you do not try to use free FFlib.NET? See http://www.intuitive.sk/fflib/ :o)

  • http://www.marbellasunrentals.com Helen

    I had many problems with this script, it only converted some files. And Todd Kirby told me to replace the first . $destFile . in exec with ‘-‘, which I did and now the scripts works perfect.

  • http://arronlimcewi.blogspot.com/ Alli

    $flvtool2Path = “/path/to/flvtool2″;

    i have already installed ffmpeg. but i didn’t know flvtool2. what is for?

    thanks

  • Pingback: 247-ocs.com » How does e-mail work on a shared hosting account ?

  • list

    hii,
    Is it necessary to install “flvtool2″ to work along with ffmpeg ?

    Thanks in advance

  • http://www.visitvortex.com nishad aliyar

    I didn’t see “flvtool2″ in my server.Shall I report this to the server providers

  • Aijaz

    Please help.

    I am using this code

    $src = “test.avi”;
    $dest = “movies/test.flv”;

    echo “src: $src
    dest: $dest
    “;
    $command = escapeshellcmd(“ffmpeg -i $src $dest”);

    $output = shell_exec($command);
    echo $output;

    but unable to make flv.

    its 0 byte in length.

    pls help for any server issue etc.

    m working on my linux server.

    Thanks a lot in advance

  • Pingback: FFMPEG How to Install. - all things L337

  • http://www.mysoogal.com mysoogal

    this works for me good ubuntu 8.10

    ffmpeg
    mplayer
    mencoder
    php-ffmpeg
    Lamp package / mysql / phpmyadmin

    here is code to convert to flv

    —————————————

    <?php
    convertToFlv( “video.avi”, “video.flv” );

    function convertToFlv( $input, $output ) {
    echo “Converting $input to $output”;
    $command = “ffmpeg -i $input -s 320×240 -ar 44100 -r 12 $output”;
    echo “$command”;
    shell_exec( $command );
    echo “Converted”;
    }
    ?>

    ————————————-

  • http://ilearntube.net jorjdaniel

    I have the same problem , lenght of output file is 0 kb :(. Anybody can say why?

  • http://www.hdflvplayer.net FLV Player Download

    Great Article!!!!! Thank you for posted this……………

  • http://www.pronity.de mati

    how did you create this diagramm?

    thx, Mati

  • http://jorjdaniel.blogspot.com Jorj Daniel

    Please HELP US, I have the same problem as many people: The output file have 0kb. Why?

  • vexxhost

    If anyone needs help with this, they should post on our forums at http://forums.vexxhost.com and we can help you!

  • Pingback: Using FFMpeg to create YouTube clone script – Money-Code

  • newbie boy

    tried this one…

    it converts the video file but with 0kb size…

    how come?

  • newbie boy

    and what’s this for?

    $srcFPS = $ffmpegObj->getFrameRate();

  • http://www.mihitha.com MRK

    What is the path to ffmpeg and flvtool2

    • vexxhost

      This changes and depends on your server, you have to contact your service provider :)

  • http://www.mihitha.com MRK

    Output file 0 kb. why?? Help

    • vexxhost

      I suggest checking the output of your system call to see why it's doing that (or run the command manually)

  • http://www.hitasoft.com Ripe FLV Player

    Is there any application to draw picture like this?

    • vexxhost

      This was just done using Photoshop

  • http://onlyclassy.com Fathi

    the solution :
    -1 install ffmpeg
    -2 install flvtool2
    -3 install the ffmpeg-php = will be used as an extension
    -4 import ffmpeg and flvtool2
    $ffmpegPath = “/path/to/ffmpeg/ffmpeg”;// full path
    $flvtool2Path = “/path/to/flvtool2″; // full path
    $ffmpegObj = new ffmpeg_movie($srcFile);
    exec($ffmpegPath.” -i “.$srcFile.” “.$destFile);

    That is it

  • abdulla

    I have using this code installtion also successfully of ffmpeg and flvtool2. but i dont know after the flv file is ) kb. anyone knows help me..???

    • http://deranjer.is-a-geek.com deranjer

      Some of this is old, but if you are having the 0kb problem, IF POSSIBLE, run the command in a SSH session (you may not have that option.) A lot of times if ffmpeg or flvtool2 is having problems, the output file will show 0k, but you won't get specific errors displayed in the webpage. If you run it from the shell you can see the various errors that pop up. I learned that after an hour of fiddling and getting 0kb files that I did not have lame installed which caused it to fail.
      Right now, I have the following problem with flvtool2, haven't figured it out yet.
      ERROR: /usr/lib/ruby/1.8/flv/stream.rb:274:in `duration'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:181:in `add_meta_data_tag'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:137:in `update'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:47:in `send'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:47:in `execute!'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:46:in `each'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:46:in `execute!'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:206:in `process_files'
      ERROR: /usr/lib/ruby/1.8/flvtool2/base.rb:44:in `execute!'
      ERROR: /usr/lib/ruby/1.8/flvtool2.rb:168:in `execute!'
      ERROR: /usr/lib/ruby/1.8/flvtool2.rb:228
      ERROR: /usr/bin/flvtool2:2:in `require'
      ERROR: /usr/bin/flvtool2:2

      If I run flvtool2 seperate from ffmpeg, it works correctly, but when coupled with ffmpeg, the errors occur. If anyone has the solution, please post!

  • sachin

    Fatal error: Class 'ffmpeg_movie' not found in C:wampwwwprojectsffmpegffmpeg.php on line 9

  • http://www.cuscoperuvian.com cusco

    Great tutorial. Thanks

  • mahin

    hei pls help me for video to fla conversion

  • http://www.trazi-torrent.iz.rs triva911

    What is flvtool2Path ???

    • vexxhost

      It's the path that is to the flvtool2 executable installed on your server

  • http://www.trazi-torrent.iz.rs triva911

    I have this error plz help

    Fatal error: Class 'ffmpeg_movie' not found in /data/multiserv/users/383774/projects/823634/www/convert.php on line 8

    • vexxhost

      Your hosting does not have FFMPEG-PHP module enabled, it has to be compiled.

  • http://www.gsid.net Kelso

    Is this article still current? If not does someone have a link to a more current one?

    Thanks

  • http://www.rwp-pk.com Ahmad Ali

    This is good but how can i check that FFMPEG is available on my hosting server. Please let me know

  • ravi

    Hello but where is the class ffmpeg_movie…..?

  • mrk

    what is path to ffmpeg. Can someone please give me script to get screenshot from video file.

  • http://www.easygiga.com edz

    Excellent! The post is perfect and the staff behind it are very generous. I will surely ask for your help if I have some problem debugging my program. Thanks in advance.

  • epixeltechnologies

    Hello,

    I am developing video website and i am using php script to convert .avi,. mpg file to .flv file. The problem is i can not convert the video file more then 10MB. Please Help me. Its really urgent.

    Thanks,

  • Joe Smith

    I have got it so it converts the file. But I think there is an error, as my makeMultipleTwo was giving out an error i used fixed width and height.
    When I view the file in my FTP (file zilla) it states that the file size is 0 and when i download it and try to play it it does not work any suggestions?

    $srcWidth = "320";
    $srcHeight = "240";
    $srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
    $srcAR = $ffmpegObj->getAudioSampleRate();
    $srcFPS = $ffmpegObj->getFrameRate();

    // Call our convert using exec()

    exec($ffmpegPath . " -i " . $srcFile . " -ar " . $srcAR . " -ab " . $srcAB . " -f flv -s " . $srcWidth . "x" . $srcHeight . " " . $destFile . " | " . $flvtool2Path . " -U stdin " . $destFile);

    Thanks:)

  • http://www.drm-removal-tool.com/ drm removal

    Where's the ffmpeg_movie object class?

  • http://registrodelapropiedad.info registro propiedad

    This is good but how can i check that FFMPEG is available on my hosting server. Please let me know

  • Thanuja Gunasekara

    this script is not not working. what is flvtool2 pls describe.

  • techndu

    its not working please help me