Tuesday, May 1, 2012

Installing and working with FFMPEG with PHP

Installing and working with FFMPEG with PHP


ffmpeg is widely used for media format conversions or encoding. You may have seen on Youtube. Whatever media format you upload, it is converted into streaming flash files.(.flv). You can get more information about ffmpeg from http://ffmpeg.mplayerhq.hu/

ffmeg installation on Linux may be easy because primarily it is made for Linux not Windows. However some good experts have made it for windows also.(here we are trying to explain ,how we can use ffmpeg in windows also how we can make use of FFMPEG with PHP to make good web applications with video)

one thing is confirmed that you must have dedicated Windows server to install ffmpeg. There are a lot of changes in the OS and PHP settings.(in this tutorial we explaining installation of ffmpeg in windows 7, and basic operations ,and working with php and Apache sever)

install ffmpeg on Windows 7( or Windows XP/Vista)

Right now I will guide you to install ffmpeg on Windows 7, please follow these steps to install ffmpeg on your Windows 7 machine.
1- You must have PHP version 5.2.6 or above installed
2- Graphics library GD2 must also be installed. ( ignore if you are using AppServe )
3- Now download ffmpeg.exe file from this URL http://ffmpeg.arrozcru.org/builds/


4- extract and Put this files (ffmpeg.exe and ffplay.exe, it will be available in side the bin folder) on a base or short location like c:/ffmpeg/ffmpeg.exe this path will be used in DOS commands. So keep it easy and simple.

5- In fact this is the only file required for encoding files. However I will guide you to creating frame images and video conversions etc.
7-put one sample .flv in the same directory(test.flv video file and ,now we can convert it using ffmpeg) 
flv to mpg conversion using FFMPEG

 now we are trying to convert the test.flv file to new_test.mg
