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
?>


 


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


Related Articles:

About Hitesh Agrawal

Wait... checking
Tagged . Bookmark the permalink.

20 Responses to Execute FFMPEG Command from PHP Applications

  1. skvenkate says:

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

  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 says:

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

  4. Jonathan says:

    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. Just superb! Great solution, you made my developing life a lot easier =)

  6. varun pandey says:

    I m not getting your message.!!!!

  7. dhaval dave says:

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

  8. sudip says:

    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

  9. sudip says:

    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

  10. mysoogal says:

    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.

  11. Naved says:

    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

  12. utsavi says:

    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.

  13. Gin says:
     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;
    }
    ?>
    
  14. Pingback: comandos cmd

  15. Pingback: Motionshare - Free video sharing for Churches etc - Video Editing Forums: Digital Director

  16. karthick says:

    can any one send how to get screenshots from video using php?

  17. Viper says:

    @karthick
    @exec($ffmpeg.’ -i ‘.$path.’ -ss ‘.$time.’ -f image2 ‘.$image_path);

    where:
    $ffmpeg – absolute path to ffmpeg library(e.g. for *nix it’s /usr/bin/ffmpeg, for Windows c:/www/htdocs/ffmpeg.exe);
    $path – where media file locate;
    $time – time where library get screenshot(more info on official library homepage, by simple way we can set it in seconds);
    $image_path – where save resulting file. This is absolute path w/ file name(e.g. c:/www/htdocs/screenshots/screenshot.jpg)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>