Add doc blocks
This commit is contained in:
		
					parent
					
						
							
								70f963f347
							
						
					
				
			
			
				commit
				
					
						4582e152df
					
				
			
		
					 2 changed files with 144 additions and 23 deletions
				
			
		|  | @ -11,17 +11,31 @@ | ||||||
| 
 | 
 | ||||||
| namespace FFMpeg; | namespace FFMpeg; | ||||||
| 
 | 
 | ||||||
|  | use \Symfony\Component\Process\Process; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * FFMpeg driver | ||||||
|  |  * | ||||||
|  |  * @author Romain Neutron imprec@gmail.com | ||||||
|  |  */ | ||||||
| class FFMpeg extends Binary | class FFMpeg extends Binary | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|     protected $pathfile; |     protected $pathfile; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Opens a file in order to be processed | ||||||
|  |      * | ||||||
|  |      * @param string $pathfile | ||||||
|  |      * @throws Exception\InvalidFileArgumentException | ||||||
|  |      */ | ||||||
|     public function open($pathfile) |     public function open($pathfile) | ||||||
|     { |     { | ||||||
|         if ( ! file_exists($pathfile)) |         if ( ! file_exists($pathfile)) | ||||||
|         { |         { | ||||||
|             $this->logger->addError(sprintf('Request to open %s failed', $pathfile)); |             $this->logger->addError(sprintf('Request to open %s failed', $pathfile)); | ||||||
|             throw new \InvalidArgumentException(sprintf('File %s does not exists', $pathfile)); | 
 | ||||||
|  |             throw new Exception\InvalidFileArgumentException(sprintf('File %s does not exists', $pathfile)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $this->logger->addInfo(sprintf('FFmpeg opens %s', $pathfile)); |         $this->logger->addInfo(sprintf('FFmpeg opens %s', $pathfile)); | ||||||
|  | @ -29,11 +43,20 @@ class FFMpeg extends Binary | ||||||
|         $this->pathfile = $pathfile; |         $this->pathfile = $pathfile; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * | ||||||
|  |      * @param int $time         The time in second where to take the snapshot | ||||||
|  |      * @param string $output    The pathfile where to write | ||||||
|  |      * @param int $width        The width of the image | ||||||
|  |      * @param int $height       The height of the image | ||||||
|  |      * @return boolean          True if success | ||||||
|  |      * @throws Exception\RuntimeException | ||||||
|  |      */ | ||||||
|     public function extractImage($time, $output, $width, $height) |     public function extractImage($time, $output, $width, $height) | ||||||
|     { |     { | ||||||
|         if ( ! $this->pathfile) |         if ( ! $this->pathfile) | ||||||
|         { |         { | ||||||
|             throw new \RuntimeException('No file open'); |             throw new Exception\RuntimeException('No file open'); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $cmd = $this->binary |         $cmd = $this->binary | ||||||
|  | @ -43,19 +66,24 @@ class FFMpeg extends Binary | ||||||
| 
 | 
 | ||||||
|         $this->logger->addInfo(sprintf('Executing command %s', $cmd)); |         $this->logger->addInfo(sprintf('Executing command %s', $cmd)); | ||||||
| 
 | 
 | ||||||
|         $process = new \Symfony\Component\Process\Process($cmd); |         $process = new Process($cmd); | ||||||
|  | 
 | ||||||
|  |         try | ||||||
|  |         { | ||||||
|             $process->run(); |             $process->run(); | ||||||
|  |         } | ||||||
|  |         catch (\RuntimeException $e) | ||||||
|  |         { | ||||||
|  | 
 | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         if ( ! $process->isSuccessful()) |         if ( ! $process->isSuccessful()) | ||||||
|         { |         { | ||||||
|             $this->logger->addError(sprintf('Command failed :: %s', $process->getErrorOutput())); |             $this->logger->addError(sprintf('Command failed :: %s', $process->getErrorOutput())); | ||||||
| 
 | 
 | ||||||
|             if (file_exists($output) && is_writable($output)) |             $this->cleanupTemporaryFile($output); | ||||||
|             { |  | ||||||
|                 unlink($output); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             throw new \RuntimeException('Failed to extract image'); |             throw new Exception\RuntimeException('Failed to extract image'); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $this->logger->addInfo('Command run with success'); |         $this->logger->addInfo('Command run with success'); | ||||||
|  | @ -63,11 +91,20 @@ class FFMpeg extends Binary | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Encode the file to the specified format | ||||||
|  |      * | ||||||
|  |      * @param Format\AudioFormat $format    The output format | ||||||
|  |      * @param string $outputPathfile        The pathfile where to write | ||||||
|  |      * @param int $threads                  The number of threads to use | ||||||
|  |      * @return boolean                      True if success | ||||||
|  |      * @throws Exception\RuntimeException | ||||||
|  |      */ | ||||||
|     public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1) |     public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1) | ||||||
|     { |     { | ||||||
|         if ( ! $this->pathfile) |         if ( ! $this->pathfile) | ||||||
|         { |         { | ||||||
|             throw new \RuntimeException('No file open'); |             throw new Exception\RuntimeException('No file open'); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $threads = max(min($threads, 64), 1); |         $threads = max(min($threads, 64), 1); | ||||||
|  | @ -86,6 +123,15 @@ class FFMpeg extends Binary | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Encode to audio | ||||||
|  |      * | ||||||
|  |      * @param Format\AudioFormat $format    The output format | ||||||
|  |      * @param string $outputPathfile        The pathfile where to write | ||||||
|  |      * @param int $threads                  The number of threads to use | ||||||
|  |      * @return boolean                      True if success | ||||||
|  |      * @throws Exception\RuntimeException | ||||||
|  |      */ | ||||||
|     protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads) |     protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads) | ||||||
|     { |     { | ||||||
|         $cmd = $this->binary |         $cmd = $this->binary | ||||||
|  | @ -98,17 +144,34 @@ class FFMpeg extends Binary | ||||||
|           . ' -ac 2 -ar ' . $format->getAudioSampleRate() |           . ' -ac 2 -ar ' . $format->getAudioSampleRate() | ||||||
|           . ' ' . escapeshellarg($outputPathfile); |           . ' ' . escapeshellarg($outputPathfile); | ||||||
| 
 | 
 | ||||||
|         $process = new \Symfony\Component\Process\Process($cmd); |         $process = new Process($cmd); | ||||||
|  | 
 | ||||||
|  |         try | ||||||
|  |         { | ||||||
|             $process->run(); |             $process->run(); | ||||||
|  |         } | ||||||
|  |         catch (\RuntimeException $e) | ||||||
|  |         { | ||||||
|  | 
 | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         if ( ! $process->isSuccessful()) |         if ( ! $process->isSuccessful()) | ||||||
|         { |         { | ||||||
|             throw new \RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); |             throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Encode to video | ||||||
|  |      * | ||||||
|  |      * @param Format\VideoFormat $format    The output format | ||||||
|  |      * @param string $outputPathfile        The pathfile where to write | ||||||
|  |      * @param int $threads                  The number of threads to use | ||||||
|  |      * @return boolean                      True if success | ||||||
|  |      * @throws Exception\RuntimeException | ||||||
|  |      */ | ||||||
|     protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads) |     protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads) | ||||||
|     { |     { | ||||||
|         $cmd_part1 = $this->binary |         $cmd_part1 = $this->binary | ||||||
|  | @ -138,15 +201,17 @@ class FFMpeg extends Binary | ||||||
|         $passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2 |         $passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2 | ||||||
|           . ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile); |           . ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile); | ||||||
| 
 | 
 | ||||||
|  |         $process = null; | ||||||
|  | 
 | ||||||
|         foreach ($passes as $pass) |         foreach ($passes as $pass) | ||||||
|         { |         { | ||||||
|             $process = new \Symfony\Component\Process\Process($pass); |             $process = new Process($pass); | ||||||
| 
 | 
 | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|                 $process->run(); |                 $process->run(); | ||||||
|             } |             } | ||||||
|             catch (\Exception $e) |             catch (\RuntimeException $e) | ||||||
|             { |             { | ||||||
|                 break; |                 break; | ||||||
|             } |             } | ||||||
|  | @ -156,14 +221,19 @@ class FFMpeg extends Binary | ||||||
|         $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log'); |         $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log'); | ||||||
|         $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree'); |         $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree'); | ||||||
| 
 | 
 | ||||||
|         if ($process instanceof \Symfony\Component\Process\Process && ! $process->isSuccessful()) |         if ( ! $process->isSuccessful()) | ||||||
|         { |         { | ||||||
|             throw new \RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); |             throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Removes unnecessary file | ||||||
|  |      * | ||||||
|  |      * @param string $pathfile | ||||||
|  |      */ | ||||||
|     protected function cleanupTemporaryFile($pathfile) |     protected function cleanupTemporaryFile($pathfile) | ||||||
|     { |     { | ||||||
|         if (file_exists($pathfile) && is_writable($pathfile)) |         if (file_exists($pathfile) && is_writable($pathfile)) | ||||||
|  | @ -172,6 +242,11 @@ class FFMpeg extends Binary | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Return the binary name | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|     protected static function getBinaryName() |     protected static function getBinaryName() | ||||||
|     { |     { | ||||||
|         return 'ffmpeg'; |         return 'ffmpeg'; | ||||||
|  |  | ||||||
|  | @ -11,14 +11,28 @@ | ||||||
| 
 | 
 | ||||||
| namespace FFMpeg; | namespace FFMpeg; | ||||||
| 
 | 
 | ||||||
|  | use \Symfony\Component\Process\Process; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * FFProbe driver | ||||||
|  |  * | ||||||
|  |  * @author Romain Neutron imprec@gmail.com | ||||||
|  |  */ | ||||||
| class FFProbe extends Binary | class FFProbe extends Binary | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Probe the format of a given file | ||||||
|  |      * | ||||||
|  |      * @param string $pathfile | ||||||
|  |      * @return string | ||||||
|  |      * @throws Exception\InvalidFileArgumentException | ||||||
|  |      */ | ||||||
|     public function probeFormat($pathfile) |     public function probeFormat($pathfile) | ||||||
|     { |     { | ||||||
|         if ( ! is_file($pathfile)) |         if ( ! is_file($pathfile)) | ||||||
|         { |         { | ||||||
|             throw new \InvalidArgumentException($pathfile); |             throw new Exception\InvalidFileArgumentException($pathfile); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $cmd = $this->binary . ' ' . $pathfile . ' -show_format'; |         $cmd = $this->binary . ' ' . $pathfile . ' -show_format'; | ||||||
|  | @ -26,32 +40,64 @@ class FFProbe extends Binary | ||||||
|         return $this->executeProbe($cmd); |         return $this->executeProbe($cmd); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Probe the streams contained in a given file | ||||||
|  |      * | ||||||
|  |      * @param string $pathfile | ||||||
|  |      * @return string | ||||||
|  |      * @throws Exception\InvalidFileArgumentException | ||||||
|  |      */ | ||||||
|     public function probeStreams($pathfile) |     public function probeStreams($pathfile) | ||||||
|     { |     { | ||||||
|         if ( ! is_file($pathfile)) |         if ( ! is_file($pathfile)) | ||||||
|         { |         { | ||||||
|             throw new \InvalidArgumentException($pathfile); |             throw new Exception\InvalidFileArgumentException($pathfile); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $cmd = $this->binary . ' ' . $pathfile . ' -show_streams'; |         $cmd = $this->binary . ' ' . $pathfile . ' -show_streams'; | ||||||
| 
 | 
 | ||||||
|  |         try | ||||||
|  |         { | ||||||
|             return $this->executeProbe($cmd); |             return $this->executeProbe($cmd); | ||||||
|         } |         } | ||||||
|  |         catch (Exception\RuntimeException $e) | ||||||
|  |         { | ||||||
| 
 | 
 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * | ||||||
|  |      * @param string $command | ||||||
|  |      * @return string | ||||||
|  |      * @throws Exception\RuntimeException | ||||||
|  |      */ | ||||||
|     protected function executeProbe($command) |     protected function executeProbe($command) | ||||||
|     { |     { | ||||||
|         $process = new \Symfony\Component\Process\Process($command); |         try | ||||||
|  |         { | ||||||
|  |             $process = new Process($command); | ||||||
| 
 | 
 | ||||||
|             $process->run(); |             $process->run(); | ||||||
|  |         } | ||||||
|  |         catch (\RuntimeException $e) | ||||||
|  |         { | ||||||
|  |             throw new Exception\RuntimeException(sprintf('Failed to run the given command %s', $command)); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         if ( ! $process->isSuccessful()) |         if ( ! $process->isSuccessful()) | ||||||
|         { |         { | ||||||
|             throw new \RuntimeException(sprintf('Failed to probe %s', $command)); |             throw new Exception\RuntimeException(sprintf('Failed to probe %s', $command)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return $process->getOutput(); |         return $process->getOutput(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Return the binary name | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|     protected static function getBinaryName() |     protected static function getBinaryName() | ||||||
|     { |     { | ||||||
|         return 'ffprobe'; |         return 'ffprobe'; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue