Beispiel #1
0
 /**
  * Exports the audio in the desired format, applies registered filters.
  *
  * @param FormatInterface $format
  * @param string          $outputPathfile
  *
  * @return Audio
  *
  * @throws RuntimeException
  */
 public function save(FormatInterface $format, $outputPathfile)
 {
     $listeners = null;
     if ($format instanceof ProgressableInterface) {
         $listeners = $format->createProgressListener($this, $this->ffprobe, 1, 1);
     }
     $commands = array('-y', '-i', $this->filePath);
     $filters = clone $this->filters;
     $filters->add(new SimpleFilter($format->getExtraParams(), 10));
     if ($this->driver->getConfiguration()->has('ffmpeg.threads')) {
         $filters->add(new SimpleFilter(array('-threads', $this->driver->getConfiguration()->get('ffmpeg.threads'))));
     }
     if (null !== $format->getAudioCodec()) {
         $filters->add(new SimpleFilter(array('-acodec', $format->getAudioCodec())));
     }
     foreach ($filters as $filter) {
         $commands = array_merge($commands, $filter->apply($this, $format));
     }
     if (null !== $format->getAudioKiloBitrate()) {
         $commands[] = '-b:a';
         $commands[] = $format->getAudioKiloBitrate() . 'k';
     }
     if (null !== $format->getAudioChannels()) {
         $commands[] = '-ac';
         $commands[] = $format->getAudioChannels();
     }
     $commands[] = $outputPathfile;
     try {
         $this->driver->command($commands, false, $listeners);
     } catch (ExecutionFailureException $e) {
         $this->cleanupTemporaryFile($outputPathfile);
         throw new RuntimeException('Encoding failed', $e->getCode(), $e);
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Exports the video in the desired format, applies registered filters.
  *
  * @param FormatInterface $format
  * @param string          $outputPathfile
  *
  * @return Video
  *
  * @throws RuntimeException
  */
 public function save(FormatInterface $format, $outputPathfile)
 {
     $commands = array('-y', '-i', $this->pathfile);
     $filters = clone $this->filters;
     $filters->add(new SimpleFilter($format->getExtraParams(), 10));
     if ($this->driver->getConfiguration()->has('ffmpeg.threads')) {
         $filters->add(new SimpleFilter(array('-threads', $this->driver->getConfiguration()->get('ffmpeg.threads'))));
     }
     if ($format instanceof VideoInterface) {
         if (null !== $format->getVideoCodec()) {
             $filters->add(new SimpleFilter(array('-vcodec', $format->getVideoCodec())));
         }
     }
     if ($format instanceof AudioInterface) {
         if (null !== $format->getAudioCodec()) {
             $filters->add(new SimpleFilter(array('-acodec', $format->getAudioCodec())));
             if ($format->getAudioCodec() == 'aac') {
                 $filters->add(new SimpleFilter(array('-strict', '-2')));
             }
         }
     }
     foreach ($filters as $filter) {
         $commands = array_merge($commands, $filter->apply($this, $format));
     }
     if ($format instanceof VideoInterface) {
         $commands[] = '-b:v';
         $commands[] = $format->getKiloBitrate() . 'k';
         $commands[] = '-refs';
         $commands[] = '6';
         $commands[] = '-coder';
         $commands[] = '1';
         $commands[] = '-sc_threshold';
         $commands[] = '40';
         $commands[] = '-flags';
         $commands[] = '+loop';
         $commands[] = '-me_range';
         $commands[] = '16';
         $commands[] = '-subq';
         $commands[] = '7';
         $commands[] = '-i_qfactor';
         $commands[] = '0.71';
         $commands[] = '-qcomp';
         $commands[] = '0.6';
         $commands[] = '-qdiff';
         $commands[] = '4';
         $commands[] = '-trellis';
         $commands[] = '1';
     }
     if ($format instanceof AudioInterface) {
         if (null !== $format->getAudioKiloBitrate()) {
             $commands[] = '-b:a';
             $commands[] = $format->getAudioKiloBitrate() . 'k';
         }
         if (null !== $format->getAudioChannels()) {
             $commands[] = '-ac';
             $commands[] = $format->getAudioChannels();
         }
     }
     $fs = FsManager::create();
     $fsId = uniqid('ffmpeg-passes');
     $passPrefix = $fs->createTemporaryDirectory(0777, 50, $fsId) . '/' . uniqid('pass-');
     $passes = array();
     $totalPasses = $format->getPasses();
     if (1 > $totalPasses) {
         throw new InvalidArgumentException('Pass number should be a positive value.');
     }
     for ($i = 1; $i <= $totalPasses; $i++) {
         $pass = $commands;
         if ($totalPasses > 1) {
             $pass[] = '-pass';
             $pass[] = $i;
             //$pass[] = '-passlogfile';
             //$pass[] = $passPrefix;
         }
         $pass[] = $outputPathfile;
         $passes[] = $pass;
     }
     $failure = null;
     foreach ($passes as $pass => $passCommands) {
         try {
             /** add listeners here */
             $listeners = null;
             if ($format instanceof ProgressableInterface) {
                 $listeners = $format->createProgressListener($this, $this->ffprobe, $pass + 1, $totalPasses);
             }
             $this->driver->command($passCommands, false, $listeners);
         } catch (ExecutionFailureException $e) {
             $failure = $e;
             break;
         }
     }
     $fs->clean($fsId);
     if (null !== $failure) {
         throw new RuntimeException('Encoding failed', $failure->getCode(), $failure);
     }
     return $this;
 }
Beispiel #3
0
 /**
  * Exports the video in the desired format, applies registered filters.
  *
  * @param FormatInterface $format
  * @param string          $outputPathfile
  *
  * @return Video
  *
  * @throws RuntimeException
  */
 public function save(FormatInterface $format, $outputPathfile, $advCommands = array())
 {
     $commands = array('-y', '-i', $this->pathfile);
     $filters = clone $this->filters;
     $filters->add(new SimpleFilter($format->getExtraParams(), 10));
     if ($this->driver->getConfiguration()->has('ffmpeg.threads')) {
         $filters->add(new SimpleFilter(array('-threads', $this->driver->getConfiguration()->get('ffmpeg.threads'))));
     }
     if (null !== $format->getVideoCodec()) {
         $filters->add(new SimpleFilter(array('-vcodec', $format->getVideoCodec())));
     }
     if (null !== $format->getAudioCodec()) {
         $filters->add(new SimpleFilter(array('-acodec', $format->getAudioCodec())));
     }
     foreach ($filters as $filter) {
         $commands = array_merge($commands, $filter->apply($this, $format));
     }
     $commands[] = '-b';
     $commands[] = $format->getKiloBitrate() . 'k';
     $commands[] = '-refs';
     $commands[] = '6';
     $commands[] = '-coder';
     $commands[] = '1';
     $commands[] = '-sc_threshold';
     $commands[] = '40';
     $commands[] = '-flags';
     $commands[] = '+loop';
     $commands[] = '-me_range';
     $commands[] = '16';
     $commands[] = '-subq';
     $commands[] = '7';
     $commands[] = '-i_qfactor';
     $commands[] = '0.71';
     $commands[] = '-qcomp';
     $commands[] = '0.6';
     // $commands[] = '-qdiff';
     // $commands[] = '4';
     $commands[] = '-trellis';
     $commands[] = '1';
     if (null !== $format->getAudioKiloBitrate()) {
         $commands[] = '-ab';
         $commands[] = $format->getAudioKiloBitrate() . 'k';
     }
     foreach ($advCommands as $cmd) {
         $commands[] = $cmd;
     }
     $passPrefix = uniqid('pass-');
     $pass1 = $commands;
     $pass2 = $commands;
     $pass1[] = '-pass';
     $pass1[] = '1';
     $pass1[] = '-passlogfile';
     $pass1[] = $passPrefix;
     $pass1[] = $outputPathfile;
     $pass2[] = '-pass';
     $pass2[] = '2';
     $pass2[] = '-passlogfile';
     $pass2[] = $passPrefix;
     $pass2[] = $outputPathfile;
     $failure = null;
     foreach (array($pass1, $pass2) as $pass => $passCommands) {
         try {
             /** add listeners here */
             $listeners = null;
             if ($format instanceof ProgressableInterface) {
                 $listeners = $format->createProgressListener($this, $this->ffprobe, $pass + 1, 2);
             }
             $this->driver->command($passCommands, false, $listeners);
         } catch (ExecutionFailureException $e) {
             $failure = $e;
             break;
         }
     }
     $this->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '-0.log')->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '-0.log.mbtree')->cleanupTemporaryFile(getcwd() . '/' . $passPrefix)->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '.mbtree');
     if (null !== $failure) {
         throw new RuntimeException('Encoding failed', $failure->getCode(), $failure);
     }
     return $this;
 }