From 94775176c93936ad40f1b8617b8b103282102b73 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 13 Apr 2012 12:45:41 +0200 Subject: [PATCH] Add Audio and Video interfaces --- src/FFMpeg/FFMpeg.php | 2 +- src/FFMpeg/Format/AudioFormat.php | 14 ++ src/FFMpeg/Format/DefaultAudioFormat.php | 64 ++++++++ ...faultFormat.php => DefaultVideoFormat.php} | 61 +------ src/FFMpeg/Format/{ => Video}/Ogg.php | 6 +- src/FFMpeg/Format/{ => Video}/WebM.php | 6 +- src/FFMpeg/Format/{ => Video}/X264.php | 6 +- .../Format/{Format.php => VideoFormat.php} | 10 +- tests/src/FFMpeg/FFMpegTest.php | 11 +- .../FFMpeg/Format/DefaultAudioFormatTest.php | 154 ++++++++++++++++++ ...matTest.php => DefaultVideoFormatTest.php} | 149 +++-------------- .../src/FFMpeg/Format/{ => Video}/OggTest.php | 10 +- .../FFMpeg/Format/{ => Video}/WebMTest.php | 12 +- .../FFMpeg/Format/{ => Video}/X264Test.php | 11 +- 14 files changed, 294 insertions(+), 222 deletions(-) create mode 100644 src/FFMpeg/Format/AudioFormat.php create mode 100644 src/FFMpeg/Format/DefaultAudioFormat.php rename src/FFMpeg/Format/{DefaultFormat.php => DefaultVideoFormat.php} (64%) rename src/FFMpeg/Format/{ => Video}/Ogg.php (72%) rename src/FFMpeg/Format/{ => Video}/WebM.php (76%) rename src/FFMpeg/Format/{ => Video}/X264.php (74%) rename src/FFMpeg/Format/{Format.php => VideoFormat.php} (54%) create mode 100644 tests/src/FFMpeg/Format/DefaultAudioFormatTest.php rename tests/src/FFMpeg/Format/{DefaultFormatTest.php => DefaultVideoFormatTest.php} (53%) rename tests/src/FFMpeg/Format/{ => Video}/OggTest.php (63%) rename tests/src/FFMpeg/Format/{ => Video}/WebMTest.php (65%) rename tests/src/FFMpeg/Format/{ => Video}/X264Test.php (63%) diff --git a/src/FFMpeg/FFMpeg.php b/src/FFMpeg/FFMpeg.php index b354eab..822e495 100644 --- a/src/FFMpeg/FFMpeg.php +++ b/src/FFMpeg/FFMpeg.php @@ -54,7 +54,7 @@ class FFMpeg extends Binary return true; } - public function encode(Format\Format $format, $outputPathfile, $threads = 1) + public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1) { if ( ! $this->pathfile) { diff --git a/src/FFMpeg/Format/AudioFormat.php b/src/FFMpeg/Format/AudioFormat.php new file mode 100644 index 0000000..8bdb071 --- /dev/null +++ b/src/FFMpeg/Format/AudioFormat.php @@ -0,0 +1,14 @@ +audioCodec; + } + + public function setAudioCodec($audioCodec) + { + if ( ! in_array($audioCodec, $this->getAvailableAudioCodecs())) + { + throw new \InvalidArgumentException('Wrong audiocodec value'); + } + + $this->audioCodec = $audioCodec; + } + + public function getAudioSampleRate() + { + return $this->audioSampleRate; + } + + public function setAudioSampleRate($audioSampleRate) + { + if ($audioSampleRate < 1) + { + throw new \InvalidArgumentException('Wrong audio sample rate value'); + } + + $this->audioSampleRate = (int) $audioSampleRate; + } + + public function getKiloBitrate() + { + return $this->kiloBitrate; + } + + public function setKiloBitrate($kiloBitrate) + { + if ($kiloBitrate < 1) + { + throw new \InvalidArgumentException('Wrong kiloBitrate value'); + } + + $this->kiloBitrate = (int) $kiloBitrate; + } + + abstract protected function getAvailableAudioCodecs(); + +} diff --git a/src/FFMpeg/Format/DefaultFormat.php b/src/FFMpeg/Format/DefaultVideoFormat.php similarity index 64% rename from src/FFMpeg/Format/DefaultFormat.php rename to src/FFMpeg/Format/DefaultVideoFormat.php index 6f4ccdb..5654a58 100644 --- a/src/FFMpeg/Format/DefaultFormat.php +++ b/src/FFMpeg/Format/DefaultVideoFormat.php @@ -2,28 +2,20 @@ namespace FFMpeg\Format; -abstract class DefaultFormat implements Format +abstract class DefaultVideoFormat extends DefaultAudioFormat implements VideoFormat { protected $width; protected $height; - protected $frameRate = 25; - protected $audioCodec; - protected $audioSampleRate = 44100; + protected $frameRate = 25; protected $videoCodec; - protected $kiloBitrate = 1000; - protected $GOPsize = 25; + protected $GOPsize = 25; public function __construct($width, $height) { $this->setDimensions($width, $height); } - public function getExtraParams() - { - return ''; - } - public function getWidth() { return $this->width; @@ -66,36 +58,6 @@ abstract class DefaultFormat implements Format $this->frameRate = (int) $frameRate; } - public function getAudioCodec() - { - return $this->audioCodec; - } - - public function setAudioCodec($audioCodec) - { - if ( ! in_array($audioCodec, $this->getAvailableAudioCodecs())) - { - throw new \InvalidArgumentException('Wrong audiocodec value'); - } - - $this->audioCodec = $audioCodec; - } - - public function getAudioSampleRate() - { - return $this->audioSampleRate; - } - - public function setAudioSampleRate($audioSampleRate) - { - if ($audioSampleRate < 1) - { - throw new \InvalidArgumentException('Wrong audio sample rate value'); - } - - $this->audioSampleRate = (int) $audioSampleRate; - } - public function getVideoCodec() { return $this->videoCodec; @@ -111,21 +73,6 @@ abstract class DefaultFormat implements Format $this->videoCodec = $videoCodec; } - public function getKiloBitrate() - { - return $this->kiloBitrate; - } - - public function setKiloBitrate($kiloBitrate) - { - if ($kiloBitrate < 1) - { - throw new \InvalidArgumentException('Wrong kiloBitrate value'); - } - - $this->kiloBitrate = (int) $kiloBitrate; - } - public function getGOPsize() { return $this->GOPsize; @@ -172,8 +119,6 @@ abstract class DefaultFormat implements Format return (int) $ret; } - abstract protected function getAvailableAudioCodecs(); - abstract protected function getAvailableVideoCodecs(); } \ No newline at end of file diff --git a/src/FFMpeg/Format/Ogg.php b/src/FFMpeg/Format/Video/Ogg.php similarity index 72% rename from src/FFMpeg/Format/Ogg.php rename to src/FFMpeg/Format/Video/Ogg.php index fff04a8..765efae 100644 --- a/src/FFMpeg/Format/Ogg.php +++ b/src/FFMpeg/Format/Video/Ogg.php @@ -1,8 +1,10 @@ object->encode(new Format\WebM(32, 32), './invalid.file'); + $this->object->encode(new Format\Video\WebM(32, 32), './invalid.file'); } /** @@ -83,7 +82,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase $ffmpeg = new FFMpeg('wrongbinary', $logger); $ffmpeg->open(__DIR__ . '/../../files/Test.ogv'); - $ffmpeg->encode(new Format\WebM(32, 32), './invalid.file'); + $ffmpeg->encode(new Format\Video\WebM(32, 32), './invalid.file'); } /** @@ -94,7 +93,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase $dest = __DIR__ . '/../../files/encode_test.webm'; $this->object->open(__DIR__ . '/../../files/Test.ogv'); - $this->object->encode(new Format\WebM(32, 32), $dest); + $this->object->encode(new Format\Video\WebM(32, 32), $dest); $this->probe->probeFormat($dest); @@ -109,7 +108,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase $dest = __DIR__ . '/../../files/encode_test.ogv'; $this->object->open(__DIR__ . '/../../files/Test.ogv'); - $this->object->encode(new Format\Ogg(32, 32), $dest); + $this->object->encode(new Format\Video\Ogg(32, 32), $dest); $this->probe->probeFormat($dest); @@ -124,7 +123,7 @@ class FFMpegTest extends \PHPUnit_Framework_TestCase $dest = __DIR__ . '/../../files/encode_test.mp4'; $this->object->open(__DIR__ . '/../../files/Test.ogv'); - $this->object->encode(new Format\X264(32, 32), $dest); + $this->object->encode(new Format\Video\X264(32, 32), $dest); $this->probe->probeFormat($dest); diff --git a/tests/src/FFMpeg/Format/DefaultAudioFormatTest.php b/tests/src/FFMpeg/Format/DefaultAudioFormatTest.php new file mode 100644 index 0000000..2a27cf0 --- /dev/null +++ b/tests/src/FFMpeg/Format/DefaultAudioFormatTest.php @@ -0,0 +1,154 @@ +object = new DefaultAudioFormatTester(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::getExtraParams + */ + public function testGetExtraParams() + { + $this->assertEquals('-f format', $this->object->getExtraParams()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::getAudioCodec + */ + public function testGetAudioCodec() + { + $this->assertEquals('audiocodec1', $this->object->getAudioCodec()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setAudioCodec + */ + public function testSetAudioCodec() + { + $this->object->setAudioCodec('audiocodec2'); + $this->assertEquals('audiocodec2', $this->object->getAudioCodec()); + $this->object->setAudioCodec('audiocodec1'); + $this->assertEquals('audiocodec1', $this->object->getAudioCodec()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setAudioCodec + * @expectedException \InvalidArgumentException + */ + public function testSetWrongAudioCodec() + { + $this->object->setAudioCodec('audiocodec4'); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::getAudioSampleRate + */ + public function testGetAudioSampleRate() + { + $this->assertEquals(44100, $this->object->getAudioSampleRate()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setAudioSampleRate + */ + public function testSetAudioSampleRate() + { + $this->object->setAudioSampleRate(22050); + $this->assertEquals(22050, $this->object->getAudioSampleRate()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setAudioSampleRate + * @expectedException \InvalidArgumentException + * @dataProvider getWrongAudioSampleRate + */ + public function testSetWrongAudioSampleRate($samplerate) + { + $this->object->setAudioSampleRate($samplerate); + } + + public function getWrongAudioSampleRate() + { + return array(array(-5), array(0)); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::getKiloBitrate + */ + public function testGetKiloBitrate() + { + $this->assertEquals(1000, $this->object->getKiloBitrate()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setKiloBitrate + */ + public function testSetKiloBitrate() + { + $this->object->setKiloBitrate(500); + $this->assertEquals(500, $this->object->getKiloBitrate()); + } + + /** + * @covers FFMpeg\Format\DefaultAudioFormat::setKiloBitrate + * @dataProvider getWrongKiloBitrate + * @expectedException \InvalidArgumentException + */ + public function testSetWrongKiloBitrate($kbrate) + { + $this->object->setKiloBitrate($kbrate); + } + + public function getWrongKiloBitrate() + { + return array(array(-5), array(0)); + } + +} + +class DefaultAudioFormatTester extends DefaultAudioFormat +{ + + protected $audioCodec = 'audiocodec1'; + + protected function getAvailableAudioCodecs() + { + return array('audiocodec1', 'audiocodec2', 'audiocodec3'); + } + + public function getExtraParams() + { + return '-f format'; + } + +} diff --git a/tests/src/FFMpeg/Format/DefaultFormatTest.php b/tests/src/FFMpeg/Format/DefaultVideoFormatTest.php similarity index 53% rename from tests/src/FFMpeg/Format/DefaultFormatTest.php rename to tests/src/FFMpeg/Format/DefaultVideoFormatTest.php index 2426fe2..bc010b2 100644 --- a/tests/src/FFMpeg/Format/DefaultFormatTest.php +++ b/tests/src/FFMpeg/Format/DefaultVideoFormatTest.php @@ -2,29 +2,33 @@ namespace FFMpeg\Format; -require_once dirname(__FILE__) . '/../../../../src/FFMpeg/Format/DefaultFormat.php'; +require_once dirname(__FILE__) . '/../../../../src/FFMpeg/Format/DefaultVideoFormat.php'; /** - * Test class for DefaultFormat. - * Generated by PHPUnit on 2012-04-13 at 11:03:02. + * Test class for DefaultVideoFormat. + * Generated by PHPUnit on 2012-04-13 at 12:32:40. */ -class DefaultFormatTest extends \PHPUnit_Framework_TestCase +class DefaultVideoFormatTest extends \PHPUnit_Framework_TestCase { /** - * @var DefaultFormat + * @var DefaultVideoFormat */ protected $object; + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ protected function setUp() { - $this->object = new DefaultFormatTester(320, 240); + $this->object = new DefaultVideoFormatTester(320, 240); } /** - * @covers FFMpeg\Format\DefaultFormat::__construct - * @covers FFMpeg\Format\DefaultFormat::getWidth - * @covers FFMpeg\Format\DefaultFormat::getHeight + * @covers FFMpeg\Format\DefaultVideoFormat::__construct + * @covers FFMpeg\Format\DefaultVideoFormat::getWidth + * @covers FFMpeg\Format\DefaultVideoFormat::getHeight */ public function testConstruct() { @@ -33,15 +37,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::getExtraParams - */ - public function testGetExtraParams() - { - $this->assertEquals('-f format', $this->object->getExtraParams()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setDimensions + * @covers FFMpeg\Format\DefaultVideoFormat::setDimensions */ public function testSetDimensions() { @@ -55,7 +51,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setDimensions + * @covers FFMpeg\Format\DefaultVideoFormat::setDimensions * @dataProvider getWrongDimensions * @expectedException \InvalidArgumentException */ @@ -82,7 +78,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::getFrameRate + * @covers FFMpeg\Format\DefaultVideoFormat::getFrameRate */ public function testGetFrameRate() { @@ -90,7 +86,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setFrameRate + * @covers FFMpeg\Format\DefaultVideoFormat::setFrameRate */ public function testSetFrameRate() { @@ -99,7 +95,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setFrameRate + * @covers FFMpeg\Format\DefaultVideoFormat::setFrameRate * @dataProvider getWrongFrameRates * @expectedException \InvalidArgumentException */ @@ -119,67 +115,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::getAudioCodec - */ - public function testGetAudioCodec() - { - $this->assertEquals('audiocodec1', $this->object->getAudioCodec()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setAudioCodec - */ - public function testSetAudioCodec() - { - $this->object->setAudioCodec('audiocodec2'); - $this->assertEquals('audiocodec2', $this->object->getAudioCodec()); - $this->object->setAudioCodec('audiocodec1'); - $this->assertEquals('audiocodec1', $this->object->getAudioCodec()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setAudioCodec - * @expectedException \InvalidArgumentException - */ - public function testSetWrongAudioCodec() - { - $this->object->setAudioCodec('audiocodec4'); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::getAudioSampleRate - */ - public function testGetAudioSampleRate() - { - $this->assertEquals(44100, $this->object->getAudioSampleRate()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setAudioSampleRate - */ - public function testSetAudioSampleRate() - { - $this->object->setAudioSampleRate(22050); - $this->assertEquals(22050, $this->object->getAudioSampleRate()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setAudioSampleRate - * @expectedException \InvalidArgumentException - * @dataProvider getWrongAudioSampleRate - */ - public function testSetWrongAudioSampleRate($samplerate) - { - $this->object->setAudioSampleRate($samplerate); - } - - public function getWrongAudioSampleRate() - { - return array(array(-5), array(0)); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::getVideoCodec + * @covers FFMpeg\Format\DefaultVideoFormat::getVideoCodec */ public function testGetVideoCodec() { @@ -187,7 +123,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setVideoCodec + * @covers FFMpeg\Format\DefaultVideoFormat::setVideoCodec */ public function testSetVideoCodec() { @@ -198,7 +134,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setVideoCodec + * @covers FFMpeg\Format\DefaultVideoFormat::setVideoCodec * @expectedException \InvalidArgumentException */ public function testSetWrongVideoCodec() @@ -207,39 +143,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::getKiloBitrate - */ - public function testGetKiloBitrate() - { - $this->assertEquals(1000, $this->object->getKiloBitrate()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setKiloBitrate - */ - public function testSetKiloBitrate() - { - $this->object->setKiloBitrate(500); - $this->assertEquals(500, $this->object->getKiloBitrate()); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::setKiloBitrate - * @dataProvider getWrongKiloBitrate - * @expectedException \InvalidArgumentException - */ - public function testSetWrongKiloBitrate($kbrate) - { - $this->object->setKiloBitrate($kbrate); - } - - public function getWrongKiloBitrate() - { - return array(array(-5), array(0)); - } - - /** - * @covers FFMpeg\Format\DefaultFormat::getGOPsize + * @covers FFMpeg\Format\DefaultVideoFormat::getGOPsize */ public function testGetGOPsize() { @@ -247,7 +151,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setGOPsize + * @covers FFMpeg\Format\DefaultVideoFormat::setGOPsize */ public function testSetGOPsize() { @@ -256,7 +160,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::setGOPsize + * @covers FFMpeg\Format\DefaultVideoFormat::setGOPsize * @dataProvider getWrongGOPsize * @expectedException \InvalidArgumentException */ @@ -271,7 +175,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\DefaultFormat::getMultiple + * @covers FFMpeg\Format\DefaultVideoFormat::getMultiple */ public function testGetMultiple() { @@ -285,7 +189,7 @@ class DefaultFormatTest extends \PHPUnit_Framework_TestCase } -class DefaultFormatTester extends DefaultFormat +class DefaultVideoFormatTester extends DefaultVideoFormat { protected $audioCodec = 'audiocodec1'; @@ -312,3 +216,4 @@ class DefaultFormatTester extends DefaultFormat } } + diff --git a/tests/src/FFMpeg/Format/OggTest.php b/tests/src/FFMpeg/Format/Video/OggTest.php similarity index 63% rename from tests/src/FFMpeg/Format/OggTest.php rename to tests/src/FFMpeg/Format/Video/OggTest.php index 77ee711..7084bca 100644 --- a/tests/src/FFMpeg/Format/OggTest.php +++ b/tests/src/FFMpeg/Format/Video/OggTest.php @@ -1,8 +1,6 @@ assertInstanceOf('\\FFMpeg\\Format\\DefaultFormat', $this->object); + $this->assertInstanceOf('\\FFMpeg\\Format\\DefaultVideoFormat', $this->object); } /** - * @covers FFMpeg\Format\Ogg::getAvailableAudioCodecs + * @covers FFMpeg\Format\Video\Ogg::getAvailableAudioCodecs */ public function testGetAvailableAudioCodecs() { @@ -31,7 +29,7 @@ class OggTest extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\Ogg::getAvailableVideoCodecs + * @covers FFMpeg\Format\Video\Ogg::getAvailableVideoCodecs */ public function testGetAvailableVideoCodecs() { diff --git a/tests/src/FFMpeg/Format/WebMTest.php b/tests/src/FFMpeg/Format/Video/WebMTest.php similarity index 65% rename from tests/src/FFMpeg/Format/WebMTest.php rename to tests/src/FFMpeg/Format/Video/WebMTest.php index 8a7571a..ffb7abf 100644 --- a/tests/src/FFMpeg/Format/WebMTest.php +++ b/tests/src/FFMpeg/Format/Video/WebMTest.php @@ -1,8 +1,6 @@ assertInstanceOf('\\FFMpeg\\Format\\DefaultFormat', $this->object); + $this->assertInstanceOf('\\FFMpeg\\Format\\DefaultVideoFormat', $this->object); } } diff --git a/tests/src/FFMpeg/Format/X264Test.php b/tests/src/FFMpeg/Format/Video/X264Test.php similarity index 63% rename from tests/src/FFMpeg/Format/X264Test.php rename to tests/src/FFMpeg/Format/Video/X264Test.php index 2521002..5a69438 100644 --- a/tests/src/FFMpeg/Format/X264Test.php +++ b/tests/src/FFMpeg/Format/Video/X264Test.php @@ -1,8 +1,6 @@ assertInstanceOf('\\FFMpeg\\Format\\DefaultFormat', $this->object); + $this->assertInstanceOf('\\FFMpeg\\Format\\DefaultVideoFormat', $this->object); } /** - * @covers FFMpeg\Format\X264::getAvailableAudioCodecs + * @covers FFMpeg\Format\Video\X264::getAvailableAudioCodecs */ public function testGetAvailableAudioCodecs() { @@ -31,12 +29,11 @@ class X264Test extends \PHPUnit_Framework_TestCase } /** - * @covers FFMpeg\Format\X264::getAvailableVideoCodecs + * @covers FFMpeg\Format\Video\X264::getAvailableVideoCodecs */ public function testGetAvailableVideoCodecs() { $this->object->setVideoCodec('libx264'); } - }