How to convert videos with ffmpeg - php

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

Related

FFMPEG and validating command

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

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

ffmpeg-php and shell_exec

I'm using ffmpeg-php for uploading videos and converting videos from
"ogv", "mp4", "avi", "flv", "ogg",
"mpg", "wmv", "asf" and "mov"
formats to
"flv" , "ogg" and "flv" formats.
I'm doing this by:
$command1='ffmpeg -i '.$Source." -ar 44100 -ab 128k -s wvga -aspect 4:3 -f FLV -y ".$dest;
$command2="ffmpeg -i ".$Source." -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -threads 10 -s wvga -aspect 4:3 -f mp4 -y ".$dest;
$command3="ffmpeg -i ".$Source." -acodec libvorbis -ab 128k -ac 2 -vcodec libtheora -threads 10 -s wvga -aspect 4:3 -f ogg -y ".$dest;
#shell_exec($command1.";".$command2.";".$command3);
When applying those commands on .ogv videos it works fine but when applying on .MOV it does not work and no errors displays although when I paste the command into the terminal it works fine.
Hint: Script is running by cron job.
is there any suggestions for this problem?
Most likely the script is running out of time before the conversion takes place.
Increase the execution time of your scripts.
Although its not a recommend solution for production. You might want to run background processes to do the conversion and use the PHP script just to upload the video.
What happens when you run the problem part outside of a crontab?
are you saving the output of your crontab entry to files? i.e.
59 12 1 1 * { myPhpScript ; } > /tmp/myPhpScript.log 2>&1` ...
( The semicolon preceding the closing '}' is critical)
This captures any stdout or stderr messages to a logfile.
I hope this helps.
P.S. as you appear to be a newish user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

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 take pictures of the video through mplayer or ffmpeg to php?

How to take pictures of the video through mplayer or ffmpeg to php?
try,
exec("ffmpeg -i $video_file_path -an
-y -f mjpeg -ss 00:02:00 -vframes 1 $image_path")
Assuming ffmpeg is installed on your server, you can use the following code to output the frame at exactly 2 minutes to a JPEG file:
function vidtojpeg($video_filename, $dimensions) {
exec("ffmpeg -i $video_filename -an -ss 00:01:59 -t 00:00:01 -r 1 -y -s $dimensions video%d.jpg");
}
In this function, the $video_filename parameter is self-explanatory. The $dimensions parameter accepts width and height of the outputted images in this format: WIDTHxHEIGHT. For example: 320x480 would be an acceptable parameter.
Converting the video to frames and getting the required frames based on timings can help. Try this : ffmpeg -i video.flv -r 25 -vcodec png -pix_fmt rgb32 %d.png
You can manipulate the formats and bitrate(-r) for getting the required frame in proper format.

Categories