Run PHP exec() asynchronously, but check for completion? - php

I'm coding up a website back-end that will include user-uploaded video. In order to ensure maximum accessibility, I'm compressing the uploaded videos and re-saving them as .mp4 and .webm format to cover all browsers (or as many as possible anyway). To do this, I'm running an avconv command in the PHP exec() function.
I don't want to make the user wait for the script to finish before the page loads, so I'm running the code asynchronously. My code so far is below.
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"');
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');
In addition to running the exec functions, I also save the video to a database and send the user an email thanking them for uploading their video.
Here's the rub: I want the server to WAIT until the video conversion is finished, and THEN add it to the database and send the user an email. Basically, the program flow would be:
User uploads video.
Video is placed in a temp folder.
User is taken to a thank you page indicating their video will be up shortly.
The server executes two avconv commands to convert and compress the video for web use.
Once BOTH conversions are finished, the video info is added to a MySQL database, an email is sent to the user, and the original uploaded video is deleted.
It may just be my ignorance of the command line (in fact it almost definitely is), but how could I 'queue up' these commands? First do both conversions, then call a PHP script to add to the database, then delete the original video, all while being asynchronous with the original PHP script?
EDIT: I've tried queuing them up with an '&&' operator, like below:
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 && avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');
However, that seems to cancel out the fact that I'm running it asynchronously, since the page now seems to wait for the command to finish.

You should start an asynchronous command line php script that encodes both videos and then sends an email :
upload.php :
exec('/usr/bin/php -f encode_files.php > /dev/null 2>/dev/null &"');
echo "Files will be encoded, come back later !";
encode_files.php
exec('avconv ...'); // Synchronously ! Without > /dev/null etc ...
exec('avconv ...'); // webm ...
mail('user#user.com', 'Encoding complete ! ', 'Great ! ');
I left the call as "bash -c exec ..." but i think there are shorter ways to call php scripts asynchronously :
Asynchronous shell exec in PHP
You can even pass params (like the user/video id, ...)
$cmd = 'nohup /usr/bin/php -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & printf "%u" $!';
$pid = shell_exec($cmd);

You can disconnect the PHP script from the client but leave it running to complete your tasks.
// Your preliminary stuff here ...
/// then send the user elsewhere but carry on in the background
ignore_user_abort(true);
set_time_limit(0); // i.e. forever
header("Location: thankyoubutwait.php", true);
header("Connection: close", true);
header("Content-Encoding: none\r\n");
header("Content-Length: 0", true);
flush();
ob_flush();
session_write_close();
// more of your video stuff here including database writes
// and clean up bits
// (you may end up with zombie processes though so check your logs or write statuses to files etc.)

It's easy you just have to check the good execution of your command line like this:
// Your code before...
$command = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"'
exec($command, $return, $status);
if($status == 0 ) {
$command2 = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"';
exec($command2, $return2, $status2);
if($status2==0){
// let your user know your video traitement has been done
// lauch a new function for alert him
}
}
// Kill your process at end
die();

Related

Running commands from PHP exec shows different result than from shell

The PHP exec command is not executing the same as the shell's interaction.
cd /var/www/myfolder
zip -r /var/www/myfolder/temp/newfile.zip ./*
generates just a zip of files in the temp directory. However (simplified version):
$zip_dir = '/var/www/myfolder';
$temp_dir = $zip_dir . '/temp/';
chdir($zip_dir);
exec('zip -r ' . $temp_dir . 'newfile.zip ./*', $return);
generates the same zip but with the full path's of var and www (which results in two copies of myfolder so my file is twice as large as needed). The $return however has the same output as the command line execution. Both state only 15 files directories/folders were zipped. There is no mention of var or www in the PHP output.
I believe the chdir() command will not have any bearing on how the commands in exec() are run. So this might fix it:
$zip_dir = '/var/www/myfolder';
$temp_dir = $zip_dir . '/temp/';
$cmd = 'cd ' . escapeshellarg($zip_dir) . ' && zip -r ' . escapeshellarg($temp_dir . 'newfile.zip') . ' ./*';
exec($cmd, $return);
Note we always escape variables being passed to the command line.
But why not just zip within PHP?
<?php
$zip_target = "/var/www/myfolder";
$zip_file = "/var/www/myfolder/temp/newfile.zip";
$zip_temp = tempnam(sys_get_temp_dir(), 'a458');
$zip_obj = new \ZipArchive();
if ($zip_obj->open($zip_temp, ZIPARCHIVE::OVERWRITE)) {
$zip_obj->addGlob("$zip_target/**/*.*");
}
$zip_obj->close();
rename($zip_temp, $zip_file);

adding a logfile to ffmpeg

