Thursday, December 30, 2010

ffmpeg

how to upload video and create thumbnail and store in database with ffmpeg

This mod requires ffmpeg pre-installed and working on your server. Please DO NOT ask for support on installing or configuring ffmpeg. If you don't have ffmpeg or can't install ffmpeg on your server then this mod is not meant for you.

What will this mod do?
This mod will create thumbnail from the uploaded video file automatically on the fly. Thumbnails will be placed in the directory where the videos are uploaded/batch added. Though ffmpeg supports many video formats but i am unsure about wmv. File formats which i tested were avi, mpg, asf, flv.

Edit include/picmgmt.inc.php

Add

Code:
/**
* Function to create video thumbnail using ffmpeg
*/
function create_movie_thumb($src_file)
{
global $CONFIG, $ERROR;

$CONFIG['ffmpeg_path'] = '/usr/local/bin/'; // Change the path according to your server.

$src_file = $src_file;
$dest_file = $CONFIG['fullpath']."edit/".md5(uniqid(time()))."%d.jpg";

if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
// get the basedir, remove '/include'
$cur_dir = substr(dirname(__FILE__), 0, -8);
$src_file = '"' . $cur_dir . '\\' . strtr($src_file, '/', '\\') . '"';
$ff_dest_file = '"' . $cur_dir . '\\' . strtr($dest_file, '/', '\\') . '"';
} else {
$src_file = escapeshellarg($src_file);
$ff_dest_file = escapeshellarg($dest_file);
}

$output = array();

if (eregi("win",$_ENV['OS'])) {
// Command to create video thumb
$cmd = "\"".str_replace("\\","/", $CONFIG['ffmpeg_path'])."ffmpeg\" -i ".str_replace("\\","/" ,$src_file )." -an -ss 00:00:05 -r 1 -vframes 1 -y ".str_replace("\\","/" ,$ff_dest_file);
exec ("\"$cmd\"", $output, $retval);
} else {
// Command to create video thumb
$cmd = "{$CONFIG['ffmpeg_path']}ffmpeg -i $src_file -an -ss 00:00:05 -r 1 -vframes 1 -y $ff_dest_file";
exec ($cmd, $output, $retval);
}


if ($retval) {
$ERROR = "Error executing FFmpeg - Return value: $retval";
if ($CONFIG['debug_mode']) {
// Re-execute the command with the backtick operator in order to get all outputs
// will not work if safe mode is enabled
$output = `$cmd 2>&1`;
$ERROR .= "

Cmd line :
" . nl2br(htmlspecialchars($cmd)) . "
";
$ERROR .= "

The ffmpeg program said:
";
$ERROR .= nl2br(htmlspecialchars($output));
$ERROR .= "
";
}
@unlink($dest_file);
return false;
}

$return = str_replace("%d", "1", $dest_file);
@chmod($return, octdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled
return $return;
}

just before

Code:
// Add a picture to an album
function add_picture($aid, $filepath, $filename, $position = 0, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '', $iwidth = 0, $iheight = 0)

In above code change $CONFIG['ffmpeg_path'] value to where ffmpeg is installed on your server.

Add

Code:
} elseif (is_movie($image)) {
preg_match("/(.+)\.(.*?)\Z/", $filename, $matches);
$thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $matches[1].".jpg";
$videoThumb = create_movie_thumb($image);
if ($videoThumb) {
if (!resize_image($videoThumb, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use'])) {
return false;
}
@unlink($videoThumb);
$imagesize = getimagesize($thumb);
} else {
return false;
}

just before

Code:
} else {
$imagesize[0] = $iwidth;
$imagesize[1] = $iheight;
}

That's it, only one file to be modified.


Few Notes
The mod works best on linux servers
If you have Dreamhost as your webhost then this might help you
The dreamhost installation way might work on other shared servers too but not sure. Give it a try.
If you have a dedicated server then ffmpeg will not be that difficult for your webhost to install



2 comments:

Mirza said...

i think this is very helpful for you..

Unknown said...

How about apache servers n MySQL in Windows doesn't it worked...??