Jun 12 2007

ffmpeg sample code

ffmpeg.jpg

Install ffmpeg library in your server first.

Place a video file of any type on current directory (or anywhere), here it is clock.avi .

Click more for source code:

extension_loaded('ffmpeg') or die('Error in loading ffmpeg');

$ffmpegInstance = new ffmpeg_movie('clock.avi');
echo "getDuration: " . $ffmpegInstance->getDuration() .
"getFrameCount: " . $ffmpegInstance->getFrameCount() .
"getFrameRate: " . $ffmpegInstance->getFrameRate() .
"getFilename: " . $ffmpegInstance->getFilename() .
"getComment: " . $ffmpegInstance->getComment() .
"getTitle: " . $ffmpegInstance->getTitle() .
"getAuthor: " . $ffmpegInstance->getAuthor() .
"getCopyright: " . $ffmpegInstance->getCopyright() .
"getArtist: " . $ffmpegInstance->getArtist() .
"getGenre: " . $ffmpegInstance->getGenre() .
"getTrackNumber: " . $ffmpegInstance->getTrackNumber() .
"getYear: " . $ffmpegInstance->getYear() .
"getFrameHeight: " . $ffmpegInstance->getFrameHeight() .
"getFrameWidth: " . $ffmpegInstance->getFrameWidth() .
"getPixelFormat: " . $ffmpegInstance->getPixelFormat() .
"getBitRate: " . $ffmpegInstance->getBitRate() .
"getVideoBitRate: " . $ffmpegInstance->getVideoBitRate() .
"getAudioBitRate: " . $ffmpegInstance->getAudioBitRate() .
"getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate() .
"getVideoCodec: " . $ffmpegInstance->getVideoCodec() .
"getAudioCodec: " . $ffmpegInstance->getAudioCodec() .
"getAudioChannels: " . $ffmpegInstance->getAudioChannels() .
"hasAudio: " . $ffmpegInstance->hasAudio();

//___________________________________Code end here_____________________________________

I also can be used for file format conversion

Eg: exec(’ffmpeg -i ‘.$SourcePath.’ ‘.$Destination);

Here $SourcePath any file of any format , and destionation also another format.

eg: exec(’ffmpeg -i saji.3gp saji.flv’);

53 Comments on this post

