You are here

public function video_phpvideotoolkit::convert_video in Video 7

Overrides transcoder_interface::convert_video

File

transcoders/video_phpvideotoolkit.inc, line 143

Class

video_phpvideotoolkit

Code

public function convert_video($video) {

  // This will update our current video status to active.
  //    $this->change_status($video->vid, VIDEO_RENDERING_ACTIVE);
  // get the paths so tokens will compatible with this
  // @todo : add best method to get existing file path and add converted there
  $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;
  }

  //get the actual video file path from the stream wrappers
  $original_video_path = drupal_realpath($video->uri);

  // process presets
  $presets = $video->presets;
  $converted_files = array();
  foreach ($presets as $name => $preset) {
    $settings = $preset['settings'];

    // override with 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;

    //get the actual video file path from the stream wrappers
    $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) {

      // 			if there was an error then get it
      $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) {

        // 			if there was an error then get it
        $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $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) {

      // 			if there was an error then get it
      $error_msg = t($this->toolkit
        ->getLastError());
      watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
      $this->toolkit
        ->reset();
      continue;
    }
    $command_output = $this->toolkit
      ->getLastOutput();

    /*
     if ($ffmpeg_output != $converted_video_path && 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_video_path, $verbose);
     $this->params['cmd_path'] = $cmd_path;

     // Delete the temporary output file.
     drupal_unlink($ffmpeg_output);
     }
    */

    //lets check to make sure our file exists, if not error out
    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;
    }

    // Setup our converted video object
    $video_info = pathinfo($converted_video_path);

    //update our converted video
    $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;
  }

  // Update our video_files table with the converted video information.
  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;
}