ive been searching for a solution for some hours now, but I cant find a solution.
I want ffmpeg to give me a log file after converting is completed.
this is my code:
$convert = '/usr/bin/ffmpeg -i /var/www/html/videos/' . $fileNormal . '.mp4 -vn -sn -c:a mp3 -ab 192k /var/www/html/audio/' . $fileFixed . '.mp3 2> var/www/html/logs/' . $id . '.txt';
exec($convert);
same with
$convert = '/usr/bin/ffmpeg -i /var/www/html/videos/' . $fileNormal . '.mp4 -vn -sn -c:a mp3 -ab 192k /var/www/html/audio/' . $fileFixed . '.mp3 2> /var/www/html/logs/' . $id . '.txt';
exec($convert);
or
$convert = '/usr/bin/ffmpeg -i /var/www/html/videos/' . $fileNormal . '.mp4 -vn -sn -c:a mp3 -ab 192k /var/www/html/audio/' . $fileFixed . '.mp3 2> /logs/' . $id . '.txt';
exec($convert);
if I add anything at the end after mp3 it doesnt convert at all.
appreciate any help. thanks
If you want to log the screen output of the ffmpeg command, you can do this by redirecting the output to a log file. For example:
/usr/bin/ffmpeg -i /var/www/html/videos/' . $fileNormal . '.mp4 -vn -sn -c:a mp3 -ab 192k /var/www/html/audio/' . $fileFixed . '.mp3 > log_file_path.txt
Also see the man page for the ffmpeg command: https://linux.die.net/man/1/ffmpeg. It mentions loglevel and debug options which can be used to control the level of detail in the log data

PHP LFTP data mirror output

I'm using a Linux local computer and need to backup/mirror some very large file structures regularly. I only have access to SFTP.
I was after a simple one click solution. I originally tried to write the little script in BASH but I've never used it before and am not up to scratch with the syntax so I resorted to PHP. (I do understand PHP is not designed for this kind of work, but I'm on a tight time scale and don't have the time to get into BASH atm)
<?php
//init
parse_str(implode('&', array_slice($argv, 1)), $_GET);
$error = array();
$lPrefix = '/home/hozza/Sites/';
$archiveLocation = '/home/hozza/Backups/';
$lDir = isset($_GET['l']) ? $_GET['l'] : $error[] = 'Local Directory Required';
$rDir = isset($_GET['r']) ? $_GET['r'] : $error[] = 'Remote Directory Required';
$bookmark = isset($_GET['b']) ? $_GET['b'] : $error[] = 'lftp Bookmark Required';
//Check for args
if(count($error) == 0) {
$archiveName = end(explode('/', $lDir)) . '_' . date('Y-m-d_H-i');
//Validate local dir
if(is_dir($lPrefix . $lDir)) {
//preserve Sublime Text 2 config SFTP files
$ST2_SFTP_conf = false;
if(file_exists($lPrefix . $lDir . '/sftp-config.json')) {
$ST2_SFTP_conf = file_get_contents($lPrefix . $lDir . '/sftp-config.json');
unlink($lPrefix . $lDir . '/sftp-config.json');
}
//Start mirror
$lftOutput = explode("\n", shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark));
//Tar regardless of lftp error or success
$tarOutput = shell_exec('cd ' . $lPrefix . ' && tar -czf ' . $archiveLocation . $archiveName . '.tar.gz ' . $lDir);
//Output completion or errors
shell_exec('notify-send -i gnome-network-properties -t 0 "Mirror & Archive Complete" "' . $archiveName . '\n\n' . implode('\n', $lftOutput) . $tarOutput . '"');
//put back ST2 SFTP conf
if($ST2_SFTP_conf != false) file_put_contents($lPrefix . $lDir . '/sftp-config.json', $ST2_SFTP_conf);
exit;
}
else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . $lDir . ' \n Does not exist! D:"');
}
else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . implode('\n', $error) . '"');
?>
It can be run for many sites via a short-cut like so...
terminator -T "Mirror & Archive" -e "php ~/Programs/mirror.php l=local-dir_path r=./ b=lftp-bookmark-name"
If no password is in the LFTP bookmark (there shouldn’t be as it's stored in plain text) the terminal prompts for a password, after the script has run, a nice notification is given with some info about files/folders/speed etc.
However, when the script is running in a terminal, only the "input password" bit is output to the terminal, I would like all the output displayed in the terminal (normally that would display what file/folder is currently working with etc.)
Anyone know how to do that?
IIRC the reason that you see the password prompt output to the terminal is that it is using stderr. You could try redirecting stdout to stderr for your commands which should show you the 'real-time' progress. Tack this on to the end of the shell_exec() command: 1>&2
ie:
shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark . ' 1>&2')
However, this will preclude you from having anything returned by shell_exec for logging purposes. What I would suggest is something like:
$log_stem = '/tmp/' . time() . '_'; // ie: /tmp/1357581737_
$lfOutfile = $log_stem . 'lftp.log';
$tarOutfile = $log_stem . 'tar.log';
shell_exec('lftp -blah | tee ' . $lfOutfile ' 1>&2' );
shell_exec('tar -blah | tee ' . $tarOutfile ' 1>&2' );
$lfOut = file_get_contents($lfOutfile);
$tarOut = file_get_contetns(tarOutfile);
// remove tmp files
unlink($lfOutfile);
unlink($tarOutfile);
Which will capture a copy of the output to a file before redirecting the output to stderr so you can watch it live.
However, if you want to run this via cron I would recommend against writing anything to stderr that is not an error, otherwise cron will send a warning email every time it is run.
I think the last answer was close:
Either this:
shell_exec('lftp -blah |& tee ' . $lfOutfile );
shell_exec('tar -blah |& tee ' . $tarOutfile );
Or if that still doesn't work try this:
shell_exec('lftp -blah 2>&1 | tee ' . $lfOutfile );
shell_exec('tar -blah 2>&1 | tee ' . $tarOutfile );

