ffmpeg not creating video from images - php

I have read this Create Video using ffmpeg
Stack Question for the Same
Wiki Page for the same
Still I am not able to get it.
I have written this shell command in PHP
echo $make_movie = "$ffmpeg -framerate 1/5 -i $folder_name/img%03d.png -c:v libx264 -r 25 -pix_fmt yuv420p $folder_name/output.mp4";
This gives output
ffmpeg\bin\ffmpeg.exe -framerate 1/5 -i ankit/img%03d.png -c:v libx264 -r 25 -pix_fmt yuv420p ankit/output.mp4
if(shell_exec($make_movie)){
echo "<br />Movie Created..<br />";
}
else{
echo "<br />Movie Creation Error..<br />";
}
The Output is Movie Creation Error that means the Shell Command is not executing?
Questions:
What is wrong?
For future use, any debugging methods for this?
I ran the same command on cmd and it made the video..!!!

shell_exec is pretty useless when it comes to figuring out why your command failed. use exec() instead:
$last_line = exec($make_movie, $all_output, $exit_code);
var_dump($exit_code);
You'll have to look at ffmpeg's docs to determine what the exit code means

Related

Running PHP remotely via SSH

I have a video encoding server set up on Laravel Forge with nginx. I'm trying to run a testing script to encode a video remotely via SSH, using the LaravelCollective SSH package.
This is my testing script (index.php)
<?php
exec("ffmpeg -I input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 output.mp4 1> output.txt 2>&1");
When I SSH into the server and run the script from the command line it works: the video is encoded; the script is working.
$ php /home/forge/mydomainname.com/public/test/index.php
However, when I run the same command locally in my Laravel app - using the SSH package - it doesn't encode and I receive no output; the browser just returns a white page.
SSH::run('php /home/forge/mydomainname.com/public/test/index.php', function($line) {
echo $line.PHP_EOL;
});
However however, if I open index.php, comment out the FFmpeg command and add some code to check if exec is enabled, it will execute and send output, so I know that the SSH package is actually working and executing the script remotely.
<?php
// exec("ffmpeg -I input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 output.mp4 1> output.txt 2>&1");
if(function_exists('exec')) {
echo "exec is enabled";
} else {
echo "exec is disabled";
}
It will return "exec is enabled" to my browser.
To sum up:
The script will encode video when running it via the command line on the server.
The script will not encode video when running it remotely.
The script will execute when running it remotely.
WTH?
I figured it out.
From within my Laravel app I decided to try echo exec('whoami'); and see if that returned anything to the browser. It did, so I knew exec() was working and I could trigger it via the SSH package.
Then I realized that my ffmpeg encoding command was suppressing output with 2>&1. I removed that and finally saw what was going on: I was receiving a "file not found" error, which was weird because input.mpg is in the same directory as index.php.
This has worked on three other servers, but not on this one created with Forge.
So I added the full path to the input file and voilĂ ! It works!
exec("ffmpeg -i /home/forge/mydomainname.com/public/test/input.mpg -c:v libx264 -preset faster -crf 22 -c:a aac -strict experimental -movflags +faststart -vf scale=360:-1 /home/forge/mydomainname.com/public/test/output.mp4 1> /home/forge/mydomainname.com/public/test/output.txt ");

ffmpegbest way to feed lots of images

I have a php script which is creating a list of images (usually around 400) to feed to ffmpeg via an exec command, it breaks. Is there another way to send multiple images?
Sample code below
$imgs4vid = "'dir/img1.jpg' 'dir/img2.jpg' 'dir/img3.jpg' 'dir/img4.jpg' 'dir/img5.jpg' etc.."
exec("ffmpeg -r 1/5 -pattern_type glob -i ".$imgs4vid." -c:v libx264 -r 30 -pix_fmt yuv420p ".$vid_name.".mp4 2>&1", $output);
var_dump($output);
I have a way to do with -f concat -i (generatoe a list of files and put her)
Seems like a long way to do it though, must be an easier solution?
Thanks
You can use patterns for input (and output) file names:
ffmpeg -i /dir/img%d.jpg -s 640x480 -vcodec mjpeg /tmp/out.avi

