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>";
Related
here is my code for FFMPEG
$mp3path=realpath("1.mp3");
$cmd = "/usr/include/ffmpeg -i $mp3path -ab 128 /home/swiftfa1/public_html/output.mp3 2>&1";
exec($cmd, $output, $value);
echo '<pre>'; print_r($output); echo '</pre>';
echo '<pre>'; print_r($value); echo '</pre>';
exit;
and here is my result:
Array
(
[0] => sh: /usr/include/ffmpeg: Is a directory
)
126
I used whereis for ffmpeg path and searched a lot but can't solve my issue. any command returns 126!
I'm working on generating thumbnail from uploaded video in Codeigniter for this I'm using ffmpeg but when I'm trying to generate the thumbnail I'm getting this error
ffmpeg: symbol lookup error: /usr/lib/x86_64-linux-gnu/libass.so.5:
undefined symbol: FT_Outline_EmboldenXY
I've already installed ffmpeg on my system, currently I'm trying to print the result.
This is my code to generate thumbnail
if ( $this->upload->do_upload( 'userFile' ) ) {
$videoData = $this->upload->data();
/*for thumbnail of video*/
echo "</pre>";
exec( "/tagittty/ffmpeg.exe" );
$videoFile = $_FILES['userFile']['name'];
exec( 'ffmpeg -i' . $videoFile . ' -r 1 -ss 00:00:50 -vf scale=1024:-1 -vframes 1 output.jpeg 2>&1', $result );
echo "<pre>";
print_r( $result );
echo "</pre>";
die();
}
Response I got after
ldd -r /usr/lib/x86_64-linux-gnu/libass.so.5
Thanks for the help. :)
Is there a way to run a shell script from PHP and echo the results after progress is completed?
Here is my shell script:
(Its multilines - a few commands that have to be ran one after the other. )
cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save
npm start
here's my currently functioning PHP script. It only outputs some of the progress and only outputs it when complete. I need a live preview of the progress.
<?php
echo "loaded<br><br>";
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
Here is another approach. It uses redirection to output the results and then file_get_contents() to read the output.
<?php
echo "loaded<br><br>";
$cmd = 'npm start';
$run = $cmd . ' > result.txt 2> errors.txt';
$output = shell_exec($run);
$results = file_get_contents('result.txt');
if (strlen(file_get_contents('errors.txt')) != 0) {
$results .= file_get_contents('errors.txt');
}
echo "<pre>$results</pre>";
?>
I guess, ob_flush() would work:
<?php
echo "loaded<br><br>";
ob_flush();
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
ob_flush();
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
But I don't understand why you echo HTML-tags when you run that script on the console... Hope I got you right
code works fine but if the filename has a single qoute just as "Britney's video.mp4" it does not work.
$ffmpeg = "/usb/bin/local/ffmpeg";
$videos = "/videos/*.mp4";
$ouput_path = "/videos/thumbnails/";
foreach(glob($videos) as $video_file){
$lfilename = basename($video_file);
$filename = basename($video_file, ".mp4");
$thumbnail = $ouput_path.$filename.'.jpg';
if (!file_exists($filename)) {
#$thumbnail = str_replace("'", "%27", $thumbnail);
exec("/usr/local/bin/ffmpeg -i '$video_file' -an -y -f mjpeg -ss 00:00:30 -vframes 1 '$thumbnail'");
}
echo "<a href='$lfilename'>$filename<img src='thumbnails/$filename.jpg' width='350'>";
i got it working but not using something overly complicated.
thanks all
$comd = "/usr/local/bin/ffmpeg -i \"$video_file\" -y -f mjpeg -ss 00:00:30 -vframes 1 \"$thumbnail\" 2>&1"; shell_exec($comd);
shell_exec($comd);
As suggested in the comments, you can just wrap all your shell-command String within the escapeshellarg() as shown below.
<?php
foreach(glob($videos) as $video_file){
$lfilename = basename($video_file);
$filename = basename($video_file, ".mp4");
$thumbnail = $ouput_path.$filename.'.jpg';
if (!file_exists($filename)) {
$cmd = "/usr/local/bin/ffmpeg -i ";
$cmd .= $video_file . " -an -y -f mjpeg -ss 00:00:30 ";
$cmd .= "-vframes 1 " . $thumbnail;
exec( escapeshellarg($cmd) );
}
How do I know there is error occured in ffmpeg command? and How do I get the error in my php?
below is my code
<?php
$cmd = "$ffmpeg -i $vid -an -ss $getfromsecond -s $size -vframes 1 $imageFile";
if(!shell_exec($cmd)){
echo "Thumbnail created" . "<br/>";
}else{
echo "Error Creating thumbnail". "<br/>";
}
?>
I am not sure if above approach is right.I have also tried below code
exec($cmd, $output, $return);
echo '$output :' ; print_r($output); echo "<br/>";echo '$return :' . $return . "<br/>";exit;
but in server it is just showing out put as
$output :Array ( )
$return127
I don't understand what is that error no, How to know if error has occurred and return the ffmpeg error no and ffmpeg error text , in php.
use it
exec($cmd,$test);
then check
if(file_exists($image))
{
echo 'suc';
}