ffmpeg sample code

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’);
|
|
|
|
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!


August 5th, 2007 at 12:42 pm
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
December 30th, 2007 at 1:43 am
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
February 7th, 2008 at 12:24 pm
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:34 pm
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 2:36 pm
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:43 pm
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 23rd, 2008 at 12:06 am
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 9:09 am
These links may help you:
http://pcwin.com/software/Html_To_Jpeg/index-1.htm
http://www.guangmingsoft.net/htmlsnapshot/html2image.htm
http://www.filetransit.com/files.php?name=Html_To_Jpeg
http://www.amazon.com/gp/browse.html?node=236156011
March 15th, 2008 at 4:56 pm
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 5:10 pm
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:23 pm
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 6:15 pm
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 7:26 pm
Sajith,
Undefined method ‘timestamp’ for nil:Nillclass
and alist of errors…
help me..
March 15th, 2008 at 7:30 pm
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 25th, 2008 at 11:36 am
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:44 am
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:49 am
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:50 am
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:57 am
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 12:06 pm
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:17 pm
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:20 pm
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 26th, 2008 at 7:53 am
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 8:56 am
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 9:01 am
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:08 am
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 1:03 pm
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:15 pm
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 27th, 2008 at 8:08 am
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 10:18 am
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 29th, 2008 at 8:48 am
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 9:59 am
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 31st, 2008 at 2:50 pm
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
April 2nd, 2008 at 1:12 pm
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:24 pm
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:33 pm
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:41 pm
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.
April 6th, 2008 at 3:39 pm
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 7th, 2008 at 1:00 pm
I have no idea about PDF to SWF (FLASH) Conversion. Sorry naveen
July 23rd, 2008 at 7:43 pm
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 25th, 2008 at 1:18 pm
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’);
August 12th, 2008 at 2:05 pm
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 21st, 2008 at 4:49 pm
how to keep play,stop,pause buttons for swf file.
please help me
August 21st, 2008 at 7:30 pm
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:49 pm
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 28th, 2008 at 1:51 pm
Why you do not use fflib.net? Try http://www.intuitive.sk/fflib/