From ad2b9e75a5c2bd57043f1c5ef5d08ad13ec2adb5 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 29 Nov 2013 11:14:49 +0100 Subject: [PATCH] Fix #76 : Remove restriction on filesystem resources --- CHANGELOG.md | 1 + src/FFMpeg/FFMpeg.php | 7 +++---- .../AbstractProgressListener.php | 11 ++++++++++- src/FFMpeg/Media/AbstractMediaType.php | 11 ----------- .../FFMpeg/Functional/VideoTranscodeTest.php | 19 +++++++++++++++++++ tests/FFMpeg/Tests/FFMpegTest.php | 5 +++-- .../Tests/Media/AbstractMediaTestCase.php | 1 - tests/FFMpeg/Tests/Media/AudioTest.php | 11 ----------- tests/FFMpeg/Tests/Media/FrameTest.php | 8 -------- tests/FFMpeg/Tests/Media/VideoTest.php | 11 ----------- 10 files changed, 36 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7535cde..26d9950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG * Add Rotate filter. * Remove time_start metadata when using synchronize filter + * Remove restriction on filesystem resources. * 0.4.1 (11-26-2013) diff --git a/src/FFMpeg/FFMpeg.php b/src/FFMpeg/FFMpeg.php index 1abc05a..91ce361 100644 --- a/src/FFMpeg/FFMpeg.php +++ b/src/FFMpeg/FFMpeg.php @@ -14,6 +14,7 @@ namespace FFMpeg; use Alchemy\BinaryDriver\ConfigurationInterface; use FFMpeg\Driver\FFMpegDriver; use FFMpeg\Exception\InvalidArgumentException; +use FFMpeg\Exception\RuntimeException; use FFMpeg\Media\Audio; use FFMpeg\Media\Video; use Psr\Log\LoggerInterface; @@ -88,12 +89,10 @@ class FFMpeg */ public function open($pathfile) { - if (!file_exists($pathfile)) { - throw new InvalidArgumentException(sprintf('File %s does not exists', $pathfile)); + if (null === $streams = $this->ffprobe->streams($pathfile)) { + throw new RuntimeException(sprintf('Unable to probe "%s".', $pathfile)); } - $streams = $this->ffprobe->streams($pathfile); - if (0 < count($streams->videos())) { return new Video($pathfile, $this->driver, $this->ffprobe); } elseif (0 < count($streams->audios())) { diff --git a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php index 8636825..cda6815 100644 --- a/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php +++ b/src/FFMpeg/Format/ProgressListener/AbstractProgressListener.php @@ -154,6 +154,10 @@ abstract class AbstractProgressListener extends EventEmitter implements Listener $this->initialize(); } + if (null === $this->totalSize || null === $this->duration) { + return; + } + $matches = array(); if (preg_match($this->getPattern(), $progress, $matches) !== 1) { @@ -226,9 +230,14 @@ abstract class AbstractProgressListener extends EventEmitter implements Listener private function initialize() { - $format = $this->ffprobe->format($this->pathfile); + try { + $format = $this->ffprobe->format($this->pathfile); + } catch (RuntimeException $e) { + return; + } if (false === $format->has('size') || false === $format->has('duration')) { + return; throw new RuntimeException(sprintf('Unable to probe format for %s', $this->pathfile)); } diff --git a/src/FFMpeg/Media/AbstractMediaType.php b/src/FFMpeg/Media/AbstractMediaType.php index 4cb3345..879265b 100644 --- a/src/FFMpeg/Media/AbstractMediaType.php +++ b/src/FFMpeg/Media/AbstractMediaType.php @@ -30,8 +30,6 @@ abstract class AbstractMediaType implements MediaTypeInterface public function __construct($pathfile, FFMpegDriver $driver, FFProbe $ffprobe) { - $this->ensureFileIsPresent($pathfile); - $this->pathfile = $pathfile; $this->driver = $driver; $this->ffprobe = $ffprobe; @@ -106,15 +104,6 @@ abstract class AbstractMediaType implements MediaTypeInterface return $this->filters; } - protected function ensureFileIsPresent($filename) - { - if (!is_file($filename) || !is_readable($filename)) { - throw new InvalidArgumentException(sprintf( - '%s is not present or not readable', $filename - )); - } - } - protected function cleanupTemporaryFile($filename) { if (file_exists($filename) && is_writable($filename)) { diff --git a/tests/FFMpeg/Functional/VideoTranscodeTest.php b/tests/FFMpeg/Functional/VideoTranscodeTest.php index 2b9ff24..38e230e 100644 --- a/tests/FFMpeg/Functional/VideoTranscodeTest.php +++ b/tests/FFMpeg/Functional/VideoTranscodeTest.php @@ -3,6 +3,7 @@ namespace FFMpeg\Functional; use FFMpeg\Format\Video\X264; +use FFMpeg\Media\Video; class VideoTranscodeTest extends FunctionalTestCase { @@ -35,4 +36,22 @@ class VideoTranscodeTest extends FunctionalTestCase $this->assertFileExists($filename); unlink($filename); } + + /** + * @expectedException \FFMpeg\Exception\RuntimeException + */ + public function testTranscodeInvalidFile() + { + $ffmpeg = $this->getFFMpeg(); + $ffmpeg->open(__DIR__ . '/../../files/UnknownFileTest.ogv'); + } + + public function testSaveInvalidForgedVideo() + { + $ffmpeg = $this->getFFMpeg(); + $video = new Video(__DIR__ . '/../../files/UnknownFileTest.ogv', $ffmpeg->getFFMpegDriver(), $ffmpeg->getFFProbe()); + + $this->setExpectedException('FFMpeg\Exception\RuntimeException'); + $video->save(new X264('libvo_aacenc'), __DIR__ . '/output/output-x264.mp4'); + } } diff --git a/tests/FFMpeg/Tests/FFMpegTest.php b/tests/FFMpeg/Tests/FFMpegTest.php index 6695df0..f9e8779 100644 --- a/tests/FFMpeg/Tests/FFMpegTest.php +++ b/tests/FFMpeg/Tests/FFMpegTest.php @@ -10,7 +10,8 @@ use FFMpeg\FFProbe\DataMapping\Stream; class FFMpegTest Extends TestCase { /** - * @expectedException FFMpeg\Exception\InvalidArgumentException + * @expectedException \FFMpeg\Exception\RuntimeException + * @expectedExceptionMessage Unable to probe "/path/to/unknown/file". */ public function testOpenInvalid() { @@ -58,7 +59,7 @@ class FFMpegTest Extends TestCase } /** - * @expectedException FFMpeg\Exception\InvalidArgumentException + * @expectedException \FFMpeg\Exception\InvalidArgumentException */ public function testOpenUnknown() { diff --git a/tests/FFMpeg/Tests/Media/AbstractMediaTestCase.php b/tests/FFMpeg/Tests/Media/AbstractMediaTestCase.php index 1af48a5..c30f198 100644 --- a/tests/FFMpeg/Tests/Media/AbstractMediaTestCase.php +++ b/tests/FFMpeg/Tests/Media/AbstractMediaTestCase.php @@ -6,5 +6,4 @@ use FFMpeg\Tests\TestCase; abstract class AbstractMediaTestCase extends TestCase { - abstract public function testWithInvalidFile(); } diff --git a/tests/FFMpeg/Tests/Media/AudioTest.php b/tests/FFMpeg/Tests/Media/AudioTest.php index 81b956a..2f37942 100644 --- a/tests/FFMpeg/Tests/Media/AudioTest.php +++ b/tests/FFMpeg/Tests/Media/AudioTest.php @@ -9,17 +9,6 @@ use FFMpeg\Format\AudioInterface; class AudioTest extends AbstractStreamableTestCase { - /** - * @expectedException FFMpeg\Exception\InvalidArgumentException - */ - public function testWithInvalidFile() - { - $driver = $this->getFFMpegDriverMock(); - $ffprobe = $this->getFFProbeMock(); - - new Audio('/no/file', $driver, $ffprobe); - } - public function testFiltersReturnsAudioFilters() { $driver = $this->getFFMpegDriverMock(); diff --git a/tests/FFMpeg/Tests/Media/FrameTest.php b/tests/FFMpeg/Tests/Media/FrameTest.php index b64d6e9..8f9af04 100644 --- a/tests/FFMpeg/Tests/Media/FrameTest.php +++ b/tests/FFMpeg/Tests/Media/FrameTest.php @@ -6,14 +6,6 @@ use FFMpeg\Media\Frame; class FrameTest extends AbstractMediaTestCase { - /** - * @expectedException FFMpeg\Exception\InvalidArgumentException - */ - public function testWithInvalidFile() - { - new Frame($this->getVideoMock('/No/file'), $this->getFFMpegDriverMock(), $this->getFFProbeMock(), $this->getTimeCodeMock()); - } - public function testGetTimeCode() { $driver = $this->getFFMpegDriverMock(); diff --git a/tests/FFMpeg/Tests/Media/VideoTest.php b/tests/FFMpeg/Tests/Media/VideoTest.php index a2c776e..9f5e84c 100644 --- a/tests/FFMpeg/Tests/Media/VideoTest.php +++ b/tests/FFMpeg/Tests/Media/VideoTest.php @@ -9,17 +9,6 @@ use FFMpeg\Format\VideoInterface; class VideoTest extends AbstractStreamableTestCase { - /** - * @expectedException FFMpeg\Exception\InvalidArgumentException - */ - public function testWithInvalidFile() - { - $driver = $this->getFFMpegDriverMock(); - $ffprobe = $this->getFFProbeMock(); - - new Video('/no/file', $driver, $ffprobe); - } - public function testFiltersReturnsVideoFilters() { $driver = $this->getFFMpegDriverMock();