php exec() for ffmpeg not working

hai i am using ffmpeg command in php exec() for converting any type of video to flv format the command i'm using i sworking fine in the command prompt but when i run the same commend it returns nothing..actully the output is Array( ) and the the third parameter $result is "1"
i've read similar questions like this on stackoverflow but it's not helping
most of the time i noticed that the issue is in path and the safe mode of php
and i have disabled safe mode using .htaccess the syntax is given below and i am using windows 7 os
the directory to the ffmpeg application is c:\ffmpeg\bin\ffmpeg
the full script and output is given below:
the ffmpeg command:
ffmpeg -i c:\xampp\htdocs\video\original\robot.mp4 -c:v libx264 -ar 44100 -crf 17 c:\xampp\htdocs\video\vids\robot.flv
the php script:
echo "starting ffmpeg...<br/>";
echo exec("ffmpeg -i c:\xampp\htdocs\video\original\robot.mp4 -c:v libx264 -ar 44100 -crf 17 c:\xampp\htdocs\video\vids\robot.flv",$out,$r);
var_dump($out);
echo $r."<br/>";
echo "done...<br/>";
?>
the htaccess for switching off the safe mode:
php_value safe_mode "0"
the output:
starting ffmpeg...
array(0) { } 1
done...
Try the below one instead of using exec(ffmpeg -i 'inputfile' 'outputfile');
$cmd = "ffmpeg -i c:\xampp\htdocs\video\original\robot.mp4 -c:v libx264 -ar 44100 -crf 17 c:\xampp\htdocs\video\vids\robot.flv"
ob_start();
passthru($cmd);
$cmd_retr = ob_get_contents();
print_r($cmd_retr);
<?php
echo exec("cmd /c ffmpeg -i c:\\xampp\htdocs\video\original\robot.mp4 -c:v libx264 -ar 44100 -crf 17 c:\\xampp\htdocs\video\vids\robot.flv",$out,$r");
?>

php library for ffmpeg video processing?

I have installed ffmpeg on my server.
Now I am looking for a php library which can perform ffmpeg functionality, like retrieving video information, converting it to FLV or in any other format, and streaming video.
Please help, Thanks!
You can use below function to convert mp4 video to flv
function mp4toflv($in, $out)
{
//echo $in.' '.$out;
$thumb_stdout;
$errors;
$retval = 0;
// Delete the file if it already exists
if (file_exists($out)) { unlink($out); }
$cmd = "ffmpeg -i $in -ar 22050 -acodec libmp3lame -ab 32K -r 25 -s 320x240 -vcodec flv $out";
//$cmd = "ffmpeg -i $in -b 1024k -s 352x264 -r 25 -acodec copy $out";
//echo escapeshellcmd($cmd);
exec(escapeshellcmd($cmd));
unlink($in);
}
similarly you can also convert other video formats to flv or any other format. Below are some help to convert videos to mp4(h264)
1]. ffmpeg -i input.mp4 -vcodec libx264 output.mp4
2]. ffmpeg input.AVI -vcodec libx264 -sameq output.mp4
option 1 can use for :- (mp4,mov,flv)
option 2 can user for :- (3gp,avi,mp4,mov,flv)
execute above commands using "exec(escapeshellcmd($cmd))" where $cmd will be any from above two options.
Hope this will help someone :)
We have been using ffmpeg-php without any major problems, just make sure you are using supported ffmpeg version. If you need any special behaviour, you can always additionally wrap it using exec().

How to convert videos with ffmpeg

I want to convert a video from one format to another using ffmpeg. I try lots of code but it does not convert the video.
For example:
exec("ffmpeg -i mickey.flv -ar 22050
-ab 32 -f avi -s 320x240 mickey.avi ");
This code does not convert the video, it does not show any error, it is loading continuously.
It is impossible to pinpoint the problem, because you are executing an external application and any number of things could be going wrong in the process.
See this question for a number of very good hints to debug exec() commands.
Display FFMPEG Error Output:
Command Line:
$command = "ffmpeg -i mickey.flv -ar 22050 -ab 32 -f avi -s 320x240 mickey.avi ";
exec($command . ' 2>&1', $output);
print_r($output);

Categories