You are here

public function video_localcommand::convert_video in Video 6.5

Convert the given video.

Make sure to update the provided video object with any information that is required by the file system module after conversion.

Return value

bool TRUE if the conversion was successful, FALSE otherwise

Overrides video_transcoder::convert_video

File

transcoders/video_localcommand.inc, line 132

Class

video_localcommand

Code

public function convert_video(stdClass $video) {

  // This will update our current video status to active.
  $this
    ->change_status($video, VIDEO_RENDERING_ACTIVE);
  $converteddir = dirname(dirname($video->filepath)) . '/converted';

  // Check if $converteddir is an absolute path, we need this later
  $converteddirabsolute = FALSE;
  if ($converteddir[0] == '/') {
    $converteddirabsolute = TRUE;
  }
  elseif (substr($converteddir, 1, 2) == ':\\') {
    $converteddirabsolute = TRUE;
  }
  if (!field_file_check_directory($converteddir, FILE_CREATE_DIRECTORY)) {
    watchdog('video_command', 'Video conversion could not be started: Could not create directory %dir for storing converted videos.', array(
      '%dir' => $converteddir,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $sourcedimensions = $this
    ->get_dimensions($video->filepath);
  if (empty($sourcedimensions)) {
    watchdog('video_command', 'Video conversion could not be started: Could not find file information for file %file.', array(
      '%file' => $video->filepath,
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Increase the database timeout to prevent database errors after a long upload
  _video_db_increase_timeout();

  // Get the placeholders for the transcoding command
  $dimensionparameters = $this
    ->getDimensionParameters($video, $sourcedimensions);
  $parameters = array(
    '!cmd_path' => $this->cmdpath,
    '!videofile' => escapeshellarg(realpath($video->filepath)),
    '!convertfile' => NULL,
    '!width' => $dimensionparameters->width,
    '!height' => $dimensionparameters->height,
    '!paddingwidth' => $dimensionparameters->paddingwidth,
    '!paddingheight' => $dimensionparameters->paddingheight,
    '!paddingtop' => $dimensionparameters->paddingtop,
    '!paddingbottom' => $dimensionparameters->paddingbottom,
    '!paddingleft' => $dimensionparameters->paddingleft,
    '!paddingright' => $dimensionparameters->paddingright,
  );

  // Create a temporary directory and move to it
  $drupaldir = getcwd();
  $tmpdir = tempnam(file_directory_temp(), 'drupal-video-' . $video->fid);
  unlink($tmpdir);
  mkdir($tmpdir, 0777);
  chdir($tmpdir);
  $result = TRUE;

  // process presets
  $converted_files = array();
  foreach ($video->presets as $preset) {

    // Preset settings
    $settings = $preset
      ->getSettings();

    // Update our filename after the move to maintain filename uniqueness.
    $outputfile = file_create_filename(str_replace(' ', '_', pathinfo($video->filepath, PATHINFO_FILENAME)) . $preset->filenamesuffix . '.' . $preset->extension, $converteddir);
    $outputfilefull = $converteddirabsolute ? $outputfile : $drupaldir . '/' . $outputfile;
    $parameters['!convertfile'] = escapeshellarg($outputfilefull);
    $command_output = '';

    // Setup our default command to be run.
    foreach ($settings['commands'] as $cmdnr => $command) {
      $command = strtr($command, $parameters);

      // Process our video
      if (!$this
        ->run_command($command, $command_output, t('rendering preset %preset, command #@cmdnr', array(
        '%preset' => $preset->name,
        '@cmdnr' => $cmdnr + 1,
      )))) {
        $result = FALSE;
        break 2;
      }
    }

    // Run flvtool2, if required
    if (!empty($settings['useflvtool2'])) {
      $command = $this->flvtoolcmd . ' -U ' . escapeshellarg($outputfilefull);
      $this
        ->run_command($command, $command_output, t('running flvtool2'));
    }

    // Run qt-faststart, if required
    // qt-faststart needs a temporary file
    if (!empty($settings['useqtfaststart'])) {
      $tmpfile = $outputfilefull . '-qt';
      $command = $this->faststartcmd . ' ' . escapeshellarg($outputfilefull) . ' ' . escapeshellarg($tmpfile);

      // qt-faststart does not return an error code when it doesn't generate an output file,
      // so also check if the output file has been generated.
      if ($this
        ->run_command($command, $command_output, t('running qt-faststart')) && file_exists($tmpfile)) {
        file_delete($outputfilefull);
        rename($tmpfile, $outputfilefull);
      }
      else {
        file_delete($tmpfile);
      }
    }

    // Lets check to make sure our file exists, if not error out
    if (!file_exists($outputfilefull) || ($filesize = filesize($outputfilefull)) === 0) {
      watchdog('video_command', 'Video conversion failed for preset %preset: result file was not found.', array(
        '%preset' => $preset->name,
      ), WATCHDOG_ERROR);
      $result = FALSE;
      break;
    }

    // Create result object
    $converted_files[] = $file = new stdClass();
    $file->vid = intval($video->vid);
    $file->filename = basename($outputfile);
    $file->filepath = $outputfile;
    $file->filemime = file_get_mimetype($outputfile);
    $file->filesize = $filesize;
    $file->preset = $preset->name;
  }
  chdir($drupaldir);
  rmdirr($tmpdir);

  // Update our video_files table with the converted video information.
  if ($result) {
    $video->status = VIDEO_RENDERING_COMPLETE;
    $video->completed = time();
    $video->data = $converted_files;
    $result = db_query('UPDATE {video_files} SET status = %d, completed = %d, data = "%s" WHERE vid = %d', $video->status, $video->completed, serialize($video->data), $video->vid);

    // Prepare the watchdog statement
    $destinationfiles = array();
    foreach ($converted_files as $file) {
      $destinationfiles[] = $file->filepath;
    }
    watchdog('video_command', 'Successfully converted %orig to !destination-files', array(
      '%orig' => $video->filepath,
      '!destination-files' => implode(', ', $destinationfiles),
    ), WATCHDOG_INFO);
  }
  else {

    // Remove files that have been created
    foreach ($converted_files as $file) {
      file_delete($file->filepath);
    }
    $this
      ->change_status($video, VIDEO_RENDERING_FAILED);
  }
  return $result;
}