Home > FFMPEG > Execute FFMPEG Command from PHP Applications

Execute FFMPEG Command from PHP Applications

It has been a nightmare for me searching for executing FFMPEG command from PHP. But finally i got the solution for executing ffmpeg from php script. This article will guide you on how to convert videos and audios using FFMPEG from within the PHP scripts.

NOTE:
I have tested this on Red Hat Enterprise(RHEL). Not sure about other Linux OS like Ubuntu etc. But i would say if it works then its rocking to execute FFMPEG inside the php scripts.

PHP Script

1
2
3
4
5
< ?
   define('FFMPEG_LIBRARY', '/usr/local/bin/ffmpeg ');
   $exec_string = FFMPEG_LIBRARY.' -i inputfile.mpg outputfile.flv';
   exxc($exec_string); //where exxc is the command used to execute shell command in php
?>

Your email:

 


PHP Code Explanation

  • Defined an variable called FFMPEG_LIBRARY that holds the path to FFMPEG Libraries
  • Finally i am calling exec() function that executes the FFMPEG command


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: FFMPEG Tags:
  1. February 5th, 2008 at 22:12 | #1

    above command is not working.in php script. Pls help

  2. February 5th, 2008 at 22:26 | #2

    Hi skvenkate,
    I think you have not enabled some parameter required for ffmpeg to work with PHP. Can u please check crosscheck the installation steps for ffmpeg with this article http://www.hiteshagrawal.com/ffmpeg/installing-ffmpeg-easily-on-linux

  3. stvinzce
    February 12th, 2008 at 04:44 | #3

    a litte error in your PHP script : “exxc” doesn’t exist, I think “exec” is better ;)

  4. Jonathan
    March 24th, 2008 at 18:55 | #4

    This isnt working at all at my script. It just shows me blank page. And it doesnt create anything at all. Can anyone help me at this? If this worked out for you, please tell me. Thanks.

  5. April 9th, 2008 at 22:38 | #5

    nice one

  6. December 13th, 2008 at 11:33 | #6

    Just superb! Great solution, you made my developing life a lot easier =)

  7. varun pandey
    February 7th, 2009 at 04:27 | #7

    I m not getting your message.!!!!

  8. dhaval dave
    February 23rd, 2009 at 23:16 | #8

    Hi Actully i want to convert .rm to .flv what can i do for that using ffmpeg?Can anyone help me at this?
    Thanks

  9. sudip
    May 19th, 2009 at 23:30 | #9

    Hi,
    I have booked some space from a third party hosting service provider which includes ffmpeg-php service. I have used the syntax from my php script:

    When I execute the script it always going to die section. Rather than when I am using this ( the $videoffmpeg value )

    It created a flv file in the desire folder, but its a zero byte file.

    I need some sugg..

    thanks

  10. sudip
    May 19th, 2009 at 23:32 | #10

    Hi,
    I have booked some space from a third party hosting service provider which includes ffmpeg-php service. I have used the syntax from my php script:

    php tag
    $videoffmpeg=’/usr/local/bin/ffmpeg’;
    $encode_cmd = “$videoffmpeg -i a.mp4 -sameq b.flv”;
    exec($encode_cmd) or die ( “ffmpeg did not work” );
    php tag

    When I execute the script it always going to die section. Rather than when I am using this ( the $videoffmpeg value )

    php tag
    $videoffmpeg=’ffmpeg’;
    $encode_cmd = “$videoffmpeg -i a.mp4 -sameq b.flv”;
    exec($encode_cmd) or die ( “ffmpeg did not work” );
    php tag

    It created a flv file in the desire folder, but its a zero byte file.

    I need some sugg..

    thanks

  11. June 17th, 2009 at 09:29 | #11

    Line, 3 is where you input your video, the output.jpg is the output mpeg also called jpg image format which will be be grabbed. the output.ogm is the actual encoded video. ogm is just a open source video container like avi mkv etc. you can change it to whatever suits your needs.

    Line 12, deals with the image grabbing which uses ffmpeg, not much to be said here.

    Line 16, deals with Mencoder this basically is the encoding engine, here you can change bit-rate, video width and height and aspect ratio. and volume control.

    Dependency packages and operating systems

    1. Ubuntu 8.10 Desktop or Server version
    2. Lamp server – php5 CLI everything that includes inside Lamp server
    3. FFmpeg
    4. Mencoder

    thanks it.

  12. Naved
    July 4th, 2009 at 04:09 | #12

    This isnt working at all at my script. It just shows me blank page. And it doesnt create anything at all. Can anyone help me at this? If this worked out for you, please tell me. Thanks.


    I also refer this link but the result is same ..on above link can anyone tell what will be the value should be passed in “&$code var..
    Thanks In Advance
    Naved Mohammad

  13. utsavi
    December 23rd, 2009 at 21:51 | #13

    Hi,

    I have been working the the audio conversion formats from wav,avi to mp3. I have successfully installed ffmpeg and it does shows in phpinfo().

    However, when I run the script , it does not convert the audio file.

    Below is the php script I am trying to execute:

    define(‘FFMPEG_LIBRARY’, ‘ /usr/local/bin/’);
    exec(FFMPEG_LIBRARY.’ffmpeg -i notify.wav newwave.mp3′) or die (“ffmpeg did not work”);

    Output is : ffmpeg did not work.

    Any guess?

    Can you help with this? I am trying since long time to resolve the issue.

    Thanks,
    Utsavi.

  14. Gin
    January 21st, 2010 at 05:28 | #14
     array(\pipe\, \r\), // stdin is a pipe that the child will
    read from
    1 => array(\pipe\, \w\), // stdout is a pipe that the child will
    write to
    2 => array(\pipe\, \w\) // stderr is a file to write to
    );
    
    $pipes= array();
    $process = proc_open($cmd, $descriptorspec, $pipes);
    
    $output= \\;
    
    if (!is_resource($process)) return false;
    
    #close child's input imidiately
    fclose($pipes[0]);
    
    stream_set_blocking($pipes[1],false);
    stream_set_blocking($pipes[2],false);
    
    $todo= array($pipes[1],$pipes[2]);
    
    while( true ) {
    $read= array();
    if( !feof($pipes[1]) ) $read[]= $pipes[1];
    if( !feof($pipes[2]) ) $read[]= $pipes[2];
    
    if (!$read) break;
    
    $ready= stream_select($read, $write=NULL, $ex= NULL, 2);
    
    if ($ready === false) {
    break; #should never happen - something died
    }
    
    foreach ($read as $r) {
    $s= fread($r,1024);
    $output.= $s;
    }
    }
    
    fclose($pipes[1]);
    fclose($pipes[2]);
    
    $code= proc_close($process);
    
    return $output;
    }
    ?>
    
  15. Gin
  1. March 26th, 2010 at 18:24 | #1
  2. July 15th, 2010 at 17:37 | #2