Getting FLV duration with php

I have an flv file uploaded to a server. I would like to display its duration in the following format "minutes:seconds". Can anybody help me with this ?
Thank you
There is also a FFMPEG PHP extension ie. apt-get install php5-ffmpeg then
$movie = new ffmepg_movie("path/to/movie.flv");
$duration_in_seconds = $movie->getDuration();
This has worked for me previously. The extension is good for retrieving meta-data and testing if an uploaded file is an FLV, etc.
Here is my code to grab a frame and generate the image from the video...
// get the duration and a random place within that
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}
exec($cmd);
// get the screenshot
exec("ffmpeg -i " . $videoPath . " -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $imageOutput . " 2>&1");
$second variable is random number between 0 and total duration.
and the second exec() is to create a image file from selected frame.
$imageOutput is absolute path location to the generated image.
eg: /home/ariawan/image-generated.jpg
I´d use the getID3 PHP library, written in plain old PHP without any dependencies.
It not only gives you the duration of the .flv movie in seconds, but converts it to minute:seconds format already. Here is a code sample with v. 1.7.9 (latest stable version of getid3):
<?php
// getId3 library uses deprecated eregi_* functions
// which generate errors under PHP 5.3 - so I excluded them
error_reporting(E_ALL ^ E_DEPRECATED);
// just for debugging/sample
header('Content-Type: text/plain');
// include the getid3 base class in order to use the lib
require_once('./lib/getid3.php');
// path to your .flv file
$filename = './sample.flv';
$getID3 = new getID3();
$fileInfo = $getID3->analyze($filename);
// echoes something like 127.8743
print 'Playtime in seconds: ' . $fileInfo['playtime_seconds'];
print chr(10);
// echoes something like: 2:07
print 'Playtime in minute:seconds format: ' . $fileInfo['playtime_string'];
i am using php and ffmpeg to get duration of the video.
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
}
exec($cmd);
print_r() the $time variable to see.
make sure ffmpeg installed on your machine..
hope this will help.

Capture FFMPEG output

I need to read the output from ffmpeg in order to even try the solution to my question from yesterday. This is a separate issue from my problem there, so I made a new question.
How the heck do I get the output from an ffmpeg -i command in PHP?
This is what I've been trying:
<?PHP
error_reporting(E_ALL);
$src = "/var/videos/video1.wmv";
$command = "/usr/bin/ffmpeg -i " . $src;
echo "<B>",$command,"</B><br/>";
$command = escapeshellcmd($command);
echo "backtick:<br/><pre>";
`$command`;
echo "</pre><br/>system:<br/><pre>";
echo system($command);
echo "</pre><br/>shell_exec:<br/><pre>";
echo shell_exec($command);
echo "</pre><br/>passthru:<br/><pre>";
passthru($command);
echo "</pre><br/>exec:<br/><pre>";
$output = array();
exec($command,$output,$status);
foreach($output AS $o)
{
echo $o , "<br/>";
}
echo "</pre><br/>popen:<br/><pre>";
$handle = popen($command,'r');
echo fread($handle,1048576);
pclose($handle);
echo "</pre><br/>";
?>
This is my output:
<B>/usr/bin/ffmpeg -i /var/videos/video1.wmv</B><br/>
backtick:<br/>
<pre></pre><br/>
system:<br/>
<pre></pre><br/>
shell_exec:<br/>
<pre></pre><br/>
passthru:<br/>
<pre></pre><br/>
exec:<br/>
<pre></pre><br/>
popen:<br/>
<pre></pre><br/>
I don't get it. safe_mode is off. There's nothing in disable_functions. The directory is owned by www-data (the apache user on my Ubuntu system). I get a valid status back from exec() and system() and running the same command from the command line give me tons of output. I feel like I must be missing something obvious but I have no idea what it is.
The problem is you catch only stdout and not stderr (see Standard Streams).
Change this line:
$command = "/usr/bin/ffmpeg -i " . $src;
into
$command = "/usr/bin/ffmpeg -i " . $src . " 2>&1";
and give it another try :)
Use ffprobe instead, it's much quicker and supports JSON output.
$output = shell_exec('ffprobe -v quiet -print_format json -show_format -show_streams "path/to/yourfile.ext"');
$parsed = json_decode($output, true);
And you have all your video info in a php array! This is much faster than ffmpeg -i for some reason.
To get output status and output:
exec("ffmpeg -i input.avi output.mp4 2>&1", $output, $returnStatus);
print_r($output);
if($returnStatus === 0){
// success
}
else {
//fail
}
You can use exec and print_r the output...
exec("ffmpeg -i input.avi -vcodec h264 -acodec aac -strict -2 output.mp4 2>&1",$output);
echo "<pre>";
print_r($output);
echo "</pre>";

Categories