You are here

public function video_ffmpeg::convert_video in Video 7

Same name and namespace in other branches
  1. 6.4 transcoders/video_ffmpeg.inc \video_ffmpeg::convert_video()

Overrides transcoder_interface::convert_video

File

transcoders/video_ffmpeg.inc, line 128

Class

video_ffmpeg

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 = $converted_base_dir . '/' . file_munge_filename(str_replace(' ', '_', pathinfo($original_video_path, PATHINFO_FILENAME)) . '_' . strtolower($name) . '.' . $settings['video_extension'], $settings['video_extension']);

    //get the actual video file path from the stream wrappers
    $converted_video_path = drupal_realpath($converted);
    $dimensions = $this
      ->dimensions($video);
    $dimention = explode('x', $dimensions);
    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;
    }

    // Setup our default command to be run.
    $command = strtr($settings['cli_code'], array(
      '!videofile' => '"' . $original_video_path . '"',
      '!audiobitrate' => $settings['audio_bitrate'],
      '!width' => $dimention[0],
      '!height' => $dimention[1],
      '!videobitrate' => $settings['video_bitrate'],
      '!convertfile' => '"' . $ffmpeg_output . '"',
    ));

    // process our video
    $command_output = $this
      ->run_command($command);
    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.
      $command = implode(' ', array(
        $this->params['faststart_cmd'],
        $ffmpeg_output,
        $converted_video_path,
      ));
      $command_output .= $this
        ->run_command($command);

      // 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;
}