Trackbacks

  1. Rottenberg said:

    Hi,
    I’m seaching how to transcode various videos type to .flv.

    Do you know where can I find informations about all parameters to use in the exec (ffmpeg…)

    thank your for your help

    best regards

    Michel

    August 5th, 2007 at 12:42 pm
  2. Adrian said:

    Does anyone know the command to implement the water mark feature. I’ve installed eveything and it works fine i just need to know how to use the water mark feature.

    thanks

    December 30th, 2007 at 1:43 am
  3. naveen said:

    friend, i am using a pre compiled ffmpeg in my shared server(RHEL) and the output flv that i got has no audio(libmp3lame.so is missing).
    Since i dont have shell access to the server i asked our hosting team to install it for us.The installation path looks like usr/local/bin .. and /lib for libraries..all the required files are there but i am not able to execute ffmpeg within my php program.
    can u please suggest a solution for this.

    February 7th, 2008 at 12:24 pm
  4. Sajith M.R said:

    The installation of ffmpeg is pretty crap. You need to compile the ffmpeg with libmp3lame enabled. Since you didnt have root access, its more difficult, and your hosting solution provider may not aware of this installation procedure. So instead of ffmpeg, you can use mencoder. Tell them to install mencoder in your server. The usage of mencoder can be found from this blog itself. http://www.sajithmr.com/mencoder-video-convertion-tool/

    February 7th, 2008 at 12:34 pm
  5. naveen said:

    Hi
    Thanks for ur reply.
    Can we get a ffmpeg exectable binary which also contain libmp3lame.Which means it will b a single exectable library.

    February 7th, 2008 at 2:36 pm
  6. Sajith M.R said:

    Hello Naveen

    You can get ffmpeg executables for windows. But for linux machine you need to install it after proper compilation. Since it is utter configurable and flexible, you need to MAKE it for your own use. FFMPEG Sound problem is a big problem in various linux os. Download full ffmpeg from source forge and compile it by enabling all the options. Since you did nt have root access, it is depending with your provider. More over application like ffmpeg , mencoder etc, root accessed server is a must !!!

    February 7th, 2008 at 2:43 pm
  7. naveen said:

    hello sajith,
    do u know how to create an html thumbnail view , provided an url as input in php or javascript.Similar to a screen shot .
    We are trying it on a linux server.

    February 23rd, 2008 at 12:06 am
  8. Sajith M.R said:
  9. KiduMon said:

    Hello Sajith,

    While uploading my small files are convert into flv with out any issue..

    But with big files (>1MB) is not converting.. please help me..

    This is my code…

    $srcWidth = “320″;
    $srcHeight = “240″;
    $srcAB = “32″;
    $srcAR = “22050″;
    exec($ffmpegPath . ” -i ” . $srcFile . ” -ar ” . $srcAR . ” -ab ” . $srcAB . ” -f flv -s ” . $srcWidth . “x” . $srcHeight . ” ” . $destFile . ” | ” . $flvtool2Path . ” -U stdin ” . $destFile);

    Thanks in advance

    March 15th, 2008 at 4:56 pm
  10. Sajith M.R said:

    I dont think size of file is a problem for ffmpeg. Instead checking through your web browser with exec command, go to shell / command prompt and check using ffmpeg function call directly.

    I think your problem is web server’s file uploading, see your webserver’s uploading file size limit in configuration . Do unit testing , means test one by one, first convertion on multiple type/size files using ffmpeg command directly, and if everything works fine, test with php library. OKay

    (you can use mencoder also for video convertion)
    http://www.sajithmr.com/mencoder-video-convertion-tool/

    Thanks
    Sajith

    March 15th, 2008 at 5:10 pm
  11. KiduMon said:

    Thanks sajith,

    I tried it all that way.. I changed webserver’s uploading file size limit to 10M

    1. Files 1MB moves to the desired folder but when it comes to the converted folder size seems to 0kb

    That means it’s not get converted , am i right?

    Please help me!!!

    March 15th, 2008 at 5:23 pm
  12. Sajith M.R said:

    You can see the error message while you apply the command ffmpeg direclty from command prompt. see the error log or messages arrive when you try to convert big file.
    I repeat , never try it through php or apache , try directly from command prompt to see what actually happening behind. if there is a convertion, it shows result like input:thisfile.3gp output: thatfile.flv etc etc audio: video: etc. Okay

    Thanks
    Sajith

    March 15th, 2008 at 6:15 pm
  13. KiduMon said:

    Sajith,

    Undefined method ‘timestamp’ for nil:Nillclass
    and alist of errors…

    help me..

    March 15th, 2008 at 7:26 pm
  14. Sajith M.R said:

    Sorry Chanchel, I am not familiar with this error :( And when i googled it , nothing listed. Try some other files , i dont think it’s compilation error of ffmpeg, or try mencoder.

    March 15th, 2008 at 7:30 pm
  15. Jonathan said:

    Hi Mr Sajith,

    Im wondering if you can point out much clearer on how to convert videos using ffmpeg through php script. Ive been trying to do this for a week and still no progress. I tried:

    exec(”ffmpeg -i ‘inputFile.avi’ ‘outputFile.flv’”);

    but to no effect. Is there a problem with exec() function in PHP in converting this videos? I tried using your tutorial and I managed to run it properly but converting them is really painful. :(

    Need some help.

    March 25th, 2008 at 11:36 am
  16. Sajith M.R said:

    Hi Jonathan,
    ffmpeg is not a php function or script to convert video files. It is an application. Before you convert files from php, you have to check the ffmpeg binary working from your command prompt(windows) or shell (linux) , by typing

    ffmpeg -i ‘inputFile.avi’ ‘outputFile.flv

    For its 100% working , you have to install ffmpeg properly with libamr, lame etc support for all file format including 3gp (if you want). If it works fine in shell, your ffmpeg is okay. Exec is only a php function for executing shell commands. So if the former settings are okay, the rest will work without any error.

    March 25th, 2008 at 11:44 am
  17. Jonathan said:

    I have tried working with ffmpeg on a shell client, command prompt to be more specific and it is working fine on a command line. What I need to do is to access ffmpeg through php script. I have a webserver which records a video and I need to convert those videos to other formats.

    Thanks.

    March 25th, 2008 at 11:49 am
  18. Sajith M.R said:

    And when i tried ffmpeg in windows, everything worked very well. But when i tried in linux everything ok except the audio for flv output. When we convert some files into flv , there was no audio output. Since our server for mobshare was linux, i installed all the libraries for sound (mp3lame, amrlib etc).

    On that time, i got mencoder, another video converting tool in linux (Backend of mplayer), and it worked very well with all format. So in our site http://www.mobshare.in , we are using mencoder for video conversion . See my post on mencoder from
    http://www.sajithmr.com/mencoder-video-convertion-tool/

    March 25th, 2008 at 11:50 am
  19. Sajith M.R said:

    Hi Jonathan,
    Then you are using windows os in your php server. Then you have to do some more security thing with apache configuration. You have to change the safe_mode option in httpd.conf file for executing exec commmand from php script. If there is any antivirus etc running , these all affect the execution of exec command.

    When safe mode is enabled, you can only execute executables within the safe_mode_exec_dir

    See this: http://in.php.net/function.exec

    And do the needful for windows security configuration

    Regards
    Sajith

    March 25th, 2008 at 11:57 am
  20. Jonathan said:

    Sorry If I havent informed the situation very well. The webserver is running on Debian Linux. I used red5 server to accomplish the Record and Audio Capture things. Now what I need to do is to convert the files recorded to other formats using ffmpeg through php classes/scripts.

    I have checked the phpinfo(); of the site and the safe mode is turned to off. I am wondering what I could be missing in here. Is my exec(); line command enough or is there something I might have not include? Shall I use flvtool2 too? Like I have seen others have done after converting a file? if so, how would it be?

    March 25th, 2008 at 12:06 pm
  21. Sajith M.R said:

    If the ffmpeg is working well through shell, your ffmpeg installation and configuration is okay. Now from your comment, i got that, you could not initiate or execute same thing via php code. And your exec is not working as you expected, right ?

    try $result = exec(”whoami”); and echo the $result

    When you run exec from php, the user of linux will be apache (not all time, commonly apache) and when you try this using command prompt, the user will be root or some other else. Here also the permission problem.

    anyway echo the result of exec, sothat you can see what is the error. place the files to be converted under apache’s web root and give needful permissions. (try with 777)

    Some simple debugging can solve your problem. Please be patient and check apache’s error log after call exec function. if error log is disabled, enable it from httpd.conf and point a file to show error log.

    In another terminal , open that error file using tale command ( http://www.sajithmr.com/tail/ ) so that you can see the live error reporting.

    From these , i am damn sure, you can solve your exec ffmpeg issue. Be confident and go on :)

    Regards
    Sajith

    March 25th, 2008 at 12:17 pm
  22. Jonathan said:

    Thank you and I will try what you have advised. I will inform you the progress as I go along and thanks for the help. Also, the cheering helps. :)

    Jonathan

    March 25th, 2008 at 12:20 pm
  23. Jonathan said:

    Hi mr Sajith,
    I tried executing ffmpeg via SSH client. When i type ‘ffmpeg’ an error occurs which says:

    “ffmpeg: error while loading shared libraries: libvorbisenc.so.2: cannot open shared object file: No such file or directory”.

    Does it mean I didnt compile libvorbis or other stuffs the way it should be? If so.. What should I do so that ffmpeg can detect it?

    Thanks in advance.
    Jonathan

    March 26th, 2008 at 7:53 am
  24. Sajith M.R said:

    At last you found your ffmpeg is not working properly from ssh. This is what i am telling from the beginning. This is called atomic testing. Test one by one before testing the whole system. So make sure to compile ffmpeg by enabling all the libraries.

    The compilation process is not that much simple. You need to compile it very carefully.

    March 26th, 2008 at 8:56 am
  25. Jonathan said:

    Do I have to do the ‘./configure && make && make install’ to each and everyone again? FFmpeg seems to be trying to open a file from the shared libraries which is called libvorbisenc.so.2. I dont know where to start, Im thinking what folder is the shared libraries and if I can just put the file which ffmpeg is searching for?

    March 26th, 2008 at 9:01 am
  26. Sajith M.R said:

    a single line can enable all the libraries … like
    –enable mp3laame -enable lib etc

    See this link: http://groups.google.com/group/ffmpeg-php/web/installing-ffmpeg-and-ffmpeg-php-on-centos

    It might be a useful one for you.

    Use google properly for perfect installation. when ever an error comes, paste the exact sentence on google. it will lead you to the exact page.

    Regards
    Sythoos

    March 26th, 2008 at 9:08 am
  27. Jonathan said:

    Hi,

    I used this command on my shell ldd ‘which ffmpeg’ and it gives me this output:

    linux-gate.so.1 => (0xffffe000)
    libavdevice.so.52 => /usr/lib/libavdevice.so.52 (0×4001d000)
    libavformat.so.52 => /usr/lib/libavformat.so.52 (0×40024000)
    libavcodec.so.51 => /usr/lib/libavcodec.so.51 (0×400bd000)
    libavutil.so.49 => /usr/lib/libavutil.so.49 (0×4054d000)
    libm.so.6 => /lib/libm.so.6 (0×4055c000)
    libc.so.6 => /lib/libc.so.6 (0×40581000)
    libz.so.1 => /usr/lib/libz.so.1 (0×4069c000)
    libdl.so.2 => /lib/libdl.so.2 (0×406b0000)
    libmp3lame.so.0 => /usr/lib/libmp3lame.so.0 (0×406b4000)
    /lib/ld-linux.so.2 (0×40000000)
    libvorbisenc.so.2 => not found
    libvorbis.so.0 => not found

    It seems that the last two files were missing, and I just want to ask/confirm that if I just get this files and just put it inside the ‘/usr/lib’ folder, does it have any possibility of solving the problem? I tried recompiling the libvorbis but to no effect. Thanks

    March 26th, 2008 at 1:03 pm
  28. Sajith M.R said:

    I dont know linux libraries in deep. In my case i used ubuntu. All were in a simple step using apt-get install ffmpeg … So it is working fine there.

    http://wiki.kartbuilding.net/index.php/Ffmpeg

    The manual configuration sometimes become very crap. And in my radhat server i used mencoder for video conversion.

    Once the ffmpeg installed well, i can help you in php part. But all the minimum thing needed is, set the ffmpeg RUN

    March 26th, 2008 at 1:15 pm
  29. Jonathan said:

    Hi,
    Finally ffmpeg run through shell command and now I can execute it via php script. Im just thinking and maybe you can help me out. Here is the situation:

    The thing is I am recording a video via red5 server and it automatically saves the video to a .flv file. I converted a lot of .flv video via ffmpeg but the video recorded through the server is different. Along with the flv file it has its .meta file.
    I dont know what it was but all I know if I have for example a ‘test.flv’ along with it is ‘test.flv.meta’ and when i try to convert it via ffmpeg it says unsuported format. Do you have any ideas what should be done or is it possible for ffmpeg to convert it?

    March 27th, 2008 at 8:08 am
  30. Sajith M.R said:

    Hi Jonathan,

    I have no experience with red server. From my bare knowledge , i guess the following things:

    If there is a meta file with .flv , it will be an xml format file or something to save the extra attribute of .flv file. It might be latest flash player’s settings. In new flash players (8) the meta file also load with the flv file and retrieve attributes from that meta and plays . The process is called flv meta injector.

    And ffmpeg conversion problem. Since ffmpeg is an opensource and its development is still on, latest flv version might not be supported. These all are my guess, not from real knowledge. Check your red server configuration for recording video in older flv format , or without meta injection or something.

    And for which website, you are doing these things ? If it is private, need not to mention. Just asked.

    Anyway
    Go on
    Sajith

    March 27th, 2008 at 10:18 am
  31. Jonathan said:

    Its me again, :)
    I just wanted to ask if it is possible to join two audio file in ffmpeg or mencoder.. I’ve successfully joined two video file through mencoder but I cant find a possible command to join two audio file. I tried something like:

    copy audio1.mp3 audio2.mp3 > final.mp3

    But it just create a file that has 1Kb. :( Is there a way to join audio files using this applications??

    March 29th, 2008 at 8:48 am
  32. VKS said:

    Hi Sajith,
    I have requrment to to record a video file from remote client machine’s webcam(from browser) to the server machine.Is it possible by using ffmpeg.Iam using PHP and apache server for my development.Is it possible by using ffmpeg.any idea.

    Thanks,
    VKS.

    March 29th, 2008 at 9:59 am
  33. VKS said:

    Hi Jonathan,
    One question, how u use the RED server from PHP.I think it is running in JAVA, is it possible to use from PHP?

    Thanks,
    VKS

    March 31st, 2008 at 2:50 pm
  34. Sajith M.R said:

    Hi Jonathan, i did nt try audio joining yet. :)
    And please clear the doubt of VKS in this thread regarding red server.

    Thanks
    Sajith

    April 2nd, 2008 at 1:12 pm
  35. Jonathan said:

    Hi VKS, regarding your question, I am running it via Macromedia Flash, Flash connects through red5 server that is how web recording is achieved. Anyway, I didnt use PHP in web recording, but if I would, I believe Flash would stand as a middleman between PHP and Red5. But I do not have enough knowledge as of now, Im still exploring it.
    And by the way, Sajith, I have achieved audio concatenation/joining through mencoder.
    But do you have any idea if mencoder can join two audio and play them together, like overlaying an audio to another audio? That they would play together at the very start of the track?

    April 2nd, 2008 at 1:24 pm
  36. Sajith M.R said:

    Joathan, from these thread i got the info that you are a very good programmer, and regarding this mencoder merging, might be you are the apt person to solve this audio merging b’cause i stopped all the mencoder works when my video to flv conversion done :)

    Anyway, was a good discussion with you .

    April 2nd, 2008 at 1:33 pm
  37. Jonathan said:

    I see.. Anyway I just want to let you know that I appreciate what you contributed and it really helped me from the very start. Thanks and this thread is awesome. :D

    April 2nd, 2008 at 1:41 pm
  38. naveen said:

    Hello sajith,

    do u know any free tools that can be used to convert pdf to swf .if u have done any flick magazine app before in flash and php please let me know the issues come under it.
    thanks

    April 6th, 2008 at 3:39 pm
  39. Sajith M.R said:

    I have no idea about PDF to SWF (FLASH) Conversion. Sorry naveen

    April 7th, 2008 at 1:00 pm
  40. ovidiu said:

    Holo
    I want to use the information from exec(’ffmpeg -i file1 file2′,$output,$return_var);
    If I run the ffmpeg command from shell it is OK a lot of information is dispayed after, echo return_var is 0 so it is OK but the $output is always empty in PHP.

    Any ideeas?

    ovidiu

    July 23rd, 2008 at 7:43 pm
  41. sarayu said:

    Hi Sajith,

    I converted video files into flv in cmd prompt successfully .But when i try to use to convert this in my php file by using exec command
    it wont be converted.can u plaese help me about this.Here is my code. The exec for “hello World” is executed successfully.But the 2nd
    exec command could not be executed.

    $in = “../FFmpeg/monkeydog.wmv”;
    $out = “../FFmpeg/mn.flv”;
    $ffmpegPath = “../FFmpeg/ffmpeg”;
    echo exec(’echo Hello World!’);
    exec(’$ffmpegPath -i $in -ar 22050 -ab 32 -f flv $out’);

    July 25th, 2008 at 1:18 pm
  42. Gyan said:

    Hi Sajith,

    i want to convert wmv file to flv but i am not success.
    please help to this conversion.
    ffmpeg is already installed on server

    please open the iduce.com/test.php

    from here you can download the file and check what is the mistake and you can also check my server inf at given bottom link

    thanks
    and regards

    August 12th, 2008 at 2:05 pm
  43. sridhar said:

    how to keep play,stop,pause buttons for swf file.
    please help me

    August 21st, 2008 at 4:49 pm
  44. Tamer Qtaish said:

    Hello Sajith!
    Don’t worry I am not going to ask you any questions, not yet…
    I just got started to read about ffmpeg, I am considering it as one of my strong options for a project I am doing.
    So while I was doing my search I stumbled on this form.
    I was both amazed and surprised by how you don’t only have the knowledge but also share it freely! I like went through all the question and answers… Lots of details and crystal clear answers.

    So to sum it up I just want to say “GREAT JOB”
    And thank you!

    All the best,
    Tamer Qtaish

    August 21st, 2008 at 7:30 pm
  45. Sajith M.R said:

    Thank you Tamer, Ideas and knowledge are always for sharing… it grows as you share it… don’t let anyone to re-invent the same wheel , so share :)

    August 21st, 2008 at 7:49 pm
  46. izero said:

    Why you do not use fflib.net? Try http://www.intuitive.sk/fflib/

    August 28th, 2008 at 1:51 pm
  47. Yedhumohan said:

    i have done flv conversion in command prompt.But once i tried with php script,conversion has not been done

    September 1st, 2008 at 2:50 pm
  48. rammy said:

    hi sajith i have doubt first i want to know spouse i host my website and ffmpeg not installed can i install or i have to contact webhosting service and how i can change mov,avi in flv with specific resolution

    September 19th, 2008 at 2:14 am
  49. rahman said:

    i am the newbie for ffmpeg,i want to know it is a script or application,where can i run this file in my local server(mypc)
    give basic idea,i am using windows,i need reply from u brother

    October 4th, 2008 at 10:46 pm
  50. s said:

    i hate linux and i hate those who built this stupid ffmpeg only for linux ! fuck you geek

    October 27th, 2008 at 7:24 pm
  51. jdj said:

    It seems as if you need to get a web host that has the ffmpeg library for php so that you can execute the ffmpeg commands with exec(). I found documentation of ffmpeg: http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html that helped out a lot. I assume that the exec() function requires parameters that can be found in the ffmpeg documentation such as:

    exec(ffmpeg -f oss -i /dev/dsp -f video4linux2 -i /dev/video0 /tmp/out.mpg)

    November 7th, 2008 at 6:41 am
  52. hans said:

    Question I am trying to solve is this. We have flash based property tour tool that takes photos of property listings and displays them in sequence. Is there a way to take the swf that is displayed typically in a browser window and capture 30 seconds of it and convert to mpg using ffmpeg? Thanks in advance.

    November 15th, 2008 at 12:45 am
  53. hans said:

    or please let me know if there is another way to accomplish this… thanks.

    November 15th, 2008 at 12:47 am

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Itslife Online
    Advt on sajithmr.com
    Advt on sajithmr.com
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts