FFMPEG and validating command - php

I'm using ffmpeg with PHP to compress the size of some videos I upload on my website, but I also want to capture the error If something goes wrong during the process when executing the command.
I have read that there's no way to capture the error, but I'm not sure if that's correct.
I hope someone will help me out with this doubt I have.
Here's my code:
$command="$ffmpeg -i $video['tmp_name'] -i pics/watermark.png -s $resolution -filter_complex [1:v][0:v]scale2ref=(127/60)*ih/10/sar:ih/10[wm][base];[base][wm]overlay=10:10 -pix_fmt yuv420p -c:a copy videos/$video_name";
shell_exec($command);

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

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

how to convert video from one format to another using php

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

How can I know the error in command in an exec function?

I am using a ffmpeg command to cut a video with exec function in php. But it is not creating a video file and doesn't show any errors. I used exec as below
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv");
Can I get any help to know what is the error happening here?
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv",$output);
I tried it also. But I didn't get any error message in variable $output
As a general rule, you first need to check the return value: non-zero values indicate there has been an error (given that the author of the program you are running adheres to the standard). This value is captured in exec()'s third argument.
In second place, many programs do not send error messages to the standard output. Instead, they use the standard error. In this case, it seems safe to just redirect the latter to the former:
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv 2>&1", $output, $return_value);

Categories