Skip to content

Commit

Permalink
Uniform exceptions for services !
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Jul 9, 2018
1 parent e0e3f0d commit 3077c0d
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 38 deletions.
49 changes: 27 additions & 22 deletions src/Video/Detection/InterlaceDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@

use Soluble\MediaTools\Config\FFMpegConfigInterface;
use Soluble\MediaTools\Exception\FileNotFoundException;
use Soluble\MediaTools\Exception\UnsupportedParamException;
use Soluble\MediaTools\Exception\UnsupportedParamValueException;
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
use Soluble\MediaTools\Util\PlatformNullFile;
use Soluble\MediaTools\Video\Converter\FFMpegAdapter;
use Soluble\MediaTools\Video\Exception\DetectionExceptionInterface;
use Soluble\MediaTools\Video\Exception\DetectionProcessExceptionInterface;
use Soluble\MediaTools\Video\Exception\InvalidParamException;
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
use Soluble\MediaTools\Video\Exception\RuntimeException;
use Soluble\MediaTools\Video\Filter\IdetVideoFilter;
use Soluble\MediaTools\VideoConversionParams;
use Symfony\Component\Process\Exception\RuntimeException as SPRuntimeException;
use Symfony\Component\Process\Exception as SPException;
use Symfony\Component\Process\Process;

class InterlaceDetect
Expand All @@ -33,40 +41,37 @@ public function __construct(FFMpegConfigInterface $ffmpegConfig)
}

