Run script after FFMPEG background task completes (via PHP) - php

I have a PHP script which triggers an FFMPEG file conversion via shell_exec().
shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 >/dev/null 2>/dev/null &');
This happens in the background (hence &), i.e. the script completes before conversion has finished.
Is there a way to call and execute a PHP script (to update a DB flag) once the conversion is complete?
I've done plenty of Googling but my knowledge of server commands just isn't up to understanding what I'm reading (e.g. this answer). The best I could manage was to redirect stdout to a file via
shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 > MYFILE.txt 2>/dev/null &');
...but obviously that just creates and writes to a file, it doesn't call and execute it via PHP.

I am not that great at server commands either, so I can't really help you there. But I do have this knack for figuring things out.
So I see a few ways you could do this, essentially you need PHP to do something when the command line call finishes. The obvious answer is to remove the & off the end of the command and make it blocking so PHP sticks around tell the job is done. But in doing so you can't return to the end user until that is done.
Option 1
So one way around this is to make a sort of Bootstrap PHP script that you call non-blocking. In this script do your now blocking conversion command and after that returns have PHP do something else.
//bootstrap.php
shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 > MYFILE.txt 2>/dev/null'); //blocking
//Update the DB
Then from your Controller or what have you call the bootstrap non-blocking
shell_exec('php {pathto}/bootstrap.php 2>/dev/null &');
This way the call to the bootstrap returns immediately but the conversion call is blocking, which gives you the chance to update the DB afterwords.
Option 2
Because the conversion is outputting a file, you could start a separate background job, that monitors the modified time of the output file. Then if the modified time is like a minute in the past you could assume it's done converting and update the DB. The modified time should continue to update as long as data is being added to the file.
Hope that helps.
PS. I have some code you may fine useful on GitHub
Runs Background processes in both windows & linux
https://github.com/ArtisticPhoenix/MISC/blob/master/BgProcess.php
PHP process locking ( Mutex simulation using files)
https://github.com/ArtisticPhoenix/MISC/blob/master/ProcLock.php
Command line argument mapping for PHP programs:
https://github.com/ArtisticPhoenix/Cli
Your welcome to use them if it helps you out.

Related

ffmpeg command executes via command line, but not via PHP script

I am working on a simple video upload and compression system and I am currently using the following process.
First Step
I use a multipart/form-data to upload the original video file, which I store in /var/www/site/videos_pre/video.mp4. My public folder is var/www/site/public_html/. I store an entry in my database with the video information.
Second step
I have a converter process which is very basic, but does the job (at least in CLI).
It has the following code:
public function converter($content_id)
{
$content = $this->videos_m->get($content_id);
$id = uniqid();
$name_mp4 = $content->name_slug.'_'.$id.'.mp4';
$name_webm = $content->name_slug.'_'.$id.'.webm';
$command_mp4 = 'ffmpeg -i ../videos_pre/'.$content->original_file.' -b:v 1500k -bufsize 1500k ./videos/'.$name_mp4;
system($command_mp4);
$command_webm = 'ffmpeg -i ./videos/'.$name_mp4.' -c:v vp9 -c:a libvorbis ./videos/'.$name_webm;
system($command_webm);
$update_video = new stdClass();
$update_video->archivo_mp4 = $name_mp4;
$update_video->archivo_webm = $name_webm;
$this->db->where('content_id', $content_id);
$this->db->update('Videos', $update_video);
}
Third step - where the problem occurs
I have tested this on Windows 10 and Ubuntu 18.04. The code works on Windows 10 in both php and cli. The code on Ubuntu only works with cli.
The resulting commands are something like this:
// First command to reduce the bitrate of the mp4
ffmpeg -i /var/www/site/videos_pre/video.mp4 -b:v 1500k -bufsize 1500k /var/www/site/public_html/videos/video_5e757d3e0d762.mp4
// Second command to convert the mp4 to a webm to get both types
ffmpeg -i /var/www/site/public_html/videos/video_5e757d3e0d762.mp4 -c:v vp9 -c:a libvorbis /var/www/site/public_html/videos/video_5e757d3e0d762.webm
If I execute those commands they work perfectly. If I run the php script, be it in the browser or via CLI, literally nothing happens. No error messages nothing.
I am not sure if it's a permission issue, if there's something specific for php to run this, or some module I've not activated. I have enough execution time, I have it at 600 seconds, but as I said it doesn't even take time, it just does nothing.
I can even echo the commands and they appear. So, basically the problem is I need to be able to run those commands from PHP. I did it on Windows, but Ubuntu 18.04 won't let me. Both OSs have ffmpeg recently installed and I've been able to convert on both of them.
I tried changing paths from absolute to relative, no difference at all.
So, I managed to solve it, and it turns out it was a permission problem.
I had my /var/www/site folder with permissions like this: myuser:www-data. After checking this answer on a similar issue, I changed the folder permission to www-data:root and it worked instantly.
I hope that permission change doesn't affect the rest of things, but for now everything's working fine.

Reduce FFmpeg CPU Usage with cpulimit and PHP

