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);
//@todo: move this to the actual widget and save in video_files table.
$this->params['size'] = variable_get('video_ffmpeg_width', $this->video_width) . 'x' . variable_get('video_ffmpeg_height', $this->video_height);
$this->params['command'] = variable_get('video_ffmpeg_helper_auto_cvr_options', $this->command);
$this->params['cmd_path'] = variable_get('video_transcoder_path', $this->ffmpeg);
$this->params['thumb_command'] = variable_get('video_ffmpeg_thumbnailer_options', $this->thumb_command);
$this->nice = variable_get('video_ffmpeg_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');
}
public function run_command($options) {
// $command = $this->nice . ' ' . $this->params['cmd_path'] . ' ' . $options . ' 2>&1';
$command = $options . ' 2>&1';
watchdog('transcoder', 'Executing command: ' . $command, array(), WATCHDOG_DEBUG);
ob_start();
passthru($command, $command_return);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
public function generate_thumbnails($video) {
global $user;
// Setup our thmbnail path.
$video_thumb_path = variable_get('video_thumb_path', 'videos/thumbnails');
// Get the file system directory.
$schema_thumb_path = file_default_scheme() . '://' . $video_thumb_path . '/' . $video['fid'];
file_prepare_directory($schema_thumb_path, FILE_CREATE_DIRECTORY);
// Total thumbs to generate
$total_thumbs = variable_get('video_thumbs', 5);
$videofile = file_load($video['fid']);
//get the actual video file path from the stream wrappers
$videopath = (drupal_realpath($videofile->uri));
//get the playtime from the current transcoder
$duration = $this->get_playtime($videopath);
$files = NULL;
for ($i = 1; $i <= $total_thumbs; $i++) {
$seek = ($duration / $total_thumbs) * $i - 1; //adding minus one to prevent seek times equaling the last second of the video
$filename = file_munge_filename("video-thumb-for-" . $video['fid'] . "-$i.jpg", '', TRUE);
$thumbfile = $schema_thumb_path . '/' . $filename;
//skip files already exists, this will save ffmpeg traffic
if (!is_file(drupal_realpath($thumbfile))) {
//setup the command to be passed to the transcoder.
$options = $this->params['cmd_path'] . ' ' . t($this->params['thumb_command'], array('!videofile' => $videopath, '!seek' => $seek, '!thumbfile' => drupal_realpath($thumbfile)));
// Generate the thumbnail from the video.
$command_output = $this->run_command($options);
if (!file_exists(drupal_realpath($thumbfile))) {
$error_param = array('%file' => $thumbfile, '%cmd' => $options, '%out' => $command_output);
$error_msg = t("Error generating thumbnail for video: generated file %file does not exist.
Command Executed:
%cmd
Command Output:
%out", $error_param);
// Log the error message.
watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
continue;
}
}
// Begin building the file object.
// @TODO : use file_munge_filename()
$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) {
// This will update our current video status to active.
// $this->change_status($video->vid, VIDEO_RENDERING_ACTIVE);
// Get the converted file object
//we are going to move our video to an "original" folder
//we are going to transcode the video to the "converted" folder
// $pathinfo = pathinfo($video->filepath);
// @TODO This about getting the correct path from the filefield if they active it
$files = file_create_path();
$original = $files . '/videos/original';
$converted = $files . '/videos/converted';
if (!field_file_check_directory($original, FILE_CREATE_DIRECTORY)) {
watchdog('transcoder', 'Video conversion failed. Could not create the directory: ' . $orginal, array(), WATCHDOG_ERROR);
return false;
}
if (!field_file_check_directory($converted, FILE_CREATE_DIRECTORY)) {
watchdog('transcoder', 'Video conversion failed. Could not create the directory: ' . $converted, array(), WATCHDOG_ERROR);
return false;
}
$original = $original . '/' . $video->filename;
//lets move our video and then convert it.
if (file_move($video, $original)) {
// Update our filepath since we moved it
$update = drupal_write_record('files', $video, 'fid');
// process presets
$presets = $video->presets;
$converted_files = array();
foreach ($presets as $name => $preset) {
// reset converted file path
$converted = $files . '/videos/converted';
//update our filename after the move to maintain filename uniqueness.
// $converted = $converted .'/'. pathinfo($video->filepath, PATHINFO_FILENAME) .'.'. $this->video_extension();
$converted = file_create_filename(str_replace(' ', '_', pathinfo($video->filepath, PATHINFO_FILENAME)) . '.' . $preset['extension'], $converted);
//call our transcoder
// $command_output = $this->convert_video($video, $converted);
$dimensions = $this->dimensions($video);
$dimention = explode('x', $dimensions);
if ($this->params['enable_faststart'] && in_array($preset['extension'], array('mov', 'mp4'))) {
$ffmpeg_output = file_directory_temp() . '/' . basename($converted);
} else {
$ffmpeg_output = $converted;
}
// Setup our default command to be run.
foreach ($preset['command'] as $command) {
$command = strtr($command, array(
'!cmd_path' => $this->params['cmd_path'],
'!videofile' => '"' . $video->filepath . '"',
'!audiobitrate' => $preset['audio_bitrate'],
'!width' => $dimention[0],
'!height' => $dimention[1],
'!videobitrate' => $preset['video_bitrate'],
'!convertfile' => '"' . $ffmpeg_output . '"',
));
// print_r($preset['command']);
// die();
// Process our video
// $command_output = $this->run_command($command);
$command_output = $this->run_command($command);
}
if ($ffmpeg_output != $converted && file_exists($ffmpeg_output)) {
// Because the transcoder_interface doesn't allow the run_command() to include the ability to pass
// the command to be execute so we need to fudge the command to run qt-faststart.
$cmd_path = $this->params['cmd_path'];
$this->params['cmd_path'] = $this->params['faststart_cmd'];
$command_output .= $this->run_command($ffmpeg_output . ' ' . $converted, $verbose);
$this->params['cmd_path'] = $cmd_path;
// Delete the temporary output file.
file_delete($ffmpeg_output);
}
//lets check to make sure our file exists, if not error out
if (!file_exists($converted) || !filesize($converted)) {
watchdog('video_conversion', 'Video conversion failed for preset %preset. FFMPEG reported the following output: ' . $command_output, array('%orig' => $video->filepath, '%preset' => $name), WATCHDOG_ERROR);
$this->change_status($video->vid, VIDEO_RENDERING_FAILED);
return FALSE;
}
// Setup our converted video object
$video_info = pathinfo($converted);
//update our converted video
$video->converted = new stdClass();
$video->converted->vid = $video->vid;
$video->converted->filename = $video_info['basename'];
$video->converted->filepath = $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;
}
// Update our video_files table with the converted video information.
$result = db_query("UPDATE {video_files} SET status=%d, completed=%d, data='%s' WHERE vid=%d",
$video->converted->status, $video->converted->completed, serialize($converted_files), $video->converted->vid);
watchdog('video_conversion', 'Successfully converted %orig to %dest', array('%orig' => $video->filepath, '%dest' => $video->converted->filepath), WATCHDOG_INFO);
return TRUE;
} else {
watchdog('video_conversion', 'Cound not move the video to the original folder.', array(), WATCHDOG_ERROR);
$this->change_status($video->vid, VIDEO_RENDERING_FAILED);
return FALSE;
}
}
/**
* Get some information from the video file
*/
public function get_video_info($video) {
static $command_ouput;
if (!empty($command_output))
return $command_output;
$file = escapeshellarg($video);
// Execute the command
$options = $this->params['cmd_path'] . ' -i ' . $file;
$command_output = $this->run_command($options);
return $command_output;
}
/**
* Return the playtime seconds of a video
*/
public function get_playtime($video) {
$ffmpeg_output = $this->get_video_info($video);
// Get playtime
$pattern = '/Duration: ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9])/';
preg_match_all($pattern, $ffmpeg_output, $matches, PREG_PATTERN_ORDER);
$playtime = $matches[1][0];
// ffmpeg return length as 00:00:31.1 Let's get playtime from that
$hmsmm = explode(":", $playtime);
$tmp = explode(".", $hmsmm[2]);
$seconds = $tmp[0];
$hours = $hmsmm[0];
$minutes = $hmsmm[1];
return $seconds + ($hours * 3600) + ($minutes * 60);
}
/*
* Return the dimensions of a video
*/
public function get_dimensions($video) {
$ffmpeg_output = $this->get_video_info($video);
$res = array('width' => 0, 'height' => 0);
// Get dimensions
$regex = ereg('[0-9]?[0-9][0-9][0-9]x[0-9][0-9][0-9][0-9]?', $ffmpeg_output, $regs);
if (isset($regs[0])) {
$dimensions = explode("x", $regs[0]);
$res['width'] = $dimensions[0] ? $dimensions[0] : NULL;
$res['height'] = $dimensions[1] ? $dimensions[1] : NULL;
}
return $res;
}
/**
* Interface Implementations
* @see sites/all/modules/video/includes/transcoder_interface#get_name()
*/
public function get_name() {
return $this->name;
}
/**
* Interface Implementations
* @see sites/all/modules/video/includes/transcoder_interface#get_value()
*/
public function get_value() {
return $this->value;
}
/**
* Interface Implementations
* @see sites/all/modules/video/includes/transcoder_interface#get_help()
*/
public function get_help() {
return l(t('FFMPEG Online Manual'), 'http://www.ffmpeg.org/');
}
/**
* Interface Implementations
* @see sites/all/modules/video/includes/transcoder_interface#admin_settings()
*/
public function admin_settings() {
$form = array();
$form['video_ffmpeg_start'] = array(
'#type' => 'markup',
'#value' => '