/**
* @throws SPRuntimeException
* @throws FileNotFoundException
* @throws DetectionExceptionInterface
* @throws DetectionProcessExceptionInterface
* @throws ProcessFailedException
* @throws MissingInputFileException
* @throws RuntimeException
*/
public function guessInterlacing(string $file, int $maxFramesToAnalyze = self::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceDetectGuess
{
$this->ensureFileExists($file);

$params = (new VideoConversionParams())
->withVideoFilter(new IdetVideoFilter()) // detect interlaced frames :)
->withVideoFrames($maxFramesToAnalyze)
->withNoAudio() // speed up the thing
->withOutputFormat('rawvideo')
->withOverwrite();

$arguments = $this->adapter->getMappedConversionParams($params);
$ffmpegCmd = $this->adapter->getCliCommand($arguments, $file, new PlatformNullFile());

/*
$ffmpegCmd = $ffmpegProcess->buildCommand(
[
sprintf('-i %s', escapeshellarg($file)),
'-filter idet',
sprintf('-frames:v %d', $maxFramesToAnalyze),
'-an', // audio can be discarded
'-f rawvideo', // tmp in raw
'-y /dev/null', // discard the tmp
]
);*/

try {
$this->ensureFileExists($file);

$arguments = $this->adapter->getMappedConversionParams($params);
$ffmpegCmd = $this->adapter->getCliCommand($arguments, $file, new PlatformNullFile());

$process = new Process($ffmpegCmd);
$process->mustRun();
} catch (SPRuntimeException $e) {
throw $e;
} catch (FileNotFoundException $e) {
throw new MissingInputFileException($e->getMessage());
} catch (UnsupportedParamValueException | UnsupportedParamException $e) {
throw new InvalidParamException($e->getMessage());
} catch (SPException\ProcessFailedException | SPException\ProcessTimedOutException | SPException\ProcessSignaledException $e) {
throw new ProcessFailedException($e->getProcess(), $e);
} catch (SPException\RuntimeException $e) {
throw new RuntimeException($e->getMessage());
}

$stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
Expand Down
11 changes: 11 additions & 0 deletions src/Video/Exception/DetectionExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Soluble\MediaTools\Video\Exception;

use Soluble\MediaTools\Exception\ExceptionInterface;

interface DetectionExceptionInterface extends ExceptionInterface
{
}
11 changes: 11 additions & 0 deletions src/Video/Exception/DetectionProcessExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Soluble\MediaTools\Video\Exception;

use Soluble\MediaTools\Exception\ProcessExceptionInterface;

interface DetectionProcessExceptionInterface extends ProcessExceptionInterface, DetectionExceptionInterface
{
}
11 changes: 11 additions & 0 deletions src/Video/Exception/InfoExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Soluble\MediaTools\Video\Exception;

use Soluble\MediaTools\Exception\ExceptionInterface;

interface InfoExceptionInterface extends ExceptionInterface
{
}
11 changes: 11 additions & 0 deletions src/Video/Exception/InfoProcessExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Soluble\MediaTools\Video\Exception;

use Soluble\MediaTools\Exception\ProcessExceptionInterface;

interface InfoProcessExceptionInterface extends ProcessExceptionInterface, InfoExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Video/Exception/InvalidParamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use Soluble\MediaTools\Exception\RuntimeException;

class InvalidParamException extends RuntimeException implements ConversionExceptionInterface
class InvalidParamException extends RuntimeException implements ConversionExceptionInterface, DetectionExceptionInterface, InfoExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Video/Exception/MissingInputFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use Soluble\MediaTools\Exception\FileNotFoundException;

class MissingInputFileException extends FileNotFoundException implements ConversionExceptionInterface
class MissingInputFileException extends FileNotFoundException implements ConversionExceptionInterface, DetectionExceptionInterface, InfoExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Video/Exception/ProcessFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use Soluble\MediaTools\Exception\ProcessException;

class ProcessFailedException extends ProcessException implements ConversionExceptionInterface, ConversionProcessExceptionInterface
class ProcessFailedException extends ProcessException implements ConversionProcessExceptionInterface, DetectionProcessExceptionInterface, InfoProcessExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Video/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Soluble\MediaTools\Video\Exception;

class RuntimeException extends \Soluble\MediaTools\Exception\RuntimeException implements ConversionExceptionInterface
class RuntimeException extends \Soluble\MediaTools\Exception\RuntimeException implements ConversionExceptionInterface, DetectionExceptionInterface, InfoExceptionInterface
{
}
14 changes: 10 additions & 4 deletions src/VideoDetectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Soluble\MediaTools;

use Soluble\MediaTools\Config\FFMpegConfigInterface;
use Soluble\MediaTools\Exception\FileNotFoundException;
use Soluble\MediaTools\Video\Detection\InterlaceDetect;
use Soluble\MediaTools\Video\Detection\InterlaceDetectGuess;
use Soluble\MediaTools\Video\DetectionServiceInterface;
use Symfony\Component\Process\Exception\RuntimeException as SPRuntimeException;
use Soluble\MediaTools\Video\Exception\DetectionExceptionInterface;
use Soluble\MediaTools\Video\Exception\DetectionProcessExceptionInterface;
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
use Soluble\MediaTools\Video\Exception\RuntimeException;

class VideoDetectionService implements DetectionServiceInterface
{
Expand All @@ -24,8 +27,11 @@ public function __construct(FFMpegConfigInterface $ffmpegConfig)
/**
* @param int $maxFramesToAnalyze interlacement detection can be heavy, limit the number of frames to analyze
*
* @throws SPRuntimeException
* @throws FileNotFoundException
* @throws DetectionExceptionInterface
* @throws DetectionProcessExceptionInterface
* @throws ProcessFailedException
* @throws MissingInputFileException
* @throws RuntimeException
*/
public function detectInterlacement(string $file, int $maxFramesToAnalyze = InterlaceDetect::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceDetectGuess
{
Expand Down
28 changes: 20 additions & 8 deletions src/VideoInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
use Soluble\MediaTools\Config\FFProbeConfigInterface;
use Soluble\MediaTools\Exception\FileNotFoundException;
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
use Soluble\MediaTools\Video\Exception\InfoExceptionInterface;
use Soluble\MediaTools\Video\Exception\InfoProcessExceptionInterface;
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
use Soluble\MediaTools\Video\Exception\RuntimeException;
use Soluble\MediaTools\Video\Info;
use Soluble\MediaTools\Video\InfoServiceInterface;
use Symfony\Component\Process\Exception as SPException;
use Symfony\Component\Process\Process;

class VideoInfoService implements InfoServiceInterface
Expand All @@ -34,8 +40,6 @@ public function __construct(FFProbeConfigInterface $ffProbeConfig)
*/
public function getFFProbeProcess(string $inputFile): Process
{
$this->ensureFileExists($inputFile);

$ffprobeCmd = trim(sprintf(
'%s %s %s',
$this->ffprobeConfig->getBinary(),
Expand All @@ -57,18 +61,26 @@ public function getFFProbeProcess(string $inputFile): Process
}

/**
* @throws FileNotFoundException
* @throws \Throwable
* @throws InfoExceptionInterface
* @throws InfoProcessExceptionInterface
* @throws ProcessFailedException
* @throws MissingInputFileException
* @throws RuntimeException
*/
public function getInfo(string $file): Info
{
$process = $this->getFFProbeProcess($file);

try {
$this->ensureFileExists($file);
$process = $this->getFFProbeProcess($file);

$process->mustRun();
$output = $process->getOutput();
} catch (\Throwable $e) {
throw $e;
} catch (FileNotFoundException $e) {
throw new MissingInputFileException($e->getMessage());
} catch (SPException\ProcessFailedException | SPException\ProcessTimedOutException | SPException\ProcessSignaledException $e) {
throw new ProcessFailedException($e->getProcess(), $e);
} catch (SPException\RuntimeException $e) {
throw new RuntimeException($e->getMessage());
}

return Info::createFromFFProbeJson($file, $output);
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/UseCases/VideoInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function testGetInfo(): void
{
$videoInfo = $this->infoService->getInfo($this->videoFile);
self::assertEquals(61.533000, $videoInfo->getDuration());

self::assertEquals(realpath($this->videoFile), realpath($videoInfo->getFile()));
self::assertEquals(1113, $videoInfo->getNbFrames());
}

public function testGetMEdiaInfoThrowsFileNotFoundException(): void
Expand Down

0 comments on commit 3077c0d

Please sign in to comment.