Add audio formats MP3 and Flac
This commit is contained in:
		
					parent
					
						
							
								94775176c9
							
						
					
				
			
			
				commit
				
					
						5b53381904
					
				
			
		
					 9 changed files with 123 additions and 5 deletions
				
			
		|  | @ -63,6 +63,45 @@ class FFMpeg extends Binary | |||
| 
 | ||||
|         $threads = max(min($threads, 64), 1); | ||||
| 
 | ||||
|         switch (true) | ||||
|         { | ||||
|             case $format instanceof Format\VideoFormat: | ||||
|                 return $this->encodeVideo($format, $outputPathfile, $threads); | ||||
|                 break; | ||||
|             default: | ||||
|             case $format instanceof Format\AudioFormat: | ||||
|                 return $this->encodeAudio($format, $outputPathfile, $threads); | ||||
|                 break; | ||||
|         } | ||||
| 
 | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads) | ||||
|     { | ||||
|         $cmd = $this->binary | ||||
|           . ' -y -i ' | ||||
|           . escapeshellarg($this->pathfile) | ||||
|           . ' ' . $format->getExtraParams() | ||||
|           . ' -threads ' . $threads | ||||
|           . ' -acodec ' . $format->getAudioCodec() | ||||
|           . ' -ab ' . $format->getKiloBitrate() . 'k ' | ||||
|           . ' -ac 2 -ar ' . $format->getAudioSampleRate() | ||||
|           . ' ' . escapeshellarg($outputPathfile); | ||||
| 
 | ||||
|         $process = new \Symfony\Component\Process\Process($cmd); | ||||
|         $process->run(); | ||||
| 
 | ||||
|         if ( ! $process->isSuccessful()) | ||||
|         { | ||||
|             throw new \RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); | ||||
|         } | ||||
| 
 | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads) | ||||
|     { | ||||
|         $cmd_part1 = $this->binary | ||||
|           . ' -y -i ' | ||||
|           . escapeshellarg($this->pathfile) . ' ' | ||||
|  |  | |||
							
								
								
									
										17
									
								
								src/FFMpeg/Format/Audio/Flac.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/FFMpeg/Format/Audio/Flac.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace FFMpeg\Format\Audio; | ||||
| 
 | ||||
| use FFMpeg\Format\DefaultAudioFormat; | ||||
| 
 | ||||
| class Flac extends DefaultAudioFormat | ||||
| { | ||||
| 
 | ||||
|     protected $audioCodec = 'flac'; | ||||
| 
 | ||||
|     protected function getAvailableAudioCodecs() | ||||
|     { | ||||
|         return array('flac'); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										17
									
								
								src/FFMpeg/Format/Audio/Mp3.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/FFMpeg/Format/Audio/Mp3.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace FFMpeg\Format\Audio; | ||||
| 
 | ||||
| use FFMpeg\Format\DefaultAudioFormat; | ||||
| 
 | ||||
| class Mp3 extends DefaultAudioFormat | ||||
| { | ||||
| 
 | ||||
|     protected $audioCodec = 'libmp3lame'; | ||||
| 
 | ||||
|     protected function getAvailableAudioCodecs() | ||||
|     { | ||||
|         return array('libmp3lame'); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -7,7 +7,7 @@ abstract class DefaultAudioFormat implements AudioFormat | |||
| 
 | ||||
|     protected $audioCodec; | ||||
|     protected $audioSampleRate = 44100; | ||||
|     protected $kiloBitrate     = 1000; | ||||
|     protected $kiloBitrate     = 128; | ||||
| 
 | ||||
|     public function getExtraParams() | ||||
|     { | ||||
|  |  | |||
|  | @ -7,9 +7,10 @@ abstract class DefaultVideoFormat extends DefaultAudioFormat implements VideoFor | |||
| 
 | ||||
|     protected $width; | ||||
|     protected $height; | ||||
|     protected $frameRate = 25; | ||||
|     protected $frameRate   = 25; | ||||
|     protected $videoCodec; | ||||
|     protected $GOPsize   = 25; | ||||
|     protected $GOPsize     = 25; | ||||
|     protected $kiloBitrate = 1000; | ||||
| 
 | ||||
|     public function __construct($width, $height) | ||||
|     { | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ | |||
| 
 | ||||
| namespace FFMpeg\Format; | ||||
| 
 | ||||
| interface VideoFormat | ||||
| interface VideoFormat extends AudioFormat | ||||
| { | ||||
| 
 | ||||
|     public function getWidth(); | ||||
|  |  | |||
|  | @ -71,6 +71,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase | |||
|         $this->object->encode(new Format\Video\WebM(32, 32), './invalid.file'); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @expectedException \RuntimeException | ||||
|  | @ -87,6 +88,39 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase | |||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @covers FFMpeg\FFMpeg::encodeAudio | ||||
|      */ | ||||
|     public function testEncodeMp3() | ||||
|     { | ||||
|         $dest = __DIR__ . '/../../files/encode_test.mp3'; | ||||
| 
 | ||||
|         $this->object->open(__DIR__ . '/../../files/Audio.mp3'); | ||||
|         $this->object->encode(new Format\Audio\Mp3(), $dest); | ||||
| 
 | ||||
|         $this->probe->probeFormat($dest); | ||||
| 
 | ||||
|         unlink($dest); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @covers FFMpeg\FFMpeg::encodeAudio | ||||
|      */ | ||||
|     public function testEncodeFlac() | ||||
|     { | ||||
|         $dest = __DIR__ . '/../../files/encode_test.flac'; | ||||
| 
 | ||||
|         $this->object->open(__DIR__ . '/../../files/Audio.mp3'); | ||||
|         $this->object->encode(new Format\Audio\Flac(), $dest); | ||||
| 
 | ||||
|         $this->probe->probeFormat($dest); | ||||
| 
 | ||||
|         unlink($dest); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @covers FFMpeg\FFMpeg::encodeVideo | ||||
|      */ | ||||
|     public function testEncodeWebm() | ||||
|     { | ||||
|  | @ -102,6 +136,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase | |||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @covers FFMpeg\FFMpeg::encodeVideo | ||||
|      */ | ||||
|     public function testEncodeOgg() | ||||
|     { | ||||
|  | @ -117,6 +152,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase | |||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\FFMpeg::encode | ||||
|      * @covers FFMpeg\FFMpeg::encodeVideo | ||||
|      */ | ||||
|     public function testEncodeX264() | ||||
|     { | ||||
|  |  | |||
|  | @ -107,7 +107,7 @@ class DefaultAudioFormatTest extends \PHPUnit_Framework_TestCase | |||
|      */ | ||||
|     public function testGetKiloBitrate() | ||||
|     { | ||||
|         $this->assertEquals(1000, $this->object->getKiloBitrate()); | ||||
|         $this->assertEquals(128, $this->object->getKiloBitrate()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  |  | |||
|  | @ -187,6 +187,14 @@ class DefaultVideoFormatTest extends \PHPUnit_Framework_TestCase | |||
|         $this->assertEquals(16, $this->object->getMultiple(8, 16)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @covers FFMpeg\Format\DefaultVideoFormat::getKiloBitrate | ||||
|      */ | ||||
|     public function testGetKiloBitrate() | ||||
|     { | ||||
|         $this->assertEquals(1000, $this->object->getKiloBitrate()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| class DefaultVideoFormatTester extends DefaultVideoFormat | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue