PHP - Converting images to video using ffmpeg - php

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

Related

2>&1 and log file at the same time

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.

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;

Hide system message while running ffmpeg

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...
}

PHP exec not executing command

I have used php's exec to execute FFmpeg command but its not woking when I open it in browser. But when i run this php file script in terminal it works fine.And my php safe mode is off. please help me to get it solved. my php code is
<?php
$output=exec("ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
echo $out;
echo $output;
?>
try giving full path where the ffmpeg application is located.
e.g.
/usr/bin/ffmpeg
So your function might look like:
$output=exec("/usr/bin/ffmpeg -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi", $out);
You must check what is the location of "ffmpeg".
I had this problem and it turned out it was Apache's permission on the public directory.
Note: I am running Ubuntu 14 on AWS
After installing FFmpeg I had to change the /var/www/* ownership to www-data.
sudo chown -R www-data:root /var/www
(the www-data is the important part here)
Then I had the following code running, and it works when I access it via URL (Apache)
// test.php
$run = system("/opt/ffmpeg/bin/ffmpeg -i /var/www/html/input.mp4 -vf scale=640:480 /var/www/html/output.mp4 &");
if($run) {
echo "success";
} else {
echo "failed";
}
The /opt/ffmpeg/bin/ffmpeg is where my FFmpeg is running from. Yours might be /usr/bin/ffmpeg or something else. You can locate it by typing locate ffmpeg in the command line and looking through the list it gives you.
The input file was a public .mp4 file and the output.mp4 file was going to the same location.
Run this in your command line: php test.php - works
Run this from your browser: yourwebsite.com/test.php - works
Note that if you are on windows you must use COMMAS. I.E:
$output=exec('"/usr/bin/ffmpeg" -f image2 -i /home/phedra/imgs/image/img%03d.png -r 12 -s 610x489 /home/phedra/imgs/video/out.avi', $out);
Like #Arfeen mentioned in his answer, you should execute the command with the path of ffmpeg, but, the given path in the answer "/usr/bin/ffmpeg" is not always the same.
First locate your ffmpeg by using the command :
which ffmpeg
The result in my case is :
/usr/local/bin/ffmpeg
Then go back to your php code and replace "ffmpeg" in the command by the path of ffmpeg (which is /usr/local/bin/ffmpeg in my case).

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.

Categories