View source
<?php
class video_phpvideotoolkit implements transcoder_interface {
private $name = 'PHP Video Toolkit (FFMPEG)';
private $value = 'video_phpvideotoolkit';
protected $params = array();
protected $audio_bitrate = 64;
protected $video_bitrate = 200;
protected $video_width = 640;
protected $video_height = 480;
protected $ffmpeg = '/usr/bin/ffmpeg';
protected $nice;
protected $video_ext = 'flv';
protected $toolkit;
public function __construct() {
$this->params['audiobitrate'] = variable_get('video_ffmpeg_helper_auto_cvr_audio_bitrate', $this->audio_bitrate);
$this->params['videobitrate'] = variable_get('video_ffmpeg_helper_auto_cvr_video_bitrate', $this->video_bitrate);
$this->params['size'] = variable_get('video_ffmpeg_width', $this->video_width) . 'x' . variable_get('video_ffmpeg_height', $this->video_height);
$this->params['ffmpeg'] = variable_get('video_ffmpeg_path', $this->ffmpeg);
$this->nice = variable_get('video_phpvideotoolkit_nice_enable', FALSE) ? 'nice -n 19 ' : '';
$this->params['videoext'] = variable_get('video_ffmpeg_ext', $this->video_ext);
$this->params['enable_faststart'] = variable_get('video_ffmpeg_enable_faststart', 0);
$this->params['faststart_cmd'] = variable_get('video_ffmpeg_faststart_cmd', '/usr/bin/qt-faststart');
$use_version = 'php5';
if ($use_version == 'php5' && version_compare(PHP_VERSION, '5.0.0', '<')) {
$use_version = 'php4';
}
module_load_include('php', 'video', 'libraries/phpvideotoolkit/phpvideotoolkit.' . $use_version);
$this->toolkit = new PHPVideoToolkit($this->params['ffmpeg'], file_directory_temp() . '/');
}
public function get_codecs() {
$info = $this->toolkit
->getFFmpegInfo(FALSE);
$available_codecs = $info['codecs'];
$codecs = array(
'decode' => array(),
'encode' => array(),
);
foreach ($available_codecs as $key => $value) {
$codecs['encode'][$key] = array();
$codecs['decode'][$key] = array();
foreach ($value as $codec_key => $codec) {
if ($codec['encode']) {
$codecs['encode'][$key][$codec_key] = $codec['fullname'];
}
if ($codec['decode']) {
$codecs['decode'][$key][$codec_key] = $codec['fullname'];
}
}
}
return $codecs;
}
public function generate_thumbnails($video) {
global $user;
$video_thumb_path = variable_get('video_thumb_path', 'videos/thumbnails');
$schema_thumb_path = file_default_scheme() . '://' . $video_thumb_path . '/' . $video['fid'];
file_prepare_directory($schema_thumb_path, FILE_CREATE_DIRECTORY);
$total_thumbs = variable_get('video_thumbs', 5);
$videofile = file_load($video['fid']);
$videopath = drupal_realpath($videofile->uri);
$duration = $this
->get_playtime($videopath);
$files = NULL;
for ($i = 1; $i <= $total_thumbs; $i++) {
$seek = $duration / $total_thumbs * $i - 1;
$filename = file_munge_filename("video-thumb-for-" . $video['fid'] . "-{$i}.jpg", '', TRUE);
$thumbfile = $schema_thumb_path . '/' . $filename;
if (!is_file(drupal_realpath($thumbfile))) {
$result = $this->toolkit
->setInputFile($videopath);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$this->toolkit
->extractFrame($seek);
$result = $this->toolkit
->setOutput(drupal_realpath($schema_thumb_path) . '/', $filename, PHPVideoToolkit::OVERWRITE_EXISTING);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->execute(FALSE, TRUE);
if ($result !== PHPVideoToolkit::RESULT_OK) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
if (!file_exists(drupal_realpath($thumbfile))) {
$error_param = array(
'%file' => $thumbfile,
);
$error_msg = t("Error generating thumbnail for video: generated file %file does not exist.", $error_param);
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
continue;
}
}
$file = new stdClass();
$file->uid = $user->uid;
$file->status = 0;
$file->filename = trim($filename);
$file->uri = $thumbfile;
$file->filemime = file_get_mimetype($filename);
$file->filesize = filesize(drupal_realpath($thumbfile));
$file->timestamp = time();
$files[] = $file;
}
return $files;
}
public function convert_video($video) {
$target = str_replace('original', '', drupal_dirname($video->uri));
$converted_base_dir = $target . 'converted/' . $video->fid;
if (!file_prepare_directory($converted_base_dir, FILE_CREATE_DIRECTORY)) {
watchdog('transcoder', 'Video conversion failed. Could not create the directory: ' . $converted_base_dir, array(), WATCHDOG_ERROR);
return FALSE;
}
$original_video_path = drupal_realpath($video->uri);
$presets = $video->presets;
$converted_files = array();
foreach ($presets as $name => $preset) {
$settings = $preset['settings'];
if (isset($settings['width']) && !empty($settings['width']) && isset($settings['height']) && !empty($settings['height']) && variable_get('video_use_preset_wxh', FALSE)) {
$video->dimensions = $settings['width'] . 'x' . $settings['height'];
}
$converted_filename = file_munge_filename(str_replace(' ', '_', pathinfo($original_video_path, PATHINFO_FILENAME)) . '_' . strtolower($name) . '.' . $settings['video_extension'], $settings['video_extension']);
$converted = $converted_base_dir . '/' . $converted_filename;
$converted_video_path = drupal_realpath($converted);
$dimensions = $this
->dimensions($video);
$dimension = explode('x', $dimensions);
$video_info = $this
->get_video_info($original_video_path);
if ($this->params['enable_faststart'] && in_array($settings['video_extension'], array(
'mov',
'mp4',
))) {
$ffmpeg_output = file_directory_temp() . '/' . basename($converted_video_path);
}
else {
$ffmpeg_output = $converted_video_path;
}
$result = $this->toolkit
->setInputFile($original_video_path);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
if (!empty($settings['max_frame_rate'])) {
$result = $this->toolkit
->setVideoFrameRate($settings['max_frame_rate']);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
}
$result = $this->toolkit
->setVideoCodec($settings['video_codec'], FALSE);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$settings['audio_sample_rate'] = !empty($settings['audio_sample_rate']) ? $settings['audio_sample_rate'] : $video_info['audio']['sample_rate'];
if ($settings['audio_sample_rate'] < 1000) {
$settings['audio_sample_rate'] *= 1000;
}
$settings['audio_sample_rate'] = min($settings['audio_sample_rate'], 44100);
$result = $this->toolkit
->setAudioSampleFrequency($settings['audio_sample_rate']);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->setAudioCodec($settings['audio_codec'], FALSE);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->setAudioChannels($settings['audio_channels']);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
if (empty($settings['audio_bitrate'])) {
$settings['audio_bitrate'] = $this->audio_bitrate;
}
if ($settings['audio_bitrate'] < 1000) {
$settings['audio_bitrate'] *= 1000;
}
$result = $this->toolkit
->setAudioBitRate($settings['audio_bitrate']);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
if (empty($settings['video_bitrate'])) {
$settings['video_bitrate'] = $this->video_bitrate;
}
if ($settings['video_bitrate'] < 1000) {
$settings['video_bitrate'] *= 1000;
}
$result = $this->toolkit
->setVideoBitRate($settings['video_bitrate']);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->setVideoDimensions($dimension[0], $dimension[1]);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->setOutput(dirname($ffmpeg_output) . '/', $converted_filename, PHPVideoToolkit::OVERWRITE_EXISTING);
if (!$result) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$result = $this->toolkit
->execute(FALSE, TRUE);
if ($result !== PHPVideoToolkit::RESULT_OK) {
$error_msg = t($this->toolkit
->getLastError());
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
$this->toolkit
->reset();
continue;
}
$command_output = $this->toolkit
->getLastOutput();
if (!file_exists($converted_video_path) || !filesize($converted_video_path)) {
watchdog('transcoder', 'Video conversion failed for preset %preset. FFMPEG reported the following output: ' . $command_output, array(
'%orig' => $video->uri,
'%preset' => $name,
), WATCHDOG_ERROR);
$this
->change_status($video->vid, VIDEO_RENDERING_FAILED);
return FALSE;
}
$video_info = pathinfo($converted_video_path);
$video->converted = new stdClass();
$video->converted->vid = $video->vid;
$video->converted->filename = $video_info['basename'];
$video->converted->uri = $converted;
$video->converted->filemime = file_get_mimetype($converted);
$video->converted->filesize = filesize($converted);
$video->converted->status = VIDEO_RENDERING_COMPLETE;
$video->converted->preset = $name;
$video->converted->completed = time();
$converted_files[] = $video->converted;
}
db_update('video_files')
->fields(array(
'status' => VIDEO_RENDERING_COMPLETE,
'completed' => time(),
'data' => serialize($converted_files),
))
->condition('vid', $video->converted->vid, '=')
->execute();
watchdog('transcoder', 'Successfully converted %orig to %dest', array(
'%orig' => $video->uri,
'%dest' => $video->converted->uri,
), WATCHDOG_INFO);
return TRUE;
}
public function get_video_info($video) {
$video_info = $this->toolkit
->getFileInfo($video);
return $video_info;
}
public function get_playtime($video) {
$video_info = $this
->get_video_info($video);
return $video_info['duration']['seconds'];
}
public function get_dimensions($video) {
$video_info = $this
->get_video_info($video);
$res = array(
'width' => 0,
'height' => 0,
);
$res['width'] = $video_info['video']['dimensions']['width'] ? $video_info['video']['dimensions']['width'] : NULL;
$res['height'] = $video_info['video']['dimensions']['height'] ? $video_info['video']['dimensions']['height'] : NULL;
return $res;
}
public function get_name() {
return $this->name;
}
public function get_value() {
return $this->value;
}
public function get_help() {
return l(t('PHP Video Toolkit online documentation'), 'http://sourceforge.net/projects/phpvideotoolkit/');
}
public function admin_settings() {
$form = array();
$form = array();
$form['video_ffmpeg_toolkit_start'] = array(
'#type' => 'markup',
'#markup' => '<div id="video_phpvideotoolkit">',
);
$form['phpvideotoolkit']['video_phpvideotoolkit_nice_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the use of <b>nice</b> when calling the command.'),
'#default_value' => variable_get('video_phpvideotoolkit_nice_enable', TRUE),
'#description' => t('The nice command Invokes a command with an altered scheduling priority. This option may not be available on windows machines, so disable it.'),
);
$form['phpvideotoolkit']['transcoders'] = array(
'#type' => 'fieldset',
'#title' => t('Path to Transcoder Executables'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['phpvideotoolkit']['transcoders']['video_ffmpeg_path'] = array(
'#type' => 'textfield',
'#title' => t('FFMPEG'),
'#description' => t('Absolute path to ffmpeg executable. This will provide a token of !ffmpeg to preset commands.'),
'#default_value' => variable_get('video_ffmpeg_path', '/usr/bin/ffmpeg'),
);
$form['video_ffmpeg_toolkit_end'] = array(
'#type' => 'markup',
'#markup' => '</div>',
);
return $form;
}
public function admin_settings_validate($form, &$form_state) {
return;
}
public function create_job($video, $nid) {
return db_insert('video_files')
->fields(array(
'fid' => $video['fid'],
'nid' => $nid,
'status' => VIDEO_RENDERING_PENDING,
'dimensions' => $video['dimensions'],
))
->execute();
}
public function delete_job($video) {
$video = (object) $video;
if (!($video = $this
->load_job($video->fid))) {
return;
}
$converted = unserialize($video->data);
if (!empty($converted)) {
foreach ($converted as $file) {
if (file_exists(drupal_realpath($file->uri))) {
@drupal_unlink($file->uri);
}
}
}
db_delete('video_files')
->condition('fid', $video->fid)
->execute();
}
public function load_job($fid) {
$job = null;
$job = db_query("SELECT f.*, vf.vid, vf.nid, vf.dimensions, vf.data, vf.status as video_status\n FROM {video_files} vf LEFT JOIN {file_managed} f ON vf.fid = f.fid WHERE f.fid=vf.fid AND f.fid = :fid", array(
':fid' => $fid,
))
->fetch();
if (!empty($job)) {
return $job;
}
else {
return FALSE;
}
}
public function load_job_queue() {
$total_videos = variable_get('video_ffmpeg_instances', 5);
$videos = array();
$result = db_query_range('SELECT f.*, vf.vid, vf.nid, vf.dimensions, vf.status as video_status
FROM {video_files} vf LEFT JOIN {file_managed} f ON vf.fid = f.fid
WHERE vf.status = :vstatus AND f.status = :fstatus ORDER BY f.timestamp', 0, $total_videos, array(
':vstatus' => VIDEO_RENDERING_PENDING,
':fstatus' => FILE_STATUS_PERMANENT,
));
foreach ($result as $row) {
$videos[] = $row;
}
return $videos;
}
public function load_completed_job(&$video) {
$file = $this
->load_job($video->fid);
$data = unserialize($file->data);
if (!empty($data)) {
foreach ($data as $value) {
$extension = pathinfo(drupal_realpath($value->uri), PATHINFO_EXTENSION);
$video->files->{$extension}->filename = $value->filename;
$video->files->{$extension}->filepath = $value->uri;
$video->files->{$extension}->filemime = file_get_mimetype($value->uri);
$video->files->{$extension}->url = file_create_url($value->uri);
$video->files->{$extension}->extension = $extension;
$video->player = strtolower($extension);
}
}
else {
return FALSE;
}
}
public function change_status($vid, $status) {
db_update('video_files')
->fields(array(
'status' => $status,
))
->condition('vid', $vid, '=')
->execute();
}
public function dimensions($video) {
$aspect_ratio = _video_aspect_ratio(drupal_realpath($video->uri));
$ratio = $aspect_ratio['ratio'];
$width = $aspect_ratio['width'];
$height = $aspect_ratio['height'];
$wxh = explode('x', $video->dimensions);
$output_width = $wxh[0];
$output_height = $wxh[1];
$output_ratio = number_format($output_width / $output_height, 4);
if ($output_ratio != $ratio && $width && $height) {
$options = array();
if ($ratio < $output_width / $output_height) {
$end_width = $output_height * $ratio;
$end_height = $output_height;
}
else {
$end_height = $output_width / $ratio;
$end_width = $output_width;
}
if ($end_width == $output_width) {
$padding = round($output_height - $end_height);
$pad1 = $pad2 = floor($padding / 2);
if ($pad1 % 2 !== 0) {
$pad1++;
$pad2--;
}
if (variable_get('video_ffmpeg_pad_method', 0)) {
$options[] = '-vf "pad=' . round($output_width) . ':' . round($output_height) . ':0:' . $pad1 . '"';
}
else {
$options[] = '-padtop ' . $pad1;
$options[] = '-padbottom ' . $pad2;
}
}
else {
$padding = round($output_width - $end_width);
$pad1 = $pad2 = floor($padding / 2);
if ($pad1 % 2 !== 0) {
$pad1++;
$pad2--;
}
if (variable_get('video_ffmpeg_pad_method', 0)) {
$options[] = '-vf "pad=' . round($output_width) . ':' . round($output_height) . ':' . $pad1 . ':0"';
}
else {
$options[] = '-padleft ' . $pad1;
$options[] = '-padright ' . $pad2;
}
}
$end_width = round($end_width) % 2 !== 0 ? round($end_width) + 1 : round($end_width);
$end_height = round($end_height) % 2 !== 0 ? round($end_height) + 1 : round($end_height);
array_unshift($options, $end_width . 'x' . $end_height);
return implode(' ', $options);
}
else {
return $video->dimensions;
}
}
}