function zencoder_auto_thumbnail in Video 6.3
Generates a thumbnail from the video file Implementing hook_auto_thumbnail on inc
Parameters
$vidfile: object with element information
Return value
a drupal file objects
File
- plugins/
zencoder.inc, line 134 - Provide a api for video conversion and auto thumbnailing using ffmpeg.
Code
function zencoder_auto_thumbnail($vidfile) {
global $user;
$uploaded_file = $vidfile;
$fid = $uploaded_file["fid"];
// are we debugging?
// escape the filename for safety
$videofile = escapeshellarg($uploaded_file['filepath']);
$thumb_path = variable_get('video_thumb_path', 'video_thumbs');
//files will save in files/video_thumbs/#fileId folder
$tmp = file_directory_path() . '/' . $thumb_path . '/' . $fid;
// Ensure the destination directory exists and is writable.
$directories = explode('/', $tmp);
// array_pop($directories); // Remove the file itself.
// Get the file system directory.
$file_system = file_directory_path();
foreach ($directories as $directory) {
$full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;
// Don't check directories outside the file system path.
if (strpos($full_path, $file_system) === 0) {
field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
}
}
$count = variable_get('no_of_video_thumbs', 5);
$duration = zencoder_auto_playtime($vidfile);
$files = NULL;
for ($i = 1; $i <= $count; $i++) {
// get ffmpeg configurations
$seek = $duration / $count * $i;
$thumbfile = $tmp . "/video-thumb-for-{$fid}-{$i}.png";
//skip files already exists, this will save ffmpeg traffic
if (!is_file($thumbfile)) {
$tnail = variable_get('video_transcoder_path', '/usr/bin/ffmpeg');
$options = preg_replace(array(
'/%videofile/',
'/%thumbfile/',
'/%seek/',
), array(
$videofile,
$thumbfile,
$seek,
), variable_get('video_zencoder_thumbnailer_options', '-i %videofile -an -y -f mjpeg -ss %seek -vframes 1 %thumbfile'));
// $options = preg_replace(array('/%videofile/', '/%tmp/', '/%id/', '/%interval/'), array($videofile, $tmp, $i, ($duration/$count)), variable_get('video_image_thumbnailer_options', '-ss %id*%interval -i %videofile -vframes 1 %thumbfile'));
// ffmpeg -ss $i*$interval -i intro.mov -vframes 1 -s 320x240 thumb_$i.jpg
//ffmpeg -i superstunt_8uiarzrh.mp4 -r 0.1 -ss 00:00:5 -f image2 img/images%02d.png
////ffmpeg -i superstunt_8uiarzrh.mp4 -r 0.05 -ss 00:00:5 -f image2 img/images%1d.jpg
// executes the command
$command = "{$tnail} {$options}";
ob_start();
passthru($command . " 2>&1", $tnail_return);
$tnail_output = ob_get_contents();
ob_end_clean();
if (!file_exists($thumbfile)) {
$error_param = array(
'%file' => $thumbfile,
'%cmd' => $command,
'%out' => $tnail_output,
);
$error_msg = t("error generating thumbnail for video: generated file %file does not exist.<br />Command Executed:<br />%cmd<br />Command Output:<br />%out", $error_param);
// let's log this
watchdog('video_ffmpeg', $error_msg, array(), WATCHDOG_ERROR);
}
}
// Begin building file object.
//TODO : use file_munge_filename()
$file = new stdClass();
$file->uid = $user->uid;
$file->status = FILE_STATUS_TEMPORARY;
$file->filename = trim("video-thumb-for-{$fid}-{$i}.png");
$file->filepath = $thumbfile;
$file->filemime = file_get_mimetype("video-thumb-for-{$fid}-{$i}.png");
$file->filesize = filesize($thumbfile);
$file->timestamp = time();
$files[] = $file;
}
return $files;
}