Jump to content

Exec System()


The Little Guy

Recommended Posts

I was reading about system in the manual, and it seems like it allows for running programs in the background, this way I can run 10 versions of the same program (if I wanted).

 

Note: If a program is started with this function' date=' in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.[/quote']

 

So my questions are:

 

1. How do you output to a file?

2. Is there a better way to do this?

3. Does system actually do what I am thinking it does?

Link to comment
Share on other sites

hi 'The Little Guy',

 

I believe the function you are looking for is 'popen'. If you want to run a process in the background here is a snippet of code that may help:

 

<?php
function callTool ($path,$file) {
    chdir($path); $call = $path.$file;
    pclose(popen('start /b '.$call.'', 'r'));
}
?>

Link to comment
Share on other sites

the function I just gave you will start a program and run it indefinitely in the background processes. It doesn't look like that's what you want to do with ffmpeg. I'm guessing that you want to run that program to compress video etc. In the case of popen, this allows you to run a program indefinitely, thats not what ffmpeg will be doing. Since ffmeg has a command line interface you could use 'system()' to run the program and give you the return value to verify that it worked correctly. For instance:

 

// $last_line will return the very last line of the return response
$last_line = system('dir', $retval);

echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

 

This script on a windows server will give you a list of files and folders in the current directory. You could probably use this same code with ffmpeg, just replace the dir with the ffmpeg commands, and read the $retval to make sure everything executed successfully. I've not tested this with ffmpeg myself, but i know that this code works with other command line executables so this should work fine.

Link to comment
Share on other sites

shell_exec("/usr/bin/ffmpeg -i '$f' -vcodec flv -f flv -r 29.97 -s {$resX}x{$resY} -aspect 4:3 -padtop {$padTop}px -padbottom {$padBot}px -maxrate {$bitr}k -g 160 -cmp 2 -subcmp 2 -mbd 2 -flags +aic+cbp+mv0+mv4+trell -ac 1 -ar 22050 -ab 56k '/home/ryannaddy/flv.dudeel.com/flv/{$fileName}.flv'");

 

Here is my current command.

 

What would I do with this to run this command simultaneously on 2+ files?

 

Sorry, I'm having confusion...

Link to comment
Share on other sites

if yer using *nix use the & doohickie, if yer using windows, ya need another app to start the app in the background.

php will not run apps in the background, that for the OS to do.

 

Shell/exec return after the command is finished, reason ya use the & to put the app in the background in *nix machines.

 

 

 

Link to comment
Share on other sites

if yer using *nix use the & doohickie, if yer using windows, ya need another app to start the app in the background.

php will not run apps in the background, that for the OS to do.

 

Shell/exec return after the command is finished, reason ya use the & to put the app in the background in *nix machines.

 

Sorry laffin,

 

I've definitely run applications in the background using php.

Link to comment
Share on other sites

OK, now that I got that, another question...

 

how can I get the code to stop after the foreach loop and wait for the files to convert?

 

Example:

$fls = array();
$fls[] = '/home/ryannaddy/DudeelVideo/untuned20@gmail.com/iBfH0qsrowWuS.mp4';
$fls[] = '/home/ryannaddy/DudeelVideo/untuned20@gmail.com/xxxb3RFoljeN7.mpeg';
foreach($fls as $f){
      shell_exec("/usr/bin/ffmpeg -i  '$f' ... 'hq_{$fileName}.flv' >/dev/null 1>/dev/null 2>/dev/null &");
}
// Wait for files to convert to flv when done continue...
foreach($fls as $f){
      // run a command to make images (current command works).
}

Link to comment
Share on other sites

There isn't a way to do that.  You could keep checking for the processes using ps, but unless this will only run 1 script at a time, that won't work.  Other wise, the only way I can think of is to check if the output file exists.  ffmpeg prob creates the output file immediately, but you could use a tmp name with a rename cmd then check if the rename has happened.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.