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

  • Pingback: developercast.com » Vexxblog: How to convert/encode files to FLV using FFMPEG & PHP

  • Pingback: How to convert/encode files to FLV using FFMPEG & PHP « YouTube Clone

  • Pingback: Codificar o pasar video a .flv desde php

  • http://dtracorp.com dave

    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?

  • nil

    // Make multiples function
    function makeMultipleTwo ($value)
    {
    return ($value % 2) ? $value – 1 : $value;
    }

  • Aaron

    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.

    • MRK

      Hi Aaron,
      Can you help me plz. In my script flv files converts but file size 0 kbs. why is that. If you can give some advices I really appreciate

  • mediaman

    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

  • Pingback: hi my name is sid » How to convert/encode files to FLV using FFMPEG & PHP

  • Josh

    Or better yet….

    function makeMultipleTwo($value) {
    return $value – ($value % 2);
    }

  • Asinox

    Cool :D

  • Mike

    Where’s the ffmpeg_movie object class?

  • http://www.heidel.es Claudio Heidel

    The source code, discussion group and more is at http://ffmpeg-php.sourceforge.net/

    Best Regards
    Claudio Heidel
    http://www.heidel.com.ar

  • http://www.amazebrowser.com peter jack

    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

  • http://www.rapidsurfing.net/ Free Rapidshare Premium Accounts

    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

    • http://www.cyberls.com Bibin

      please confirm ffmpeg is installed in Your server

  • Carol

    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.

    • partha

      Class 'ffmpeg_movie' not found

  • buggedcom

    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.

  • Rahul

    How to convert a Vidio to FLV in PHP

  • Stephan

    You should say Flvtool2 REQUIRES Ruby!

  • Pingback: Neocortex » Blog Archive » Crea un clon de Youtube con PHP

  • perechubb

    great!!!

  • Adeel

    Its Great!
    But I want all PHP scripts or Classes
    That make me easy to use

  • Adeel

    I need Complete Documentation

  • http://sciencenews.dixast.com/ waqas

    Fatal error: Class ‘ffmpeg_movie’ not found
    what happened !??

  • http://www.bala-krishna.com Bala Krishna

    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..

  • Pingback: jazar.log » Blog Archive » jazar.log

  • nf14

    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?

  • http://www.delparque-dyd.com.ar vosorio

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

  • KiduMon

    Fatal error: Class ‘ffmpeg_movie’ not found !!!

    Am getting this error???

    Anybody here to help me!!!

  • http://na saigopal

    I have a binary format php file i want to see the code to editable php format? how can i do this

  • KiduMon

    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…

  • http://no shihab

    Any one know how record a vedio from a camera using ffmpeg-php,
    Please helps

  • Dan

    and how i can reproduce the .flv using mplayer? have an example script?

  • Stev

    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.

  • Stev

    Also is the ffmpeg path pointing to the ffmpeg.php file or to the extenstion.

  • http://www.globalprompt.net Justin

    This tutorial is great. I got what i want thanks for such a good tutorial …

  • aris

    hi…how about if there is space at filename instead?!
    ex: my video.avi

    thanks.

  • mike

    Thanks for the tutorial! But … How come my output file is 0 kb? Any idea? Thanks!

  • http://imranijaz.net Imran

    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″

  • moff

    it doesn’t work at all the code

  • Rupin Chheda

    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..

  • Pingback: Skylog » Blog Archive » links for 2008-05-23

  • http://www.globalprompt.net it oursourcing

    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

  • Pingback: ÊÍæíá ÇáÝíÏíæ ãä avi Çáí flv - ÓæÇáÝ ÓæÝÊ

  • http://www.globalprompt.net it oursourcing

    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

  • rush

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

  • rush

    Sorry, how to target the path for ffmpeg and flvtool2 (an example would be good)

  • Pingback: Exploration of an Inegrated Media Tool | Networked Learner News

  • http://www.chelseawebworks.com Glory Rose

    Fatal error: Class ‘ffmpeg_movie’ not found !!
    Me too

  • http://www.chelseawebworks.com Glory Rose

    did I miss that post?

  • Michael

    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