I have always used:
$pid = exec("/usr/local/bin/php file.php $args > /dev/null & echo \$!");
But I am using an XP virtual machine to develop a web app and I have no idea how to get the pid in windows.
I tried this on a cmd:
C:\\wamp\\bin\\php\\php5.2.9-2\\php.exe "file.php args" > NUL & echo $!
And it gets the file executed, but the output is "$!"
How can I get the pid into the var $pid? (using php)
I'm using Pstools which allows you to create a process in the background and capture it's pid:
// use psexec to start in background, pipe stderr to stdout to capture pid
exec("psexec -d $command 2>&1", $output);
// capture pid on the 6th line
preg_match('/ID (\d+)/', $output[5], $matches);
$pid = $matches[1];
It's a little hacky, but it gets the job done
I landed here thanks to google and decided that this ten years old post needs more info based on How to invoke/start a Process in PHP and kill it using Process ID...
Imagine that you want to execute a command (this example uses ffmpeg to stream a file on a windows system to a rtmp server). You could command something like this:
ffmpeg -re -i D:\wicked_video.mkv -c:v libx264 -preset veryfast -b:v 200k -maxrate 400k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://<IP_OF_RMTP_SERVER>/superawesomestreamkey > d:\demo.txt 2> d:\demoerr.txt
The first part is explained here and the last part of that command outputs to files with that name for logging purposes:
> d:\demo.txt 2> d:\demoerr.txt
So lets assume that that command works. You tested it.
To run that command with php you can execute it with exec but it will take time (another subject, check set_time_limit), its a video handled by ffmpeg via php. Not the way to go but it is happening in this case.
You can run the command in background but what is the pid of that Process?
We want to kill it for some reason and psexec gives only the 'process ID" runned by a user. And there is only one user in this case.
We want multiple processes on the same user.
Here is a example to get the pid of a runned process in php:
// the command could be anything:
// $cmd = 'whoami';
// This is a f* one, The point is: exec is nasty.
// $cmd = 'shutdown -r -t 0'; //
// but this is the ffmpeg example that outputs seperate files for sake
$cmd = 'ffmpeg -re -i D:\wicked_video.mkv -c:v libx264 -preset veryfast -b:v 200k -maxrate 400k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://10.237.1.8/show/streamkey1 > d:\demo.txt 2> d:\demoerr.txt';
// we assume the os is windows, pipe read and write
$descriptorspec = [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
];
// start task in background, when its a recource, you can get Parent process id
if ( $prog = is_resource( proc_open("start /b " . $cmd, $descriptorspec, $pipes ) ) )
{
// Get Parent process Id
$ppid = proc_get_status($prog);
// this is the 'child' pid
$pid = $ppid['pid'];
// use wmic to get the PID
$output = array_filter( explode(" ", shell_exec("wmic process get parentprocessid,processid | find \"$pid\"" ) ) );
array_pop($output);
// if pid exitst this will not be empty
$pid = end($output);
// outputs the PID of the process
echo $pid;
}
The code above should echo the pid of the 'inBackground' runned process.
Note that you need to save the pid to kill it later if it is still running.
Now you can do this to kill the process: (imagine the pid is 1234)
//'F' to Force kill a process
exec("taskkill /pid 1234 /F");
Here is my first post ever here on stackoverflow,
I hope this will help someone. Have a awesome and not lonely christmas ♪♪
You will have to install an extra extension, but found the solution located at Uniformserver's Wiki.
UPDATE
After some searching you might look into tasklist which coincidently, you may be able to use with the PHP exec command to get what you are after.
Here's a somewhat less "hacky" version of SeanDowney's answer.
PsExec returns the PID of the spawned process as its integer exit code. So all you need is this:
<?php
function spawn($script)
{
#exec('psexec -accepteula -d php.exe ' . $script . ' 2>&1', $output, $pid);
return $pid;
} // spawn
echo spawn('phpinfo.php');
?>
The -accepteula argument is needed only the first time you run PsExec, but if you're distributing your program, each user will be running it for the first time, and it doesn't hurt anything to leave it in for each subsequent execution.
PSTools is a quick and easy install (just unzip PSTools somewhere and add its folder to your path), so there's no good reason not to use this method.
Related
I am making a code that downloads a list of m3u8 links by FFMPEG
I had this code:
function FFMPEG($videocode, $dirvideo) {
$ffmpeg = '"D:\FFMPEG\bin\ffmpeg.exe"' . " -hide_banner -loglevel verbose -n -i https://linkplaylist/{$videocode}.m3u8 -map 0:1 -map 0:2 -acodec copy -bsf:a aac_adtstoasc -vcodec copy {$dirvideo} 1> log.txt 2>&1";
exec($ffmpeg, $output, $var);
return $var;
}
$code = FFMPEG('football', 'football.mp4');
if($code){
{ERROR CODE};
}else{
{SUCCESS CODE}
}
Initial problem
And that worked well. I could download the video and know if it was downloaded completely or had some error.
The problem is that this code "hangs" the script in exec () the page is loading until finalize exec () and that of timeout error (shared server) besides being visually strange to the visitor the page loading.
Resolution of the initial problem
After research I think the solution is to put the code execution in the background so I found this code:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "error-output.txt", "a") // stderr is a file to write to
);
function FFMPEG($videocode, $dirvideo) {
$cmd = 'start /B D:\FFMPEG\bin\ffmpeg.exe -y -i "https://linkplaylist/{$videocode}.m3u8" -map p:0 -acodec copy -bsf:a aac_adtstoasc -vcodec copy {$dirvideo}';
proc_close(proc_open ($cmd
,$descriptorspec, $foo));
}
And finally my current problem
And this works fine for the loading and timeout issue, but I can not get a return from when the download was successfully completed.
1 ° proc_open Is this the best solution for my initial problem?
2 ° How can I get a return from when ffmpeg finishes running successfully and the script continues to flow.
Extra Info
I'm trying to build a solution that works on windows (xampp) but my final server is linux.
I'm having the same challenge, and hopefully someone can complete my contribution.
This is only a partial answer that I don't know how to complete for Windows. The trick is in the pipes apparently, per the ffmpeg wiki, but the solution is for Unix:
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 "
I tried using the -nostdin, but it had no effect.
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().
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");
I have a php video uploader script and at the end it initiates an ffmpeg script for the conversion of the uploaded file. It then inserts the video info into a mysql database and in the status field, a value of 0 since the file is being processed.
I was thinking that along with the linux ffmpeg command I could put a delayed process that executes a php file so that after the file is converted and the ffmpeg process is complete it executes the php file and that file has an update statement and can update the video entry in the mysql database under status to 1. To do that though I need to somehow pass the video id into the terminal so it can pass it to the php file...Anyone know how to do this?
$sql = 'INSERT INTO video (id, uploader, video_id, status) VALUES(?,?,?,?)';
$stmt3 =$conn->prepare($sql);
$result=$stmt3->execute(array($id,$username,$video_id,0));
$command = shell_exec('/usr/local/bin/ffmpeg -i /home/g/Desktop/'.$id2.' -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 /home/g/Desktop/'.$id2.'.flv');
Try this:
$command = shell_exec('/usr/local/bin/ffmpeg -i /home/g/Desktop/'.$id2.'' .
'-acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0' .
'/home/g/Desktop/'.$id2.'.flv && /usr/bin/php /home/g/bin/update_db.php ' . $id2);
It says to execute '/usr/bin/php /home/g/bin/update_db.php ' . $id2 if the ffmpeg(1) command succeeds. (It might fail.)
To try this at the shell: true && echo hi and false && echo hi.
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