FFmpeg output to textfile causing 500 Internal Server Error in PHP - php

I have a PHP script with two FFmpeg commands. The first command combines a list of .ts files into a single .ts file and logs the output information into a textfile. The second command converts the combined .ts file into an mp4 file and logs the output information into a textfile.
Here's the PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$ffmpeg = "/usr/local/bin/ffmpeg";
$vidClips = 'my_vids/vidClipslist.txt';
$combinedFileTs = 'my_vids/combinedFileTs.ts';
$logFileCombine = 'my_vids/logFileCombine.txt';
$logFileConvert = 'my_vids/logFileConvert.txt';
$combinedFileMp4 = 'my_vids/combinedFileMp4.mp4';
shell_exec("$ffmpeg -f concat -safe 0 -i $vidClips -c copy $combinedFileTs 1> $logFileCombine 2>&1");
shell_exec("$ffmpeg -i $combinedFileTs -f mpegts -codec:v mpeg1video -bf 0 -codec:a mp2 -q 12 $combinedFileMp4 1> $logFileConvert 2>&1");
echo "Video finished.";
?>
When I run this, my 500.shtml file message is shown on the page. However, I get no other error message echoed to the page nor are there any errors in my Cpanel error log. I also have a php error log set up, but it doesn't show any errors.
Oddly, both of the FFmpeg commands do what they're supposed to, i.e., the "combinedFileTs.ts", "logFileCombine.txt", "combinedFileMp4.mp4", and "logFileConvert.txt" are all created. It's just that I'm getting the 500 error and the PHP script following the two FFmpeg commands is not being run, i.e., echo "video finished."
When I remove the output directive on the first FFmpeg command, i.e., "1> $logFileCombine 2>&1", everything works. All the files are created, the proceeding PHP script is run, and I don't get the 500 error.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$ffmpeg = "/usr/local/bin/ffmpeg";
$vidClips = 'my_vids/vidClipslist.txt';
$combinedFileTs = 'my_vids/combinedFileTs.ts';
$logFileCombine = 'my_vids/logFileCombine.txt';
$logFileConvert = 'my_vids/logFileConvert.txt';
$combinedFileMp4 = 'my_vids/combinedFileMp4.mp4';
shell_exec("$ffmpeg -f concat -safe 0 -i $vidClips -c copy $combinedFileTs");
shell_exec("$ffmpeg -i $combinedFileTs -f mpegts -codec:v mpeg1video -bf 0 -codec:a mp2 -q 12 $combinedFileMp4 1> $logFileConvert 2>&1");
echo "Video finished.";
?>
Also, the first FFmpeg command takes about 2 min to run. Thinking that the problem was due to the PHP script being killed, I increased the max_execution_time to 600 and the memory_limit to 512M in my php.ini file but that did not fix the problem.
I need both FFmpeg commands to produce an output log textfile and I need the proceeding PHP code to run.

Related

PHP popen() or proc_open() to store/set pid so I can shut down the process in windows I started

I am trying to use proc_open to open an instance of ffmpeg. It works with popen without the title but I can't set the title as it needs another line or something I tried
popen("title $title \r start /min ffmpeg -loglevel quiet -re -i udp://$UIP:$UPort -codec: copy -f mpegts udp://$ip?pkt_size=1316", "r"); didnt work,
my working code is as follows
$title=$_POST['title'];
$ip=$_POST['IPtxt'];
$UIP=$_POST['UDPAddress'];
$UPort=$_POST['UDPPort'];
echo "Starting ffmpeg...\n\n";
popen("start /min ffmpeg -loglevel quiet -re -i udp://$UIP:$UPort -codec: copy -f mpegts udp://$ip?pkt_size=1316", "r");
echo "Done.\n";
I need to store a title or process id so i can stop the process when the stop button is clicked. I've looked at proc_open but I'm not sure the right syntax for it.
popen("start /min ffmpeg
This doesn't run ffmpeg. It runs start.exe - and that runs ffmpeg. Try running ffmpeg directly, then you can use proc_get_status() to get the pid or proc_terminate() to kill it via the PHP resource.

FFmpeg infile path, my domain

I have a file located at html://www.example.com/wp-content/music.mp3
I've tested and confirmed ffmpeg is installed and have run
exec("ffmpeg -help",$output);
I successfully get an output. Now i want to start converting but i cannot locate the file above. I've tried
exec("ffmpeg -i html://www.example.com/wp-content/music.mp3",$output);
exec("ffmpeg -i home/mywebsite/public_html/wp-content/music.mp3",$output);
I get no output for either. ffmpeg is located in /usr/bin/ffmpeg.
How do i solve?
Redirect the error output (stderr) to response (stdout):
<?php
$command = "ffmpeg -i https://www.example/a.mp3 2>&1";
$output = shell_exec($command);
echo $output;

PHP - Converting images to video using ffmpeg

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);

Problems with executing FFMPEG through PHP

Have been trying to execute FFMPEG using a script I have uploaded to my domain
<?php
$output = array();
$result = -1;
exec('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3', $output, $result);
var_dump($output, $result);
?>
The example code says the program should not be returning -1 unless there is an error but I have pointed to the exact path that FFMPEG is stored in.....
When I call 'ffmpeg -ab 320k -i source.wav dest320.mp3' from CentOS it works...
Am lost and have spent hte last few hours trying to work it out.
Thanks
CP
Any time you have an exec that doesn't work in php, you should switch to passthru for debugging.
passthru('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1');
Appending 1 2>&1 to the end, pipes your stderr to stout, and returns any errors you have while running your exec.
It is probably safer to use passthru('/usr/bin/env ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1'); which will get the correct path for ffmpeg without the need to specify and absolute/relative path.
Also worth noting that if you are using Ubuntu since 12.04, the ffmpeg command has been renamed to avconv.

php and ffmpeg 500 internal server error

i'm trying to extract a single frame from a video file using the following php code:
$cmd = 'ffmpeg -i "d:\webs\beta\test\sample2.mp4" -vframes 1 -s 146x82 -f image2 "d:\webs\beta\test.jpg"';
exec($cmd, $rc);
the problem is that i'm getting an 500 internal server error the first time i'm trying to execute the script, but when reloading it works.
so it means when reloading: works / doesn't work / works ..
any ideas what could be wrong?
Try this script.
<?php
$ffmpeg = "/full/path/to/ffmpeg";
$videoFile = "/full/path/to/video.mp4";
$imgOut = "/full/path/to/frame.jpg";
$second = 0;
$cmd = $ffmpeg." -i \"".$videoFile."\" -an -ss ".$second.".001 -y -f mjpeg \"".$imgOut."\" 2>&1";
$feedback = `$cmd`;
?>
i had the same problem exactly.
using proc_open instead of exec and its variants fixed it.
Roey

Categories