From 0c8e7ae689eed6291bb2061c9c75e3057b230339 Mon Sep 17 00:00:00 2001 From: Mohamed Mujahid Date: Tue, 6 Jul 2010 17:04:02 +0000 Subject: merging changes from DRUPAL-6--4 --- transcoders/video_ffmpeg.inc | 312 +++++++++++++++++++++++++++++++++++ transcoders/video_ffmpeg_wrapper.inc | 243 +++++++++++++++++++++++++++ 2 files changed, 555 insertions(+) create mode 100644 transcoders/video_ffmpeg.inc create mode 100644 transcoders/video_ffmpeg_wrapper.inc (limited to 'transcoders') diff --git a/transcoders/video_ffmpeg.inc b/transcoders/video_ffmpeg.inc new file mode 100644 index 0000000..7a52dad --- /dev/null +++ b/transcoders/video_ffmpeg.inc @@ -0,0 +1,312 @@ +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', TRUE) ? 'nice -n 19' : ''; + $this->params['videoext'] = variable_get('video_ffmpeg_ext', $this->video_ext); + } + + public function run_command($options) { + $command = $this->nice .' '. $this->params['cmd_path'].' '.$options.' 2>&1'; + watchdog('video_ffmpeg', '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', 'video_thumbs'); + $final_thumb_path = file_directory_path(). '/' . $video_thumb_path . '/' . $video['fid']; + + // Ensure the destination directory exists and is writable. + $directories = explode('/', $final_thumb_path); + // 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); + } + } + + // Total thumbs to generate + $total_thumbs = variable_get('no_of_video_thumbs', 5); + $videofile = escapeshellarg($video['filepath']); + //get the playtime from the current transcoder + $duration = $this->get_playtime($video['filepath']); + + $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 = "/video-thumb-for-$fid-$i.jpg"; + $thumbfile = $final_thumb_path . $filename; + //skip files already exists, this will save ffmpeg traffic + if (!is_file($thumbfile)) { + //setup the command to be passed to the transcoder. + $options = t($this->params['thumb_command'], array('!videofile' => $videofile, '!seek' => $seek, '!thumbfile' => $thumbfile)); + // Generate the thumbnail from the video. + $command_output = $this->run_command($options); + if (!file_exists($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('video_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 = FILE_STATUS_TEMPORARY; + $file->filename = trim($filename); + $file->filepath = $thumbfile; + $file->filemime = file_get_mimetype($filename); + $file->filesize = filesize($thumbfile); + $file->timestamp = time(); + $files[] = $file; + } + return $files; + } + + public function convert_video($video, $converted, $dimensions) { + // Setup our default command to be run. + $command = t($this->params['command'], array( + '!videofile' => $video->filepath, + '!audiobitrate' => $this->params['audiobitrate'], + '!size' => $dimensions, + '!videobitrate' => $this->params['videobitrate'], + '!convertfile' => $converted + )); + // Process our video + return $this->run_command($command); + } + + public function video_converted_extension() { + return $this->params['videoext']; + } + + /** + * 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 = ' -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_transcoder_path'] = array( + '#type' => 'textfield', + '#title' => t('Path to Video Transcoder'), + '#description' => t('Absolute path to ffmpeg.'), + '#default_value' => variable_get('video_transcoder_path', '/usr/bin/ffmpeg'), + ); + $form['no_of_video_thumbs'] = array( + '#type' => 'textfield', + '#title' => t('Number of thumbnails'), + '#description' => t('Number of thumbnails to display from video.'), + '#default_value' => variable_get('no_of_video_thumbs', 5), + ); + $form['video_ffmpeg_nice_enable'] = array( + '#type' => 'checkbox', + '#title' => t('Enable the use of nice when calling the ffmpeg command.'), + '#default_value' => variable_get('video_ffmpeg_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.') + ); + // Thumbnail Videos We need to put this stuff last. + $form['autothumb'] = array( + '#type' => 'fieldset', + '#title' => t('Video Thumbnails'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['autothumb']['video_thumb_path'] = array( + '#type' => 'textfield', + '#title' => t('Path to save thumbnails'), + '#description' => t('Path to save video thumbnails extracted from the videos.'), + '#default_value' => variable_get('video_thumb_path', 'video_thumbs'), + ); + $form['autothumb']['advanced'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced Settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE + ); + $form['autothumb']['advanced']['video_ffmpeg_thumbnailer_options'] = array( + '#type' => 'textarea', + '#title' => t('Video thumbnailer options'), + '#description' => t('Provide the options for the thumbnailer. Available argument values are: ').'
  1. '.t('!videofile (the video file to thumbnail)').'
  2. '.t('!thumbfile (a newly created temporary file to overwrite with the thumbnail)
'), + '#default_value' => variable_get('video_ffmpeg_thumbnailer_options', '-i !videofile -an -y -f mjpeg -ss !seek -vframes 1 !thumbfile'), + ); + + // Video conversion settings. + $form['autoconv'] = array( + '#type' => 'fieldset', + '#title' => t('Video Conversion'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#prefix' => '
', + '#suffix' => '
' + ); + $form['autoconv']['video_ffmpeg_ext'] = array( + '#type' => 'textfield', + '#title' => t('Video Extension'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => variable_get('video_ffmpeg_ext', 'flv'), + '#description' => t('The video extensions without the period you want the converted file to contain.') + ); + $form['autoconv']['video_ffmpeg_width'] = array( + '#type' => 'textfield', + '#title' => t('Video Width'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => variable_get('video_ffmpeg_width', 640), + ); + $form['autoconv']['video_ffmpeg_height'] = array( + '#type' => 'textfield', + '#title' => t('Video height'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => variable_get('video_ffmpeg_height', 480), + ); + $form['autoconv']['video_ffmpeg_helper_auto_cvr_video_bitrate'] = array( + '#type' => 'textfield', + '#title' => t('Video bitrate'), + '#description' => t('The video bitrate in bit/s of the converted video.'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => variable_get('video_ffmpeg_helper_auto_cvr_video_bitrate', 200000), + ); + $form['autoconv']['video_ffmpeg_helper_auto_cvr_audio_bitrate'] = array( + '#type' => 'textfield', + '#title' => t('Audio bitrate'), + '#description' => t('The audio bitrate in bit/s of the converted video.'), + '#size' => 10, + '#maxlength' => 10, + '#default_value' => variable_get('video_ffmpeg_helper_auto_cvr_audio_bitrate', 64000), + ); + $form['autoconv']['advanced'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced Settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE + ); + $form['autoconv']['advanced']['video_ffmpeg_helper_auto_cvr_options'] = array( + '#type' => 'textarea', + '#title' => t('Video converter options'), + '#description' => t('Provide the ffmpeg options to configure the video conversion. Available argument values are: ').''.t('For further informations refer to the !ffmpegdoc', array('!ffmpegdoc' => l(t('Official FFMpeg documentation.'), 'http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html', array('fragment' => TRUE)))), + '#default_value' => variable_get('video_ffmpeg_helper_auto_cvr_options', '-y -i !videofile -f flv -ar 22050 -ab !audiobitrate -s !size -b !videobitrate -qscale 1 !convertfile'), + ); + return $form; + } +} +?> diff --git a/transcoders/video_ffmpeg_wrapper.inc b/transcoders/video_ffmpeg_wrapper.inc new file mode 100644 index 0000000..8d5861d --- /dev/null +++ b/transcoders/video_ffmpeg_wrapper.inc @@ -0,0 +1,243 @@ +params['thumb_command'] = variable_get('video_ffmpeg_thumbnailer_options', $this->thumb_command); + } + + public function run_command($options) { + watchdog('ffmpeg_wrapper', 'Sending options: '. $options, array(), WATCHDOG_DEBUG); + $output = ffmpeg_wrapper_run_command($options); + if(is_object($output) && ISSET($output->errors)) { + return implode("\n", $output->errors); + } + return $output; + } + + public function generate_thumbnails($video) { + global $user; + // Setup our thmbnail path. + $video_thumb_path = variable_get('video_thumb_path', 'video_thumbs'); + $final_thumb_path = file_directory_path(). '/' . $video_thumb_path . '/' . $video['fid']; + + // Ensure the destination directory exists and is writable. + $directories = explode('/', $final_thumb_path); + // 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); + } + } + + // Total thumbs to generate + $total_thumbs = variable_get('no_of_video_thumbs', 5); + $videofile = escapeshellarg($video['filepath']); + //get the playtime from the current transcoder + $duration = $this->get_playtime($video); + + $files = NULL; + for($i = 1; $i <= $total_thumbs; $i++) { + $seek = ($duration/$total_thumbs) * $i - 1; + $filename = "/video-thumb-for-$fid-$i.jpg"; + $thumbfile = $final_thumb_path . $filename; + //skip files already exists, this will save ffmpeg traffic + if (!is_file($thumbfile)) { + //setup the command to be passed to the transcoder. + $options = t($this->params['thumb_command'], array('!videofile' => $videofile, '!seek' => $seek, '!thumbfile' => $thumbfile)); + // Generate the thumbnail from the video. + $command_output = $this->run_command($options); + if (!file_exists($thumbfile)) { + $error_param = array('%file' => $thumbfile, '%cmd' => $command, '%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('video_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 = FILE_STATUS_TEMPORARY; + $file->filename = trim($filename); + $file->filepath = $thumbfile; + $file->filemime = file_get_mimetype($filename); + $file->filesize = filesize($thumbfile); + $file->timestamp = time(); + $files[] = $file; + } + return $files; + } + + public function video_converted_extension() { + global $conf; + if(isset($conf['ffmpeg_output_type']) && !empty($conf['ffmpeg_output_type'])) { + return $conf['ffmpeg_output_type']; + } + return $this->video_ext; + } + + public function convert_video($video, $converted, $dimensions) { + $ffmpeg_object = new stdClass(); + // check configuration are pass of then use global $conf + if (empty($params)) { + global $conf; + $params = $conf; + } + + // first error check, make sure that we can decode this kind of file + if (!ffmpeg_wrapper_can_decode($video->filepath)) { + $message = 'FFmpeg Wrapper can not decode this file: !file'; + $variables = array('!file' => l($video->filepath, file_create_url($video->filepath))); + watchdog('video_render', $message, $variables, WATCHDOG_ERROR); + return false; + } + + // did the admin define a specific FFmpeg comand to run? + // we only run what the admin specified + if ($params['ffmpeg_video_custom']) { + $options[] = str_replace(array('%in_file', '%out_file'), array($video->filepath, $converted), $params['ffmpeg_video_custom_command']); + } + // build a standard configuration + else { + // build the ffmpeg command structure out + $options = array(); + // input file + $options[] = "-i '". $video->filepath ."'"; + // build the watermark config + if ($params['ffmpeg_video_wm']) $options[] = "-vhook '". ffmpeg_wrapper_path_to_vhook('watermark.so') ." -f ". $params['ffmpeg_video_wm_file'] ."'"; + // build the audio config + if ($params['ffmpeg_audio_advanced']) { + // use a specifc codec? + if ($params['ffmpeg_audio_acodec']) $options[] = '-acodec '. $params['ffmpeg_audio_acodec']; + // use a specific sample rate? + if ($params['ffmpeg_audio_ar']) $options[] = '-ar '. $params['ffmpeg_audio_ar']; + // use a specific bit rate? + if ($params['ffmpeg_audio_ab']) $options[] = '-ab '. $params['ffmpeg_audio_ab']; + } + + //custom sizing per video + $options[] = '-s '. $dimensions; + + // build the video config + if ($params['ffmpeg_video_advanced']) { + // is codec set? + if ($params['ffmpeg_video_vcodec']) $options[] = '-vcodec '. $params['ffmpeg_video_vcodec']; + // is the bit rate set? + if ($params['ffmpeg_video_br']) $options[] = '-b '. $params['ffmpeg_video_br']; + // is frame rate set? + if ($params['ffmpeg_video_fps']) $options[] = '-r '. $params['ffmpeg_video_fps']; + } + // implement truncating + if ($params['ffmpeg_time_advanced']) $options[] = '-t '. $params['ffmpeg_time']; + // add the output file + $options[] = "'". $converted ."'"; + } + $ffmpeg_object->command = implode(" ", $options); + + // run our command and return the output. + return $this->run_command($ffmpeg_object->command); + } + + /** + * Return the playtime seconds of a video + */ + public function get_playtime($video) { + $ffmpeg_output = ffmpeg_wrapper_get_file_data($video['filepath']); + return $ffmpeg_output['duration']; + } + + /** + * Return the dimensions of a video + */ + public function get_dimensions($video) { + $ffmpeg_output = ffmpeg_wrapper_get_file_data($video); + $res = array('width' => 0,'height' => 0); + if($ffmpeg_output['video']['s']) { + $dimensions = explode("x", $ffmpeg_output['video']['s']); + $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() { + $help = l(t('FFMPEG Wrapper'), 'http://drupal.org/project/ffmpeg_wrapper'); + // If the module isn't loaded, show an error next to the link. + if(!module_exists('ffmpeg_wrapper')) { + $help .= ' '.t('You currently do not have this installed.').''; + } + return $help; + } + + /** + * Interface Implementations + * @see sites/all/modules/video/includes/transcoder_interface#admin_settings() + */ + public function admin_settings() { + global $conf; + $form = array(); + if (!module_exists('ffmpeg_wrapper')) { + $markup = t('You must download and install the !ffmpeg_wrapper to use the FFmpeg Wrapper as a transcoder.', array('!ffmpeg_wrapper' => l(t('FFmpeg Wrapper Module'), 'http://www.drupal.org/project/ffmpeg_wrapper'))); + $form['video_wrapper_info'] = array( + '#type' => 'markup', + '#value' => $markup, + '#prefix' => '
', + '#suffix' => '
', + ); + } + else { + $form['video_ffmpeg_wrapper_start'] = array( + '#type' => 'markup', + '#value' => '
', + ); + if(module_exists('ffmpeg_wrapper_ui')) { + $ffmpeg_admin_form = ffmpeg_wrapper_ui_configuration_form($conf); + } else if (module_exists('ffmpeg_wrapper')) { + $ffmpeg_admin_form = ffmpeg_wrapper_configuration_form($conf); + } + $form = array_merge($form, $ffmpeg_admin_form['ffmpeg_wrapper']); + $form['video_ffmpeg_wrapper_end'] = array( + '#type' => 'markup', + '#value' => '
', + ); + } + return $form; + } +} \ No newline at end of file -- cgit v1.2.3