php library for ffmpeg video processing? - php

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

Related

Wordpress upload video convert MP4 format

Create custom video convert plugin - MP4 format
Bellow upload code work fine but how to convert MP4 format after
insert media library.
media_handle_upload('upload_video_file', 0)
exec("ffmpeg -i $fullsize_path -ar 22050 -ab 32 -f mp4 -s 320x240 $value");
<form method="post" enctype="multipart/form-data">
<input type='file' id='upload_video_file' name='upload_video_file'></input>
<?php submit_button('Upload') ?>
</form>
If you are using ffmpeg to convert video,first make sure its installed properly on your server and running.
Check ffmpeg installed or not on your server.
$ffmpeg = shell_exec('which ffmpeg'));
$ffmpeg = shell_exec('type -P ffmpeg');
If $ffmpeg return empty then it means its not installed on your server, if its return absolute path it means installed on your server.
Then you can execute exec commond Just like below
exec("ffmpeg -i {input}.mov -vcodec h264 -acodec aac -strict -2 {output}.mp4");
-vcodec parameter to specify the encoding format to be used for the output video.
-acodec codec (input/output)
Set the audio codec. This is an alias for -codec:a
For more commond check ffmpeg link https://ffmpeg.org/ffmpeg.html

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

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.

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