6-If you switch to DOS simply give a source and destination file like
(syntax - ffmpeg -i inputfile.flv outputfile.mpg

        c:/ffmpeg/ffmpeg.exe –i test.flv new_test.mpg


It will start converting file.


You must have know-how about the parameters that you have to set for file encoding. This will increase or decrease size and quality of your output file. Read the documentation on this URL. http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html

Extracting frame from a video file

Hit following command against your video file and you'll get a splash image from the first frame of the video.(reference- http://flowplayer.org/tutorials/generating-thumbs.html)
Syntax-
ffmpeg -i [video_file] -f image2 -vframes 1 [output_image].jpg

example as given below

  • C:\ffmpeg>ffmpeg -ss 12 -i test.flv -f image2 -vframes 1 testimage.jpg




FFMPEG Working with PHP

7- Now come to another thing. It is called ffmpeg-php extension. This extension is made to manipulate encoded files. You can create frames and logo for your output file with PHP script.( Install PHP and   Apache Web Server  before you proceed 
8- You can download ffmpeg-php extension and API documentation from this URL
I used this link http://sergey89.ru/files/ffmpeg-php/ffmpeg-php-win32-all.zip and downloded ffmpeg-php-win32-all.zip which contain these files
9- Copy all fmpeg-php dll files into PHP "ext" (extensions) folder or System32, 

Where the extensions are located.














10-Also copy all these  in C:\WINDOWS\system32







10- Make this entry in php.ini (normally it will be in C:\Windows)
[PHP_ffmpeg]
extension=php_ffmpeg.dll 

(under - Dynamic Extensions )

also do the same modification in C:\AppServ\php5\php.ini (search for .ini file and modify it )




11- Now restart your PC. It’s good, because sometimes appserve restart doesn’t show effect.
(here I have used AppServe as the web server)
Check the ffmpeg installation http://localhost/phpinfo



If ffmpeg installed properly the the above detail will display.
Another php Script for checking ffmpeg
<?
extension_loaded('ffmpeg') or die("ffmpeg extension not loaded");


?>
If If you get “ffmpeg extension not loaded” then your web hosting provider does not have ffmpeg installed, if you get nothing, then you’re one the good track!
12- Take phpinfo() to check that extension is installed successfully. I hope it will be there. J
13- As ffmpeg.exe will be executed from DOS within PHP. There is a function in PHP called exec() for that. However it will not work straight away. Restart your pc in safe mode with Administrator account. Because mostly Windows 7 won’t let set security permissions in normal mode. Here is a very good example of setting permissions. http://www.somacon.com/p255.php
14- Ok, everything is done, How should we test that it is working?
here is the code http://www.sajithmr.com/ffmpeg-sample-code/ (this example explained in the bottom)
If sample code is printing all the information then your ffmpeg installation is successful and you can carry on with development of your application development.
When working on Home or PC, you might face some issues like slow encoding, It is because ffmpeg encoding requires more resources. You must experiment with small video/audio clips. However on Web servers you can try large files also.
If you are looking for ffmpeg hosting with everything ready then you can try Cirtex Hosting


Working with ffmpeg and PHP (tutorial will be uploaded soon)

Now here i am explaining how can we work with FFMPEG with PHP (in web Server)

Displaying Duration ,window size of a video file using ffmpeg with PHP  

1. First install one web sever (i have Installed AppServe) which support PHP and configure it for ffmpeg (as explained above)
2.the place the ffmpeg.exe file in web server (we have used - C:\AppServ\www)




 3.copy one video file to the same location
4.create one php file with the following code (file name test_ffmpeg.php)





<?php

$file= "test.flv";


ob_start();
passthru("ffmpeg -i ".$file." 2>&1");
$duration = ob_get_contents();
$Size = ob_get_contents();
$Other = ob_get_contents();
ob_end_clean();


$search='/Duration: (.*?)[.]/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE);
$duration = $matches[1][0];

list($hours, $mins, $secs) = split('[:]', $duration);
echo "Video Name - ".$file;
echo "</br>Duration - Hours: ".$hours." Minutes: ".$mins." Seconds: ".$secs;

$search='/Video: (.*?), (.*?), (.*?),/';
$Size=preg_match($search, $Size, $matches, PREG_OFFSET_CAPTURE, 0);
$Size = $matches[3][0];
echo "</br>Window size - ".$Size."</br>";

?>


out put




Convert video files with ffmpeg and PHP (eg: flv to avi) and Convert Every n seconds to JPEG
video is converted to .avi format and every 10 second'th frame is captured to image(.jpg)





<?
  /*** convert video to flash ***/
  $input_file = "test.flv";
  $output_file = "out.avi";
  $out_image_folder = "image";
  $out_put_dimension = "320x240";
 
 
 exec("ffmpeg -i ".$input_file." -ar 22050 -ab 32 -f flv -s  ".$out_put_dimension." ".$output_file."");
 exec("ffmpeg -i ".$input_file." -an -r .1 -y -s ".$out_put_dimension." ".$out_image_folder."/video%d.jpg");
?>




 Following tutorial will be uploaded soon

1.Thumbnail creation for  first frame of video
2.Video uploading and convering video
before trying uploading file make some modification php.in file (for upload limit, execution time tc)

in (C:\Windows, and C:\AppServ\php5)

make the following changes
----------------------------------------------------------------------------------------

post_max_size = 10000M ; Maximum size of POST data that PHP will accept.max_execution_time = 600 ; Maximum execution time of each script, in secondsmax_input_time = 600 ; Maximum amount of time each script may spend parsing request datamemory_limit = 1024M ; Maximum amount of memory a script may consume (1024MB)file_uploads = On ; Whether to allow HTTP file uploads.upload_max_filesize = 10000M; Maximum allowed size for uploaded files.extension=php_ffmpeg.dll

----------------------------------------------------------------





















8 comments:

  1. is it same proces in wamp server?

    ReplyDelete
  2. Hello
    I need .dll file but your provided link redirect to 404 can you please upload it.

    ReplyDelete
  3. I have been meaning to write something like this on my website and you have given me an idea. Cheers.
    Selenium training in Chennai

    Selenium training in Bangalore

    ReplyDelete
  4. Thanks For Sharing The Information The information shared Is Very Valuable Please Keep Updating Us Time just went On reading The article Python Online Training Aws Online Training Hadoop Online Training Data Science Online Training

    ReplyDelete
  5. Wonderful thanks for sharing an amazing idea. keep it...

    Start your journey with Best SAP Course and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete
  6. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. keep share more information!!!

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete