I'm using this code to run ffmpeg and return when it succeeded or failed using 2> & 1 and $var. The problem is that I would also like to generate a log.txt with the current ffmpeg process. I know it does using 2> log.txt but how do I use both options at the same time?
<?php
$ffmpeg = '"D:\FFMPEG\bin\ffmpeg.exe"' . " -loglevel verbose -n -i https://URLVIDEO -map p:0 -acodec copy -bsf:a aac_adtstoasc -vcodec copy video.mp4 2>&1";
exec($ffmpeg, $output, $var);
if($var){
echo 'error';
}else{
echo 'success';
}
?>
Use 1> log.txt 2>&1.
stdout is redirected to the log file and stderr is appended to stdout.
If I understood correctly, you want both streams to be logged. If so, another option is to use &> e.g. cmd &> log.txt.
Related
To run one command in the background, it works well.
$cmd = 'ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null &';
exec($cmd, $output, $return_var);
Then I need to sleep for some time before the ffmpeg command. I refer to How do I run multiple background commands in bash in a single line? which works well directly in the bash console.
While not works in the below PHP script, which will return when the bash command finishes running.
$cmd = '(sleep 5; ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null) &';
exec($cmd, $output, $return_var);
I think you also need to handle sleep's stdout/stderr.
( sleep 5 > /dev/null 2>&1; ...; ) &
Or you can put the redirection after ( ... ):
( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
To run multiple commands from on bash command; concatenate using &&.
For instance:
sleep 5 && echo hello && sleep 2 && echo world
This trick can be particularly useful in cron tasks to be able to sequence multiple items within the same minute, as a different sleep value can be used as an offset before starting the command.
When I run exec() through php, I fail to get the output (stderr) into a file.
I've included "2> my_out_put_file.txt" at the end of my command to accomplish this. But when I include this, the command is not executed. However, if I run the command without "2> my_out_put_file.txt" then it works.
The interesting thing is that the whole command, even with "2> my_out_put_file.txt" at the end of the command works if I run directly via shell/promt, but not when I run it through php / apache.
Does not work:
exec("C:/FFmpeg/bin/ffmpeg -i $new_path/$filename_with_ext -f mp4 -vcodec libx264 -preset ultrafast -profile:v main -acodec aac $new_path/$filename.mp4 2> out.txt", $a, $b);
Works:
exec("C:/FFmpeg/bin/ffmpeg -i $new_path/$filename_with_ext -f mp4 -vcodec libx264 -preset ultrafast -profile:v main -acodec aac $new_path/$filename.mp4", $a, $b);
Best regardsNeo
This worked for me:
... 2>&1 >> log.txt
Tested from exec too:
php -r "exec('ffmpeg -version 2>&1 >> log.txt');"
I am using the code bellow to grab an image from a video file and it is working great.
But when i am running the php file that contains the code bellow all system procceses appears, and i mean some infos about the convertion, export etc.
How can i make it run silent?
$stderr = system("ffmpeg -i $videoPath -vframes 1 -an -s 306x173 -ss 03 $finalfilename 2>&1 >/dev/null", $exit_status);
if ($exit_status === 0) {
print 'Done! :)';
} else {
print 'Error... :/';
// $stderr will help you figure out what happened...
}
You should change the order of redirections. Like this:
>/dev/null 2>&1
Or direct everything directly to /dev/null:
>/dev/null 2>/dev/null
You can also use -loglevel quiet as a global option but this will not be helpful if you experience errors.
First of all: sorry! On the other question I made a mistake suggesting you to use system() function, but you clearly need the exec() one.
Furthermore, you might want to suppress any ffmpeg verbose output with -v error to get only error messages on $stderr. You also need to pass -y to avoid getting stuck when $finalfilename already exists. Fixing all together:
exec("ffmpeg -v error -y -i $videoPath -vframes 1 -an -s 306x173 -ss 03 $finalfilename 2>&1 >/dev/null", $stderr, $exit_status);
if ($exit_status === 0) {
print 'Done! :)';
} else {
print 'Error... :/';
// implode("\n", $stderr) will help you figure out what happened...
}
I am good in php but very new to ffmpeg and exec stuffs.Now I have successfully installed ffmpeg via ssh.I referred some stackoverflow here and found below code to make video using several images.
<?php
echo exec('ffmpeg -f image2 -i image%d.jpg video.mpg');
?>
I have image1.jpg and image2.jpg in same folder.I run this php code but nothing happened...where does video.mpg gets saved ? and how to check if exec function ran successfully or how to debug it ?Any help is appreciated.
If it successfully worked, video.mpg got saved in your current working directory. If needed you can change filenames to absolute paths.
To check if exec() function ran successfully you can pass the third argument to get the exit status:
exec('ffmpeg -f image2 -i image%d.jpg video.mpg', $output, $exit_status);
If successful, $exit_status will got 0 value, so:
if ($exit_status === 0) {
// ran fine
} else {
// failed...
}
If it's failling you might want to get errors on $output. You can move STDERR to STDOUT ignoring original STDOUT this way:
exec('ffmpeg -f image2 -i image%d.jpg video.mpg 2>&1 >/dev/null', $output, $exit_status);
Then you could dump $output to see what you got on STDERR:
if ($exit_status !== 0) {
print implode("\n", $output);
}
If the video.mpg file already exists the command will get stuck, you might want to pass -y flag to ffmpeg to overwrite any existing file:
exec('ffmpeg -y -f image2 -i image%d.jpg video.mpg 2>&1 >/dev/null', $output, $exit_status);
I am on windows 7, using a WampServer, and trying to use FFMPEG.
The encoding works, but I can't get a process id back from either the exec()
method nor the shell_exec() methods.
This is my code :
$cmd = C:\ffmpeg\bin\ffmpeg.exe -i "C:\...\4ch.wav" -ar 44100 -ab 48000 -f mp3 -y "C:\...\enc_4ch.mp3"
This what I was trying to do with shell_exec :
shell_exec("nohup $cmd > /dev/null & echo $!");
And with exec :
exec("nohup " . $this->_command . " > /dev/null 2>/dev/null &") ; // returns null
exec("nohup " . $this->_command . " > /dev/null 2>&1 &"); // also returns null
Please let me know what I do wrong, as I'd like later to use the following method to check if my process is still running :
private function is_process_running($proccess_id)
{
exec("ps $proccess_id", $process_state);
return (count($process_state) >= 2);
}
thank you in advace
You are using echo $! to get the process ID, and that specific command is not available on Windows as it is a unix shell command. The procedure link should work: How to get PID from PHP function exec() in Windows?