ffmpeg sample code
Posted on 12. Jun, 2007 by Sajith M.R in Downloads, php source 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’);



jdj
Nov 7th, 2008
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)
hans
Nov 15th, 2008
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.
hans
Nov 15th, 2008
or please let me know if there is another way to accomplish this… thanks.
boja
Nov 19th, 2008
Hi,
first of all i would thank you every one, i have learn lot from this discussion.
i’m having the same problem exec(’ffmpeg ..’) is not working on my server. i have read all the previous comments & tried those actions
but the problem is still remaining. ffmpeg is working fine in my terminal & not working through my php code with exec(’ ‘)
i have check error_log file; it’s saying(also tried with whoami..)
sh: /whoami: No such file or directory
sh: /pwd: No such file or directory
sh: /ffmpeg: No such file or directory
the file path is correct & it has this file permissions(i hv tried with chmod -R 777 filename)
-rwxrwxrwx 1 apache
please help me to solve this!
my server details are:
Linux and OS is Fedora ,Apache/2.2.3 (Fedora),PHP Version 5.2.6
Rohit
Feb 26th, 2009
Hey
I have implemented the ffmpeg in the site, But it is not working well, Problem is the it takes too much time to upload the videos.can any one tells me how i can make it fast
vasu
Feb 26th, 2009
hi
how do i strip a certain section of a video(m3). say to post 5-10 sec audio to listen b4 some1 dwnloads it.. using ffmpeg
thanx
vasu
Sajith M.R
Feb 27th, 2009
@Rohit
To work the uploading fast, do a queuing machanism for converting videos. See the implementation in mobshare.in
Register there and upload a pic, it wont come that time, instead a processing symbol will be shown. The real image will be displayed after a little bit time of processing
Rohit
Feb 27th, 2009
Hey
Thanks for reply me. This is my site link http://www.iceageadultvideo.com.Please register here and upload any video. I am facing two problems in this site.One it takes too much time in uploading the videos and second in flash player i wanna fast speed flash player. I will be very thankful to you if u can help me in this
Rohit
Feb 27th, 2009
Hey
when i enter the above like this
extension_loaded(’ffmpeg’) or die(’Error in loading ffmpeg’);
$ffmpegInstance = new ffmpeg_movie(’http://iceageadultvideo.com/flvideo/102.flv’);
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();
in my site of one video. It gives me following description.please tell me how i can get excellent quality video from this which will buffer too much
getDuration: 14.89getFrameCount: 445getFrameRate: 29.9166660309getFilename: http://iceageadultvideo.com/flvideo/102.flvgetComment: getTitle: getAuthor: getCopyright: getArtist: getGenre: getTrackNumber: 0getYear: 0getFrameHeight: 1200getFrameWidth: 1600getPixelFormat: yuv420pgetBitRate: 64000getVideoBitRate: getAudioBitRate: 64000getAudioSampleRate: 22050getVideoCodec: flvgetAudioCodec: mp3getAudioChannels: 2hasAudio: 1
Rohit
Feb 27th, 2009
Hey
Any one tell me which is best bit rate in ffmpeg so videos will low in buffering in video player
PJaxon
Mar 9th, 2009
@ Rohit…. `best` is such a subjective term. Low quality bitrates are less than 300k, but then your video will get pixelated. If you use the -sameq switch in your ffmpeg command, then the quality will be as high as the original, but then it could stream slowly depending upon your server, the bandwidth coming out of it, the bandwidth of your users, and their machine spec’s. I like 500k bitrates, personally.
@Sajith: Thanks for the function list. Now I can make my encoding engine smarter.
Rohit
Mar 12th, 2009
Hey
Any one can tell me solution of pixlated video.Please see this link http://www.iceagevideo.com.In this video is pixlated. I am using ffmpeg to convert the video format to flv.I am also using -sameq in the ffmpeg command. Any one tell me solution then video will be clear
narayana
Mar 19th, 2009
hi.
thanks for your work. i am happy. I hope you will work better in coming days.
Narayana
kathmandu
Nepal
John
Apr 1st, 2009
Please help me
How can I join two flv files ?
raman
May 28th, 2009
iam looking for ffmpeg php windows dll files, how to setup ffmpeg in wamp could you please help in this regard.
srinivas
Jun 1st, 2009
hi,
i have windows sever, i am using php for developing any video to flv using ffmpeg. please provide the php code for converstion any video to flv on fly.
thank you.
srinivas
Jun 6th, 2009
hi,
i need conversion of any video to ‘.flv’ using FFMPeg on fly in php code. this is need to done by uploading a video
thank you.
Ujjwal Das
Jun 13th, 2009
Hello Expert,
I am trying to convert into .flv file from other format.
i am using this command
$srcFile = “/home/amitadut/public_html/gallery_video/robot.avi”;
$destFile = “/home/amitadut/public_html/gallery_video/robot.flv”;
$ffmpegPath = “/usr/local/bin/ffmpeg”;
$flvtool2Path = “/usr/local/bin/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);
But file is creating with 0 kb filesize.
can you tell me why 0 kb is creating??????????/
i have also print some propeties of ffmpeg for you
srcWidth: 160
srcHeight: 112
srcFPS: 25
srcAB: 32
srcAR: 32000
getDuration: 9.60000038147
getFrameCount: 240
getFrameRate: 25
getFilename: /home/amitadut/public_html/gallery_video/robot.avi
getComment:
getTitle:
getAuthor:
getCopyright:
getArtist:
getGenre:
getTrackNumber: 0
getYear: 0
getFrameHeight: 112
getFrameWidth: 160
getPixelFormat: yuv420p
getBitRate: 183948
getVideoBitRate: 104857200
getAudioBitRate: 32000
getAudioSampleRate: 32000
getVideoCodec: mpeg1video
getAudioCodec: mp2
getAudioChannels: 1
hasAudio: 1
Ajay
Jun 17th, 2009
Hi Sajith,
New to use this FFmpeg. and to linux also.
I installed ffmpeg and trying to use the Library of the ffmpeg.
#include “libavcodec/avcodec.h”
#include “libavformat/avformat.h”
I am using simple sample code ,which takes vedio stream ,and convert it to someformat.
I am facing problem is when i wrote the C Code .
I complied it which is not the problem,but linking is getting problem.
It is giving like this
test.c:(.text+0xf6): undefined reference to `av_register_all’
test.c:(.text+0×12b): undefined reference to `av_open_input_file’
test.c:(.text+0×146): undefined reference to `av_find_stream_info’
test.c:(.text+0×183): undefined reference to `dump_format’
test.c:(.text+0×1f6): undefined reference to `avcodec_find_decoder’
test.c:(.text+0×242): undefined reference to `avcodec_open’
test.c:(.text+0×257): undefined reference to `avcodec_alloc_frame’
test.c:(.text+0×25f): undefined reference to `avcodec_alloc_frame’
test.c:(.text+0×294): undefined reference to `avpicture_get_………
Means it is unable to link with the contents of the header files ..
Please do guide me ..what is the worng thing i am doing.
sandeep
Jun 23rd, 2009
i want to run ffmpeg in my system but its not run properly
and error msg are comming
“Error in loading ffmpeg”
pls help me
Web-Farmer
Jun 23rd, 2009
Hi folk,
Currently I come to work with FFMPEG to convert video stream into flv in php. Can anyone give how could i do this using php? Does anybody has class or source code for that?
A little help appreciate…
Web-Farmer
http://www,letsnurture.com
kumar
Jul 30th, 2009
Hello!
I want to resize jpeg image. Already resized but problem is image quality not good. i.e. Original image size is 640*480. After resize 200* auto. Resize image not looking same as original image. Anybody give an idea..
jeetu
Aug 20th, 2009
i’m installing ffmpeg and did this code
c:/ffmpeg/ffmpeg.exe –i d:/myfile.mpg d:/myfile.flv /y
on the DOS and then came this error
unable to find a suitable output format for ‘ui’
how can i solve this?
i’m using windows xp by the way.
thanks.
Bookmarks for November 12th through November 22nd « Peng’s Blog
Nov 22nd, 2009
[...] ffmpeg sample code | Programming Ideas, Logics, Tips and Tricks – [...]