You are here

public function TranscoderAbstractionFactoryFfmpeg::execute in Video 7.2

Overrides TranscoderFactoryInterface::execute

File

transcoders/TranscoderAbstractionFactoryFfmpeg.inc, line 290
File containing class TranscoderAbstractionFactoryFfmpeg

Class

TranscoderAbstractionFactoryFfmpeg
Class that handles FFmpeg transcoding.

Code

public function execute() {

  // Execute the command in a temporary directory
  $drupaldir = getcwd();
  $tmpdir = video_utility::createTempDir();
  chmod($tmpdir, 0777);
  chdir($tmpdir);

  // Make sure that exec() is enabled.
  if (!function_exists('exec')) {
    watchdog('trancoder', 'Php can\'t use FFmpeg because php.ini has disabled the exec command. Please remove exec from the disable_functions directive (http://us1.php.net/manual/en/ini.core.php#ini.disable-functions)', WATCHDOG_ERROR);
  }

  // Execute the command
  $result = $this->transcoder
    ->execute($this->multipass);

  // Restore the directory
  chdir($drupaldir);

  // Log an error when trancoding fails
  $tmpoutputpath = $this->settings['base_url'] . '/' . $this->settings['filename'];
  if ($result !== PHPVideoToolkit::RESULT_OK || !file_exists($tmpoutputpath) || ($filesize = filesize($tmpoutputpath)) == 0) {
    $errorlist = $this->transcoder
      ->getErrors();
    $_commandoutput = $this->transcoder
      ->getCommandOutput();
    $commandoutput = array();
    foreach ($_commandoutput as $cmd) {
      $commandoutput[] = '<pre>' . check_plain($cmd['command']) . '</pre><pre>' . check_plain($cmd['output']) . '</p>';
    }
    watchdog('transcoder', 'FFmpeg failed to transcode %video. !errorlist !commandlist', array(
      '%video' => $this->settings['input']['filename'],
      '!errorlist' => theme('item_list', array(
        'type' => 'ol',
        'items' => $errorlist,
        'title' => t('Reported errors'),
      )),
      '!commandlist' => theme('item_list', array(
        'type' => 'ol',
        'items' => $commandoutput,
        'title' => t('Executed commands and output'),
      )),
    ), WATCHDOG_ERROR);
    $this->errors['execute'] = $errorlist;
    $this->transcoder
      ->reset(true);
    return FALSE;
  }

  // Post-process the file with qt-faststart
  $cmd = variable_get('video_ffmpeg_qtfaststart_path');
  if ($cmd != NULL && is_file($cmd) && $this->outputextension == 'mp4') {
    $qttmpfile = $tmpoutputpath . '-qt';
    $output = array();
    $retval = 0;
    exec($cmd . ' ' . escapeshellarg($tmpoutputpath) . ' ' . escapeshellarg($qttmpfile) . ' 2>&1', $output, $retval);

    // 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 ($retval == 0 && file_exists($qttmpfile)) {
      drupal_unlink($tmpoutputpath);
      rename($qttmpfile, $tmpoutputpath);
    }
    else {
      watchdog('transcoder', 'Error while executing @cmdname on video @filename: @output', array(
        '@cmdname' => 'qt-faststart',
        '@filename' => $this->realoutputname,
        '@output' => implode("\n", $output),
      ), WATCHDOG_ERROR);
      if (file_exists($qttmpfile)) {
        drupal_unlink($qttmpfile);
      }
    }
  }

  // Post-process the file with flvtool2
  $cmd = variable_get('video_ffmpeg_flvtool2_path');
  if ($cmd != NULL && is_file($cmd) && $this->outputextension == 'flv') {
    $output = array();
    $retval = 0;
    exec($cmd . ' -U ' . escapeshellarg($tmpoutputpath) . ' 2>&1', $output, $retval);
    if ($retval != 0) {
      watchdog('transcoder', 'Error while executing @cmdname on video @filename: @output', array(
        '@cmdname' => 'flvtool2',
        '@filename' => $this->realoutputname,
        '@output' => implode("\n", $output),
      ), WATCHDOG_ERROR);
    }
  }
  $file_info = $this
    ->getFileInfo();
  $realoutputuri = $this->realoutputdir . '/' . $this->realoutputname;
  copy($tmpoutputpath, $realoutputuri);
  drupal_unlink($tmpoutputpath);
  $output = new stdClass();
  $output->filename = $this->realoutputname;
  $output->uri = $realoutputuri;
  $output->filesize = $filesize;
  $output->timestamp = REQUEST_TIME;
  $output->jobid = NULL;
  $output->duration = floor($file_info['duration']['seconds']);
  return $output;
}