Running ffmpeg commands through php locally

Discussion in 'Web Design and Development' started by Macman1993, Aug 18, 2010.

  1. Macman1993 macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #1
    I have ffmpeg installed on my macbook pro running sl 10.6.4 and I have enabled php on it. I am trying to use php on my machine to run ffmpeg commands to convert video. I have been attempting to use exec() command but it is not working. When I type the command directly into terminal it runs without a problem but when trying to do this through php nothing happens. I am attempting to convert about 600 short videos doing this and no programs that support bulk encodes will work. Heres my code does anyone see why this wouldn't work?

    PHP:
    exec('ffmpeg -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4');
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
  3. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #3
    Sometimes you have to specify the exact path to the program/script name. So figure out where exactly the ffmpeg is, and then call it like this...

    PHP:
    exec('/usr/local/bin/ffmpeg ...');
    Of course, substitute /usr/local/bin for the correct path.

    HTH!
     
  4. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #4
    I've checked and my php.ini file lists safe mode as off. I also tried specifying the location of ffmpeg in my script but still no luck.

    PHP:
    exec('/opt/local/bin/ffmpeg -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4');
    Just to give it a shot I also tried this with no luck.

    PHP:
    define('FFMPEG_LIBRARY''/opt/local/bin/ffmpeg ');
    $exec_string FFMPEG_LIBRARY.' -i /Library/WebServer/Documents/video.avi -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4';
    exec($exec_string);
     
  5. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #5
    When you say "no luck" — what do you see when you run the script (I guess you're running it in a web browser).

    What happens if you run the php script from the command line?

    Code:
    php path/to/script.php
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
  7. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #7
    Thanks, the idea about running the script through terminal worked and the video converted. I'm trying to use php to run the ffmpeg conversion on all the flv files I have in a folder so I have added to the script to make it loop through all the files and attempt to convert the flv files. Whenever I run this through terminal it wont convert the videos though. I know my conversion command is working fine and I can move the commands out of the "if ($ext=='flv')" statement and it will work. I need to check that these are flv files so I don't try and convert the wrong things but it simply will not work. And terminal doesn't display any errors or even the text "its an flv".

    PHP:

    <?php
        
        $dir 
    opendir(".");

            
        while ((
    $file readdir($dir)) !== false) {
            
            
    $ext substr($filestrrpos($file'.') + 1) ;
            
                    
    //Checks that the file is an flv file so I don't convert files I don't want converted (this condition breaks the script when run through terminal)
            
    if ($ext=='flv') {
            
                    
    //Used for testing in a browser to make sure the condition is valid
                
    echo "its an flv";

                   
    //Code to convert the videos which will not work inside this if loop
                   
    $exec_string ='/opt/local/bin/ffmpeg -i /Library/WebServer/Documents/video.flv -vcodec libx264 -vpre hq -vpre ipod640 -b 250k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 /Library/WebServer/Documents/video.mp4';
                   
                     
    exec($exec_string);
               }
               
        }
    ?>

    Oddly enough if I change the condition "if ($ext=='flv')" to something I know is true like "if (1==1)" it runs. I know the condition is working because when viewed in a browser the text "Its an flv" is showing up the proper amount of times.

    Can someone please explain what is happening I am at a loss here.
     

Share This Page