From 0567c42f07bdd6ebabf3e00dcfb250ee29036e1d Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Sun, 21 Apr 2013 17:41:55 +0200 Subject: [PATCH 1/3] Add timeout option on binary load --- src/FFMpeg/Binary.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/FFMpeg/Binary.php b/src/FFMpeg/Binary.php index 46e2582..9975a11 100644 --- a/src/FFMpeg/Binary.php +++ b/src/FFMpeg/Binary.php @@ -40,7 +40,7 @@ abstract class Binary implements AdapterInterface * * @param type $binary The path file to the binary * @param Logger $logger A logger - * @param Integer $timeout The timout for the underlying process + * @param Integer $timeout The timout for the underlying process, 0 means no timeout */ public function __construct($binary, Logger $logger, $timeout = 60) { @@ -65,11 +65,13 @@ abstract class Binary implements AdapterInterface * {@inheritdoc} * * @param Logger $logger A logger + * @param Integer $timeout The timout for the underlying process, 0 means no timeout + * * @return Binary The binary * * @throws Exception\BinaryNotFoundException */ - public static function load(Logger $logger) + public static function load(Logger $logger, $timeout = 60) { $finder = new ExecutableFinder(); $binary = null; @@ -84,7 +86,7 @@ abstract class Binary implements AdapterInterface throw new BinaryNotFoundException('Binary not found'); } - return new static($binary, $logger); + return new static($binary, $logger, $timeout); } /** From 94c13056e491cafc23667f3caec1a9dff4f5de83 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Sun, 21 Apr 2013 17:52:11 +0200 Subject: [PATCH 2/3] Add timeout getter and setter --- src/FFMpeg/Binary.php | 29 ++++++++++++++++++++++++++++ tests/src/FFMpeg/BinaryTest.php | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/FFMpeg/Binary.php b/src/FFMpeg/Binary.php index 9975a11..36fe496 100644 --- a/src/FFMpeg/Binary.php +++ b/src/FFMpeg/Binary.php @@ -12,6 +12,7 @@ namespace FFMpeg; use FFMpeg\Exception\BinaryNotFoundException; +use FFMpeg\Exception\InvalidArgumentException; use Monolog\Logger; use Symfony\Component\Process\ExecutableFinder; @@ -50,7 +51,35 @@ abstract class Binary implements AdapterInterface $this->binary = $binary; $this->logger = $logger; + $this->setTimeout($timeout); + } + + /** + * Returns the current timeout for underlying processes. + * + * @return integer|float + */ + public function getTimeout() + { + return $this->timeout; + } + + /** + * Sets the timeout for the underlying processes, use 0 to disable timeout. + * + * @param integer|float $timeout + * + * @return Binary + */ + public function setTimeout($timeout) + { + if (0 > $timeout) { + throw new InvalidArgumentException('Timeout must be a positive value'); + } + $this->timeout = $timeout; + + return $this; } /** diff --git a/tests/src/FFMpeg/BinaryTest.php b/tests/src/FFMpeg/BinaryTest.php index 081d482..5863da0 100644 --- a/tests/src/FFMpeg/BinaryTest.php +++ b/tests/src/FFMpeg/BinaryTest.php @@ -29,6 +29,40 @@ class BinaryTest extends \PHPUnit_Framework_TestCase $binary = new BinaryTester('pretty_binary', $this->logger); } + public function testTimeout() + { + $tester = BinaryTester::load($this->logger, 200); + $this->assertEquals(200, $tester->getTimeout()); + } + + public function testDefaultTimeout() + { + $tester = BinaryTester::load($this->logger); + $this->assertEquals(60, $tester->getTimeout()); + } + + public function testNoTimeout() + { + $tester = BinaryTester::load($this->logger, 0); + $this->assertEquals(0, $tester->getTimeout()); + } + + public function testSetTimeout() + { + $tester = BinaryTester::load($this->logger); + $tester->setTimeout(200); + $this->assertEquals(200, $tester->getTimeout()); + } + + /** + * @expectedException \FFMpeg\Exception\InvalidArgumentException + */ + public function testSetInvalidTimeout() + { + $tester = BinaryTester::load($this->logger); + $tester->setTimeout(-1); + } + /** * @covers FFMpeg\Binary::load */ From c642746d85077a1f76a9106af0257046ff75f97b Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Sun, 21 Apr 2013 17:52:19 +0200 Subject: [PATCH 3/3] Add timeout documentation --- docs/source/index.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/source/index.rst b/docs/source/index.rst index d976670..0432ae0 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -82,6 +82,34 @@ Basic Usage ->extractImage(12, 'second-screenshot.jpg') ->close(); +Process Timeout +--------------- + +PHP-FFMpeg runs ffmpeg commands to processs your medias. A default timeout of +60 seconds is set, but you can override this by passing a second argument load : + +.. code-block:: php + + getTimeOut(); + +You can also set and get the timeout with the appropriate getter and setter : + +.. code-block:: php + + setTimeout(200); + + // 200 + echo $ffmpeg->getTimeOut(); + +.. note:: + + To disable timeout, set its value to 0. + Recipes -------