You are here

public function TranscoderAbstractionFactoryFfmpeg::setOptions in Video 7.2

Set options is to set transcoding settings before send to the transcoder.

Overrides TranscoderAbstractionFactory::setOptions

File

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

Class

TranscoderAbstractionFactoryFfmpeg
Class that handles FFmpeg transcoding.

Code

public function setOptions(array $options) {

  // Reset the transcoder class keeping the input file info
  $this->transcoder
    ->reset(TRUE);
  $this->multipass = TRUE;
  $this->outputextension = NULL;
  $clipstart = NULL;
  $cliplength = NULL;
  $hasvideo = empty($options['skip_video']);
  $hasaudio = empty($options['skip_audio']);
  $this
    ->setAspectRatioOptions($options);
  foreach ($options as $key => $value) {
    if (empty($value) || $value === 'none') {
      continue;
    }
    if (strncmp($key, 'audio_', 6) === 0 && !$hasaudio) {
      continue;
    }
    if (strncmp($key, 'video_', 6) === 0 && !$hasvideo) {
      continue;
    }
    $result = TRUE;
    switch ($key) {
      case 'max_frame_rate':
        $result = $this->transcoder
          ->setVideoFrameRate($value);
        break;
      case 'video_codec':
        if ($hasvideo) {
          $result = $this->transcoder
            ->setVideoCodec($value);
        }
        break;
      case 'video_preset':
        $result = $this->transcoder
          ->setVideoPreset($value);
        break;
      case 'audio_sample_rate':
        if ($value < 1000) {
          $value *= 1000;
        }
        $value = min($value, 44100);
        $result = $this->transcoder
          ->setAudioSampleFrequency($value);
        break;
      case 'audio_codec':
        $result = $this->transcoder
          ->setAudioCodec($value);
        break;
      case 'audio_channels':
        $result = $this->transcoder
          ->setAudioChannels($value);
        break;
      case 'audio_bitrate':
        if ($value < 1000) {
          $value .= 'k';
        }
        $result = $this->transcoder
          ->setAudioBitRate($value);
        break;
      case 'video_bitrate':
        $result = $this->transcoder
          ->setVideoBitRate($value);
        break;
      case 'pixel_format':
        $result = $this->transcoder
          ->setPixelFormat($value);
        break;
      case 'one_pass':
        if ($value == 1) {
          $this->multipass = FALSE;
        }
        break;
      case 'video_extension':
        $this->outputextension = $value;
        break;
      case 'video_quality':
        $result = $this->transcoder
          ->setConstantQuality($value * 20);

        // phpVideoToolkit expects 1 to 100 range.
        break;
      case 'clip_start':
        if (preg_match('#^(\\d+)$#', $value)) {
          $clipstart = intval($value);
        }
        else {
          $clipstart = $this->transcoder
            ->formatTimecode($value, '%hh:%mm:%ss.%ms', '%st');
        }
        break;
      case 'clip_length':
        if (preg_match('#^(\\d+)$#', $value)) {
          $cliplength = intval($value);
        }
        else {
          $cliplength = $this->transcoder
            ->formatTimecode($value, '%hh:%mm:%ss.%ms', '%st');
        }
        break;
      case 'skip_video':
        $result = $this->transcoder
          ->addCommand('-vn');
        $this->multipass = FALSE;
        break;
      case 'skip_audio':
        $result = $this->transcoder
          ->addCommand('-an');
        break;
    }
    if ($cliplength !== NULL && $clipstart !== NULL) {
      $result = $this->transcoder
        ->extractSegment($clipstart, $clipstart + $cliplength, '%st', FALSE, FALSE);
      $cliplength = NULL;
      $clipstart = NULL;
    }
    if ($result !== PHPVideoToolkit::RESULT_OK) {
      watchdog('transcoder', 'Error set options @message', array(
        '@message' => $this->transcoder
          ->getLastError(),
      ), WATCHDOG_ERROR);
      $this->errors['options'] = $this->transcoder
        ->getLastError();
      $this->transcoder
        ->reset(true);
      return FALSE;
    }
  }
  return TRUE;
}