php exec() for ffmpeg not working - php

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

Related

FFMPEG command in PHP shell_exec is too slow for a 300Mo mp4

I have a web site where user upload videos.
When a video is uploaded I shell_exec 3 ffmpeg commands:
Generate preview:
$cmd = "ffmpeg -ss 1 -i ".$filePath." -vf \"scale='if(gt(dar,360/202),202*dar,360)':'if(gt(dar,360/202),202,360/dar)',setsar=1,crop=360:202\" -frames:v 1 ".$thumbnail;
Compress the video:
$cmd = "ffmpeg -i ".$filePath." -c:v libx264 -crf 23 -preset veryfast -c:a aac -ab 128k -movflags faststart ".$wmoutput;
Add watermark:
$cmd = 'ffmpeg -i '.$wmoutput.' -i new/watermark.png -filter_complex "overlay=10:10" '.$finaloutput;
The problem is that these 3 commands take a lot of time to finish and sometimes it goes over Maximum execution time specialy for video files that are bigger than 300Mo. I can just expand the Maximum execution time but I don't know how many seconds I have to set.
Is there a way to make the scripts above execute faster without having to change the Maximum execution time?
Combine Compress the video and add watermark into one command:
ffmpeg -i input.mp4 -i new/watermark.png -filter_complex "overlay=10:10" -c:v libx264 -crf 23 -preset veryfast -c:a aac -ab 128k -movflags faststart output.mp4
Should be significantly faster and you avoid yet another instance of generation loss.

ffmpeg command is not run when I hit using exec function in php

I have to convert some video to "h264" using FFmpeg.When I hit the below command as a cloud user with ssh login it converts successful.
ffmpeg -i /var/www/media/photos/video_demo/55291482115655.MP4 -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:300 -threads 0 -pass 1 -codec:a libfdk_aac -b:a 500k -f mp4 /var/www/media/photos/video_demo/5888.MP4.
But when I run this command using PHP it gives me an error.
$cmd = "ffmpeg -i /var/www/media/photos/video_demo/55291482115655.MP4 -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:300 -threads 0 -pass 1 -codec:a libfdk_aac -b:a 500k -f mp4 /var/www/media/photos/video_demo/85493.MP4 ";
exec($cmd .' 2>&1', $outputAndErrors, $return_value);
php error: Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
I also followed this link similar to my problem but still not get solution for this problem,In my server there is 3 FFmpeg installed.

executing exec in cakephp for ffmpeg not converting video

I am trying to run this in cakephp
function test()
{
$mainDirectoryPath = WWW_ROOT . "media";
$filePath = "userId_10/vid_1444387525.mp4";
$newPath = "userId_10/Output.mp4";
$value = exec("ffmpeg -i $filePath -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
$this->jsonOutput($value);
}
i am getting nothing $value is empty,
however in shell if i run this as shell_exec() in a php file it works.
what is wrong above
UPDATE:
Ok now i have created a file with following code
<?php
$mainFile = $_REQUEST['mainFile'];
$newPath = $_REQUEST['newFile'];
echo shell_exec("ffmpeg -i $mainFile -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
#unlink($mainFile);
?>
If i run this with mainFile and newFile it works.
However when i run it in function as
$val = "php video.php?mainFile=$filePath&newFile=$newPath > /dev/null 2>&1 &";
exec($val);
Nothing happens.
Check php.ini file and disable_functions have not exec comamnd.
For security reason site admin disabled exec command in php.ini.
Remove exec from disable_functions in php.ini file.

Why does FFMPEG work in terminal but not in php with exec()?

I am trying to get FFMPEG to work in php. I just installed ffmpeg and x264 and ran the following command in my terminal:
$command = 'ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 /home/gman/Desktop/newvideo.flv
It worked perfectly and created a new flv video from the inital video, just like I wanted.
Now when I try the same thing in php, nothing happens...
$safe_path = escapeshellarg("/home/gman/Desktop/newvideo.flv");
$command = 'ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 ' . $safe_path;
exec($command);
Anyone have any ideas? Can I somehow see what exec is doing and see some sort of output? Would appreciate it.
Usually when you are calling ffmpeg in an exec you need to put in the absolute path to ffmpeg eg:
$safe_path = escapeshellarg("/home/gman/Desktop/newvideo.flv");
$command = '/usr/local/bin/ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 ' . $safe_path;
exec($command);
http://blog.codyjung.com/2011/05/29/problems-with-lampp-and-exec/
My solution was to simply copy the ones from /usr/lib/i386-linux-gnu and overwrite the LAMPP ones. Could that cause problems later? Maybe, but I guess we’ll deal with that when it shows up.

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().

Categories