I run FFmpeg on my server using PHP exec().
It currently uses 100% of the cpu thread while encoding.
I followed this gude that uses a program called cpulimit to reduce it to 30%.
PHP
$args = "nice -19 cpulimit -l 30 -- ffmpeg -y -i intput.avi -vcodec libx264 -acodec aac -b:a 192k -threads 1 output.mp4"
exec(escapeshellcmd($args));
FFmpeg/PHP works, and it will work with nice/cpulimit through the terminal, but after adding
nice -19 cpulimit -l 30 -- to the PHP script it no longer works with exec().
Output
FFmpeg output returns blank. I'm not able to see the full output, I tried using:
$output = shell_exec($args);
echo "<p>$output</p>"
and
file_put_contents("/var/www/mysite/logs/$output.log", $line . PHP_EOL, FILE_APPEND);
But they return 1 empty line.
Solution
My thought is that www-data runs FFmpeg and nice/cpulimit may need root?
How can I get PHP exec() to work with FFmpeg args and cpulimit?
Or is there an alternative way to limit the usage %?
If safemode is enabled then, the execution of programes are limited. Check this documentation here, http://php.net/manual/en/function.exec.php
I found the answer on this forum post using google translate.
https://murobbs.muropaketti.com/threads/nice-komento-ja-php-n-exec-funktio-ratkaistu.551174/
Define the path to FFmpeg if using nice or cpulimit
nice -19 cpulimit -l 30 /usr/local/bin/ffmpeg -i ...
Now it works through PHP exec().

FFMpeg working in command line but not in PHP script

