I have a script calling a command to run an ffmpeg conversion on an uploaded video. It works only at random times however. Sometimes the form will finish submitting and the ffmpeg process will be running; at other times, the ffmpeg command fails to run at all. Here is the command that I'm running in an exec() function:
ffmpeg -i "uploaded_file -b 450k "converted_file" >/dev/null 2>&1 &
Can anyone explain why this will only work on certain tries and not on others?
What if ffmpeg fails and throws and error? Right now you're sending all output to /dev/null so you'll never know.
Change >/dev/null into >>/tmp/ffmpeglog to keep a log
Related
My problem is pretty simple: I can run ffmpeg commands perfectly fine on my server from the command line, but some of these commands experience trouble when I try to execute them from a PHP script.
For example, the following works in the command line:
ffmpeg -i cat.mpeg cat.avi
When in my PHP script, it also works as:
exec("/usr/local/bin/ffmpeg -i cat.mpeg cat.avi", $output);
This, as I said, works fine. However, this line works from the command line, but not in a PHP script:
ffmpeg -i cat.mpeg -vf scale=480:360 cat2.mpeg
Trying to put that into an exec() produces nothing. I've tried with/without quotes around the dimensions, different formats, etc. From the dozens of different commands I have tried, it seems that any will work from PHP as long as they don't contain the -vf flag. Clearly it works on the server, as executing from the command line proceeds with no issue; is there something silly I am missing here?
I believe it' not about the -vf flag, but about its parameters scale=480:360. Try escapeshellcmd() for escaping the command prior to executing it.
I never used system commands so I would need a little help running a php background process:
Can I run an exec() command on a shared server environment?
Is it possible not to wait for it to finish in the file I am running it from?
Can I use parameters to pass to this file?
It depends on the server configuration.
Maybe if you use screen.
You can use parameters as you would run the command from command line.
http://php.net/manual/en/function.exec.php
1 Try and find out
2 use /dev/null
3 exec('php -f '.ROOT_PATH.'/index.php '.$this->['param'].' '.$param.' '.$param.' "'.preg_replace('/"/', '\"', serialize($array))."\" > /dev/null &");
In a apcahe server i want to run a PHP scripts as cron which starts a php file in background and exits just after starting of the file and doesn't wait for the script to complete as that script will take around 60 minutes to complete.how this can be done?
You should know that there is no threads in PHP.
But you can execute programs and detach them easily if you're running on Unix/linux system.
$command = "/usr/bin/php '/path/to/your/php/to/execute.php'";
exec("{$command} > /dev/null 2>&1 & echo -n \$!");
May do the job. Let's explain a bit :
exec($command);
Executes /usr/bin/php '/path/to/your/php/to/execute.php' : your script is launched but Apache will awaits the end of the execution before executing next code.
> /dev/null
will redirect standard output (ie. your echo, print etc) to a virtual file (all outputs written in it are lost).
2>&1
will redirect error output to standard output, writting in the same virtual and non-existing file. This avoids having logs into your apache2/error.log for example.
&
is the most important thing in your case : it will detach your execution of $command : so exec() will immediatly release your php code execution.
echo -n \$!
will give PID of your detached execution as response : it will be returned by exec() and makes you able to work with it (such as, put this pid into a database and kill it after some time to avoid zombies).
You need to use "&" symbol to run program as background proccess.
$ php -f file.php &
Thats will run this command in background.
You may wright sh script
#!/bin/bash
php -f file.php &
And run this script from crontab.
This may not be the best solution to your specific problem. But for the record, there is Threads in PHP.
https://github.com/krakjoe/pthreads
I'm assuming you know how to use threads, this is very young code that I wrote myself, but if you have experience with threads and mutex and the like you should be able to solve your problem using this extension.
This is clearly a shameless plug of my own project, and if the user doesn't have the access required to install extensions then it won't help him, but many people find stackoverflow and it will solve other problems no doubt ...
I'm writing a class who let me access to recutils through PHP.
I have a 'database' file called books.rec in ../database/ and a script who runs my Recutils.php class.
My class simply launch system application with correct parameters.
But When I try to use recins with PHP's exec function, the command doesn't work will it work in command line.
This is the command that is executed by my script :
recins -f Title -v "Moi" -f Author -v "Moche" -f Location -v "loaned" -t Books ../database/books.rec
With PHP : Nothing, the record is not inserted (no error message at all too).
In terminal : OK, the command is well done and my record is inserted.
I also have a method to do a select operation using recsel and it works very well, will it use exactly the same file (and runs from exec too).
So, could someone explain me why the command don't work will another with the same file work ?
Thanks
PS : Further informations : http://www.gnu.org/software/recutils/
I would double check that you are running the command as the same user from the command line and your php script. That may be the problem. exec('whoami')
You said you had a script that starts your php script it should be the same user as that.
You might also want to running a simpler exec command to see if that will work first.
Other things to try:
Try checking stderr output exec('ls /tmp 2>&1', $out); This will redirect standard error to standard out so you get both.
Try using php's shell_exec() which will invoke a shell just like when you are running from the command line(eg. bash). shell_exec('ls /tmp 2>&1 >> /tmp/log') should even put all output into a log file.
I don't think this will help you but it is something to try if all else fails, set it as a background process and see if it completes. exec('nohup php process.php > process.out 2> process.err < /dev/null &'). The & will set the command to run in the background and let the script continue.
Good Luck
Is recins command accessible for PHP ? Also is path to books.rec correct ?
Try with absolute path.
am using ffmpeg in one of my sites with PHP , i convert files using the php exec function , actually it did me some headache trying to figure out WHEN this ffmpeg completes the file conversion after executing the exec command :( is there anyway to do that ?
Thanks
From what I've found, the exec function blocks until the ffmpeg conversion is complete.
For example, you can run ffmpeg like this in your PHP script:
exec($encode, $output);
(Where $encode is the ffmpeg command as a string, and $output is an array of each line of output from ffmpeg.)
For me, this exec command blocks my PHP script from continuing until ffmpeg conversion is complete, at which point my PHP script continues on, which seems to be how it is described in the PHP manual:
http://php.net/manual/en/function.exec.php
So, you can tell when exec is complete by following the exec command with another PHP command on the next line in your script that notifies you conversion is complete, or updates a database, or what-have-you.
FYI, I believe that pushing the exec command "into the background" means running the exec command but having the PHP script continue on simultaneously (i.e. asynchronously). For running the exec command in the background, Google "PHP background exec" or "php multi-process", such as:
http://www.php.net/manual/en/ref.exec.php#80241
http://www.sitecrafting.com/blog/to-run-php-code-in/