Example #1
0
 /**
  * Encode to video
  *
  * @param  Video            $format         The output format
  * @param  string           $outputPathfile The pathfile where to write
  * @param  integer          $threads        The number of threads to use
  * @return \FFMpeg\FFMpeg
  * @throws RuntimeException
  */
 protected function encodeVideo(Video $format, $outputPathfile, $threads)
 {
     $cmd_part1 = $this->binary . ' -y -i ' . escapeshellarg($this->pathfile) . ' ' . $format->getExtraParams() . ' ';
     $cmd_part2 = '';
     if ($format instanceof Video\Resizable) {
         if (!$this->prober) {
             throw new LogicException('You must set a valid prober if you use RESIZEMODE_INSET');
         }
         $result = json_decode($this->prober->probeStreams($this->pathfile), true);
         $originalWidth = $originalHeight = null;
         foreach ($result as $stream) {
             foreach ($stream as $name => $value) {
                 if ($name == 'width') {
                     $originalWidth = $value;
                     continue;
                 }
                 if ($name == 'value') {
                     $originalHeight = $value;
                     continue;
                 }
             }
         }
         if ($originalHeight !== null && $originalWidth !== null) {
             $dimensions = $format->getComputedDimensions($originalWidth, $originalHeight);
             $width = $this->getMultiple($dimensions->getWidth(), 16);
             $height = $this->getMultiple($dimensions->getHeight(), 16);
             $cmd_part2 .= ' -s ' . $width . 'x' . $height;
         }
     }
     if ($format instanceof Video\Resamplable) {
         $cmd_part2 .= ' -r ' . $format->getFrameRate();
     }
     if ($format instanceof Video\Transcodable) {
         $cmd_part2 .= ' -vcodec ' . $format->getVideoCodec();
     }
     $cmd_part2 .= ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3' . ' -threads ' . $threads . ' -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 ' . ' -sc_threshold 40 -flags +loop -cmp +chroma' . ' -me_range 16 -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 ' . ' -trellis 1 -qscale 1 ' . ' -ab 92k ';
     if ($format instanceof Audio\Transcodable) {
         $cmd_part2 .= '-acodec ' . $format->getAudioCodec();
     }
     $tmpFile = new \SplFileInfo(tempnam(sys_get_temp_dir(), 'temp') . '.' . pathinfo($outputPathfile, PATHINFO_EXTENSION));
     $passes = array();
     $passes[] = $cmd_part1 . ' -pass 1 ' . $cmd_part2 . ' -an ' . escapeshellarg($tmpFile->getPathname());
     $passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2 . ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile);
     $process = null;
     foreach ($passes as $pass) {
         $this->logger->addInfo(sprintf('FFmpeg executes command %s', $pass));
         $process = new Process($pass);
         try {
             $process->run();
         } catch (\RuntimeException $e) {
             break;
         }
     }
     $this->cleanupTemporaryFile($tmpFile->getPathname());
     $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log');
     $this->cleanupTemporaryFile(getcwd() . '/av2pass-0.log');
     $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree');
     if (!$process->isSuccessful()) {
         $this->logger->addInfo(sprintf('FFmpeg command failed'));
         throw new RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
     }
     $this->logger->addInfo(sprintf('FFmpeg command successful'));
     return $this;
 }
Example #2
0
 /**
  * @covers FFMpeg\FFProbe::probeStreams
  * @covers FFMpeg\FFProbe::executeProbe
  * @expectedException \InvalidArgumentException
  */
 public function testProbeStreamsInvalidPathFile()
 {
     $this->object->probeStreams(__DIR__ . '/../../files/unknown.file');
 }