CakePHP Video Encoder Component
Introduction
This is a component to convert and encode videos in your CakePHP application. This was extracted from a live social networking project that I built. Currently it is just optimized to encode and convert flash video but it is very easy to modify the code to your needs.
Requirements
- CakePHP 1.2+
- ffmpeg
- flvtool2
How to Use
Place the component inside of your components directory and then include it inside of any controller that you want to encode videos. Ensure that you set the correct paths to you installations of ffmpeg and flvtool2.
Example
The following is a typical example that you might use after uploading a video from a form.
<?php
function add () {
// You probably will upload the video from a form
// Let's say all of that is said and done and you have your video
// uploaded and have it's path stored in the variable $path
// Let's also say that you have a variable set with the output path
// that the converted video will be stored named $out_path
// The first this we need to do is convert the video
$this->VideoEncoder->convert_video($path, $out_path, 480, 360);
// Then we need to set the buffer on the converted video
$this->VideoEncoder->set_buffering($out_path);
// We can now get some information back about the converted video that
// can be stored in a database for further use
$duration = $this->VideoEncoder->get_duration($out_path);
$filesize = $this->VideoEncoder->get_filesize($out_path);
// We can also grab a screenshot from the video as a jpeg
// and store it for future use.
$this->VideoEncoder->grab_image($out_path, $path_to_save_image);
// Finally we can delete the original video
$this->VideoEncoder->remove_uploaded_video($path);
}
?>
Code
<?php
/*
* Video Encoder CakePHP Component
* Copyright (c) 2009 Andrew Weir
* http://andrw.new
*
*
* @author Andrew Weir <andru.weir@gmail.com>
* @version 1.0
* @license MIT
*/
class VideoEncoderComponent extends Object {
/**
* Everything in this method can be placed into a global configuration
* file that is called at bootstrap/runtime.
**/
function __construct () {
// ffmpeg path
Configure::write('Video.ffmpeg_path', '/usr/bin/ffmpeg');
// flvtool2 path
Configure::write('Video.flvtool2_path', '/bin/flvtool2');
// Bitrate of audio (valid vaues are 16,32,64)
Configure::write('Video.bitrate', 64);
// Sampling rate (valid values are 11025, 22050, 44100)
Configure::write('Video.samprate', 44100);
}
function convert_video ($in, $out, $width = 480, $height = 360, $optimized = false) {
if ($optimized == false) {
$command = Configure::read('Video.ffmpeg_path') . " -i {$in} -y -s {$width}x{$height} -r 30 -b 500 -ar " . Configure::read('Video.samprate') . " -ab " . Configure::read('Video.bitrate') . " {$out}";
} else {
$command = Configure::read('Video.ffmpeg_path') . " -i {$in} -b 256k -ac 1 -ar 44100 {$out}";
}
echo exec($command);
}
function set_buffering ($in) {
$command = Configure::read('Video.flvtool2_path') . " -U {$in}";
shell_exec($command);
}
function grab_image ($in, $out) {
$command = Configure::read('Video.ffmpeg_path') . " -y -i {$in} -f mjpeg -t 0.003 {$out}";
shell_exec($command);
}
function get_duration ($in) {
$command = Configure::read('Video.ffmpeg_path') . " -i {$in} 2>&1 | grep \"Duration\" | cut -d ' ' -f 4 | sed s/,//";
return shell_exec($command);
}
function get_filesize ($in) {
return filesize($in);
}
function remove_uploaded_video ($in) {
unlink($in);
}
}
If you have any questions or comments drop me a line below and I will try to get back to you as soon as possible.
Last Updated: November 18th 2009, 11:13pm
Download: CakePHP Video Encoder Component


6 Comments
Being a newbie to programmatic video conversion, and having no experience with ffmpeg and flvtool2 (aside from checking out their respective home pages and feeling confused), can I assume that this component will convert misc. video types (i.e., WMV, MOV, MP4, MPG, etc.) into FLV format?
In other words, I'm hoping for something like the following:
$this->VideoEncoder->convert_video('/path/video.wmv', '/another_path/video.flv', 480, 360);?
Sorry if this is a dumb question...But the answer isn't so obvious to me without testing your code first. I may give it a go anyway (and report back here), but perhaps you could clarify this point as well for other "dumb" people like myself.
Thanks again!
I'm using this component and it works like a charm. But I have one question, when I want to grab an image for the video, how can I setup on which frame I want to grab the image in the grab_image function? I try to change the 0.03 to different value, it doesn't work...
Could you give me an example of if I want to grab it from the 5's second, how can I do that?
Thousand thanks!!
-David