I am using FFMpeg to convert videos and it is working fine from the command line. I am using the following command:
ffmpeg -i input.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv output.flv
However, when I run the command using PHP script, the output video is not encoded.
exec("ffmpeg -i input.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv output.flv",$output, $returnvalue);
$returnvalue = 127;
FFMPEG installed path :
[root#localhost ~]# which ffmpeg
/root/bin/ffmpeg
My Script Path :
www.domainname.com/core/foldername/ffmpeg.php
Please provide me solution for the same ASAP.
Thank you.
The natural way to run ffmpeg from PHP scripts is something like:
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i input.avi output.avi &");
echo "Done.\n";
?>
There are several issues that need to be pointed out here. The first one is that, although we specified we want ffmpeg to be executed in the background (using the ampersand operator "&"), PHP script will not continue it's execution until ffmpeg has finished its execution. This is due to the fact, mentioned in one of the notes for the ​PHP's exec() function that says:
If a program is started with this function, 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.
Don't be confused about the example showing ​shell_exec() call instead of ​exec(). All of the ​PHP's Program execution functions share the similar code base and limitations.
So, to work around this issue, we need to do something like this:
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null &");
echo "Done.\n";
?>
The part that says ">/dev/null" will redirect the standard OUTPUT (stdout) of the ffmpeg instance to /dev/null (effectively ignoring the output) and "2>/dev/null" will redirect the standard ERROR (stderr) to /dev/null (effectively ignoring any error log messages). These two can be combined into a shorter representation: ">/dev/null 2>&1". If you like, you can ​read more about I/O Redirection.
An important note should be mentioned here. The ffmpeg command-line tool uses stderr for output of error log messages and stdout is reserved for possible use of pipes (to redirect the output media stream generated from ffmpeg to some other command line tool). That being said, if you run your ffmpeg in the background, you'll most probably want to redirect the stderr to a log file, to be able to check it later.
One more thing to take care about is the standard INPUT (stdin). Command-line ffmpeg tool is designed as an interactive utility that accepts user's input (usually from keyboard) and reports the error log on the user's current screen/terminal. When we run ffmpeg in the background, we want to tell ffmpeg that no input should be accepted (nor waited for) from the stdin. We can tell this to ffmpeg, using I/O redirection again ** "ffmpeg command-line tool in the background would be similar to this:
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -y -i input.avi output.avi </dev/null >/dev/null 2>/var/log/ffmpeg.log &");
echo "Done.\n";
?>
The ​"-y" option is used to auto-overwrite the output file (output.avi) without asking for yes/no confirmation. If you need the opposite scenario, to auto-cancel the entire process if the output file already exists, then use "-n" option instead.
Wrapper Libraries
Some PHP libraries allow wrapping ffmpeg calls into PHP objects, and give you a nice syntax to work with if you don't like to use the command line. One of these is the actively maintained ​PHP-FFMpeg. It only requires you to ​download a recent ffmpeg and ffprobe build apart from installing the PHP components. Then you can run PHP code like this:
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
->filters()
->resize(new FFMpeg\Coordinate\Dimension(320, 240))
->synchronize();
$video
->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
Of course you need to take care of running such a task in the background. Libraries such as ​GearmanClient facilitate this.
Note: ​ffmpeg-php is an extension that is not developed since 2007 (and requires "ffmpeg-0.4.9_pre1 or higher"), which means that you are restricted to use a very old version of ffmpeg, without possibility to update it to the latest version. Since a lot of changes/improvements are being made, inside ffmpeg's code, every day, it makes ffmpeg-php incompatible with the latest ffmpeg.
Read the official documentation for more info.
I covered the solution that helps most people that I spoke to about this in another thread.
https://stackoverflow.com/a/38626752/2074077
Essentially, your user needs access to the public directories.
<?php
exec('whoami'); // Gives you the running user; often it's www-data
?>
Then change your public ownership to the whoami user.
sudo chown -R www-data:root /var/www
This happened to me, I found this on another forum; the trick inside of PHP is to give the absolute path to FFMPEG. Try running it like this..
exec("/root/bin/ffmpeg -i input.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv output.flv",$output, $returnvalue);
Something about the fact that php is a different user than you are when typing straight into the command line. I guess it doesn't have the same shortcut list or whatever it is.
Not sure exactly why but it worked for me!
Good luck.

How to programmatically start/stop FFMPEG stream transcoding

I have an ip webcam which provides an MJPEG stream. I can successfully transcode and save that stream with ffmpeg under OSX. The following gives me pretty much what I want:
ffmpeg -f mjpeg -i "http://user:pass#10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4
That will start an FFMPEG session and begin saving the live stream to my test.mp4 file. pressing q will quit ffmpeg and save the file.
I would like to programmatically start & stop the recording using a PHP or Bash shell script. I have tried the following:
<?php
$pid = pcntl_fork();
if($pid == -1){
die("could not fork");
}elseif($pid){
// we are the parent...
print $pid.' started recording. waiting 10 seconds...';
sleep(10); // Wait 10 seconds
print_r(shell_exec("kill ".$pid)); // Kill the child recording process
echo 'done';
exit();
}else{
// we are the child process. call ffmpeg.
exec('../lib/ffmpeg -f mjpeg -i "http://user:pass#10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4');
}
But there are two problems:
The ffmpeg process does not end/die (probably because its forked again)
When I manually kill the ffmpeg process, the video file is not readable
So I was doing a combination of things wrong.
For starters, I needed to push the output form ffmpeg to a log file and also tell it to overwrite my temp file without prompting using the -y argument.
So instead of
ffmpeg -f mjpeg -i "http://user:pass#10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4
I am now using
ffmpeg -y -f mjpeg -i "http://user:pass#10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log &
The second problem was that I wasn't waiting long enough before sending the kill command to ffmpeg, and so a corrupt file was being created.
By adding the -t (for time limit) argument with 1 second, I determined that it takes an average of 15 seconds for ffmpeg to record 1 second of video. Increasing the time limit to 10 seconds made the average increase to 25 seconds, so it seems that on my server at least, theres 14 seconds of overhead. By increasing my sleep command in my php script to 30 seconds, I was able to get a useable video file.
So having PHP kill the ffmpeg process results in an unknown (or approximate at best) recording time length which is completely dependent on CPU power, network bandwidth, etc.
Thats a bit of a bummer because I had hoped to be able to increase the recording length depending on some external variables (I have insteon motion sensors feeding a database, i would like to record until the motion stops).
In any event, here is a working PHP script in case it helps someone in the future:
<?php
print date('H:i:s')."\nStarted recording. waiting 60 seconds...\n";
exec('../lib/ffmpeg -y -f mjpeg -i "http://user:pass#10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log &');
sleep(60); // Wait long enough before killing, otherwise the video will be corrupt!
shell_exec('pkill ffmpeg'); // find and kill
echo "done\n\n";
exit();
?>
Send a SIGQUIT signal to the background process to terminate the ffmpeg command normally.
Just use
kill -s QUIT $PID
and the process will finish with a non-corrupted MP4 video file.
Send the "q" key:
process.StandardInput.WriteLine("q");

how to convert video from one format to another using php

hi i want to include the vedio download option in my webpage. I am using ffmpeg, but it seems to work very slow. Is there is any other way to do this or how to spead up the ffmpeg.
i am using this code to get the frames from the vedio.
to convert the vedio
$call="ffmpeg -i ".$_SESSION['video_to_convert']." -vcodec libvpx -r 30 -b ".$quality." -acodec libvorbis -ab 128000 -ar ".$audio." -ac 2 -s ".$size." ".$converted_vids.$name.".".$type." -y 2> log/".$name.".txt";
$convert = (popen("start /b ".$call, "r"));
pclose($convert);
to get the frame from the vedio
exec("ffmpeg -vframes 1 -ss ".$time_in_seconds." -i $converted_vids video_images.jpg -y 2>);
but this code does not generate any error its loading continously.
Cache or pre-generate the output format.
Use the ffmpeg-php library. Should boost up some processes rather then manually calling the ffmpeg command line tool using exec.
I'd first of all take PHP out of the equasion and time how long it takes to do what you're after via the command line.
Once you're happy that works the way you'd like it to, make sure you've tweaked your script's execution time (see http://php.net/manual/en/function.set-time-limit.php) to accomodate what's likely to take a while.
Consider an async approach if it's getting in the way of UX.
Ta

Categories