From 78bb1bd86e76f2fa243a980e04e74b25c31e9463 Mon Sep 17 00:00:00 2001 From: grosroro Date: Wed, 30 May 2012 15:06:53 +0200 Subject: [PATCH] Rename interfaces --- src/FFMpeg/FFMpeg.php | 45 +++++++++++++------ src/FFMpeg/Format/Audio.php | 15 ++++--- src/FFMpeg/Format/Audio/DefaultAudio.php | 2 +- .../{InteractiveAudio.php => Interactive.php} | 4 +- .../{ResamplableAudio.php => Resamplable.php} | 2 +- src/FFMpeg/Format/Audio/Transcodable.php | 28 ++++++++++++ src/FFMpeg/Format/Dimension.php | 43 ++++++++++++++++++ src/FFMpeg/Format/Video.php | 8 +--- src/FFMpeg/Format/Video/DefaultVideo.php | 19 +++++--- .../{InteractiveVideo.php => Interactive.php} | 4 +- .../{ResamplableVideo.php => Resamplable.php} | 2 +- .../{ResizableVideo.php => Resizable.php} | 28 ++---------- src/FFMpeg/Format/Video/Transcodable.php | 29 ++++++++++++ 13 files changed, 162 insertions(+), 67 deletions(-) rename src/FFMpeg/Format/Audio/{InteractiveAudio.php => Interactive.php} (88%) rename src/FFMpeg/Format/Audio/{ResamplableAudio.php => Resamplable.php} (92%) create mode 100644 src/FFMpeg/Format/Audio/Transcodable.php create mode 100644 src/FFMpeg/Format/Dimension.php rename src/FFMpeg/Format/Video/{InteractiveVideo.php => Interactive.php} (88%) rename src/FFMpeg/Format/Video/{ResamplableVideo.php => Resamplable.php} (93%) rename src/FFMpeg/Format/Video/{ResizableVideo.php => Resizable.php} (55%) create mode 100644 src/FFMpeg/Format/Video/Transcodable.php diff --git a/src/FFMpeg/FFMpeg.php b/src/FFMpeg/FFMpeg.php index c640f6e..9b52efd 100644 --- a/src/FFMpeg/FFMpeg.php +++ b/src/FFMpeg/FFMpeg.php @@ -113,7 +113,7 @@ class FFMpeg extends Binary try { $process->run(); } catch (\RuntimeException $e) { - + } if ( ! $process->isSuccessful()) { @@ -176,11 +176,17 @@ class FFMpeg extends Binary . escapeshellarg($this->pathfile) . ' ' . $format->getExtraParams() . ' -threads ' . $threads - . ' -acodec ' . $format->getAudioCodec() . ' -ab ' . $format->getKiloBitrate() . 'k ' - . ' -ac 2 -ar ' . $format->getAudioSampleRate() . ' ' . escapeshellarg($outputPathfile); + if ($format instanceof Audio\Transcodable) { + $cmd .= ' -acodec ' . $format->getAudioCodec(); + } + + if ($format instanceof Audio\Resamplable) { + $cmd .= ' -ac 2 -ar ' . $format->getAudioSampleRate(); + } + $process = new Process($cmd); $this->logger->addInfo(sprintf('FFmpeg executes command %s', $cmd)); @@ -188,7 +194,7 @@ class FFMpeg extends Binary try { $process->run(); } catch (\RuntimeException $e) { - + } if ( ! $process->isSuccessful()) { @@ -226,8 +232,7 @@ class FFMpeg extends Binary $result = $this->prober->probeStreams($this->pathfile); - $originalWidth = $format->getWidth(); - $originalHeight = $format->getHeight(); + $originalWidth = $originalHeight = null; foreach ($result as $stream) { foreach ($stream as $info) { @@ -242,23 +247,35 @@ class FFMpeg extends Binary } } - list($width, $height) = $format->getComputedDimensions($originalWidth, $originalHeight); + if ($originalHeight !== null && $originalWidth !== null) { + list($width, $height) = $format->getComputedDimensions($originalWidth, $originalHeight); - $width = $this->getMultiple($format->getWidth(), 16); - $height = $this->getMultiple($format->getHeight(), 16); + $width = $this->getMultiple($width, 16); + $height = $this->getMultiple($height, 16); - $cmd_part2 .= ' -s ' . $width . 'x' . $height; + $cmd_part2 .= ' -s ' . $width . 'x' . $height; + } } - $cmd_part2 .= ' -r ' . $format->getFrameRate() - . ' -vcodec ' . $format->getVideoCodec() - . ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3' + if ($format instanceof Video\Resamplable) { + $cmd_part2 .= ' -r ' . $format->getFrameRate(); + } + + if ($format instanceof Video\Transcodable) { + $cmd_part2 .= ' -vcodec ' . $format->getVideoCodec(); + } + + $cmd_part2 .= ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3' . ' -threads ' . $threads . ' -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 ' . ' -sc_threshold 40 -flags +loop -cmp +chroma' . ' -me_range 16 -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 ' . ' -trellis 1 -qscale 1 ' - . '-acodec ' . $format->getAudioCodec() . ' -ab 92k '; + . ' -ab 92k '; + + if ($format instanceof Audio\Transcodable) { + $cmd_part2 .= '-acodec ' . $format->getAudioCodec(); + } $tmpFile = new \SplFileInfo(tempnam(sys_get_temp_dir(), 'temp') . '.' . pathinfo($outputPathfile, PATHINFO_EXTENSION)); diff --git a/src/FFMpeg/Format/Audio.php b/src/FFMpeg/Format/Audio.php index 02efc46..12161e1 100644 --- a/src/FFMpeg/Format/Audio.php +++ b/src/FFMpeg/Format/Audio.php @@ -19,18 +19,19 @@ namespace FFMpeg\Format; interface Audio { - /** - * Returns the audio codec - * - * @return string - */ - public function getAudioCodec(); - /** * Get the kiloBitrate value * * @return integer */ public function getKiloBitrate(); + + /** + * Give som extra parameters to add to ffmpeg commandline + * Parameters MUST be escaped + * + * @return string + */ + public function getExtraParams(); } diff --git a/src/FFMpeg/Format/Audio/DefaultAudio.php b/src/FFMpeg/Format/Audio/DefaultAudio.php index 7266531..0dfd57e 100644 --- a/src/FFMpeg/Format/Audio/DefaultAudio.php +++ b/src/FFMpeg/Format/Audio/DefaultAudio.php @@ -18,7 +18,7 @@ use FFMpeg\Exception\InvalidArgumentException; * * @author Romain Neutron imprec@gmail.com */ -abstract class DefaultAudio implements ResamplableAudio, InteractiveAudio +abstract class DefaultAudio implements Resamplable, Interactive { protected $audioCodec; protected $audioSampleRate = 44100; diff --git a/src/FFMpeg/Format/Audio/InteractiveAudio.php b/src/FFMpeg/Format/Audio/Interactive.php similarity index 88% rename from src/FFMpeg/Format/Audio/InteractiveAudio.php rename to src/FFMpeg/Format/Audio/Interactive.php index 49cef05..eb0254a 100644 --- a/src/FFMpeg/Format/Audio/InteractiveAudio.php +++ b/src/FFMpeg/Format/Audio/Interactive.php @@ -11,8 +11,6 @@ namespace FFMpeg\Format\Audio; -use FFMpeg\Format\Audio as BaseAudio; - /** * The interactive audio interface. This provide a method to list available * codecs. This is usefull to build interactive development and switch between @@ -20,7 +18,7 @@ use FFMpeg\Format\Audio as BaseAudio; * * @author Romain Neutron imprec@gmail.com */ -interface InteractiveAudio extends BaseAudio +interface Interactive extends Transcodable { /** diff --git a/src/FFMpeg/Format/Audio/ResamplableAudio.php b/src/FFMpeg/Format/Audio/Resamplable.php similarity index 92% rename from src/FFMpeg/Format/Audio/ResamplableAudio.php rename to src/FFMpeg/Format/Audio/Resamplable.php index 4e0e8dc..d208218 100644 --- a/src/FFMpeg/Format/Audio/ResamplableAudio.php +++ b/src/FFMpeg/Format/Audio/Resamplable.php @@ -20,7 +20,7 @@ use FFMpeg\Format\Audio as BaseAudio; * * @author Romain Neutron imprec@gmail.com */ -interface ResamplableAudio extends BaseAudio +interface Resamplable extends BaseAudio { /** diff --git a/src/FFMpeg/Format/Audio/Transcodable.php b/src/FFMpeg/Format/Audio/Transcodable.php new file mode 100644 index 0000000..c110b00 --- /dev/null +++ b/src/FFMpeg/Format/Audio/Transcodable.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FFMpeg\Format\Audio; + +use FFMpeg\Format\Audio as BaseAudio; + +/** + * @author Romain Neutron imprec@gmail.com + */ +interface Transcodable extends BaseAudio +{ + + /** + * Returns the audio codec + * + * @return string + */ + public function getAudioCodec(); +} diff --git a/src/FFMpeg/Format/Dimension.php b/src/FFMpeg/Format/Dimension.php new file mode 100644 index 0000000..1907b3a --- /dev/null +++ b/src/FFMpeg/Format/Dimension.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FFMpeg\Format; + +use FFMpeg\Exception\InvalidArgumentException; + +/** + * @author Romain Neutron imprec@gmail.com + */ +class Dimension +{ + protected $width; + protected $height; + + public function __construct($width, $height) + { + if ($width <= 0 || $height <= 0) { + throw InvalidArgumentException('Width and height should be positive integer'); + } + + $this->width = (int) $width; + $this->height = (int) $height; + } + + public function getWidth() + { + return $this->width; + } + + public function getHeight() + { + return $this->height; + } +} diff --git a/src/FFMpeg/Format/Video.php b/src/FFMpeg/Format/Video.php index 6f64f8b..99fa20e 100644 --- a/src/FFMpeg/Format/Video.php +++ b/src/FFMpeg/Format/Video.php @@ -18,11 +18,5 @@ namespace FFMpeg\Format; */ interface Video extends Audio { - - /** - * Returns the video codec - * - * @return string - */ - public function getVideoCodec(); + } diff --git a/src/FFMpeg/Format/Video/DefaultVideo.php b/src/FFMpeg/Format/Video/DefaultVideo.php index b28c74d..fcbb26f 100644 --- a/src/FFMpeg/Format/Video/DefaultVideo.php +++ b/src/FFMpeg/Format/Video/DefaultVideo.php @@ -12,6 +12,7 @@ namespace FFMpeg\Format\Video; use FFMpeg\Format\Audio\DefaultAudio; +use FFMpeg\Format\Dimension; use FFMpeg\Exception\InvalidArgumentException; /** @@ -19,7 +20,7 @@ use FFMpeg\Exception\InvalidArgumentException; * * @author Romain Neutron imprec@gmail.com */ -abstract class DefaultVideo extends DefaultAudio implements InteractiveVideo, ResamplableVideo, ResizableVideo +abstract class DefaultVideo extends DefaultAudio implements Interactive, Resamplable, Resizable { const RESIZEMODE_FIT = 'fit'; const RESIZEMODE_INSET = 'inset'; @@ -33,7 +34,10 @@ abstract class DefaultVideo extends DefaultAudio implements InteractiveVideo, Re protected $kiloBitrate = 1000; /** - * {@inheritdoc} + * Returns the width setting. + * The return of this method should not depend on a media file size + * + * @return integer */ public function getWidth() { @@ -41,7 +45,10 @@ abstract class DefaultVideo extends DefaultAudio implements InteractiveVideo, Re } /** - * {@inheritdoc} + * Returns the height setting + * The return of this method should not depend on a media file size + * + * @return integer */ public function getHeight() { @@ -95,7 +102,7 @@ abstract class DefaultVideo extends DefaultAudio implements InteractiveVideo, Re break; } - return array($width, $height); + return new Dimension($width, $height); } /** @@ -121,7 +128,9 @@ abstract class DefaultVideo extends DefaultAudio implements InteractiveVideo, Re } /** - * {@inheritdoc} + * Get the current resize mode name + * + * @return string */ public function getResizeMode() { diff --git a/src/FFMpeg/Format/Video/InteractiveVideo.php b/src/FFMpeg/Format/Video/Interactive.php similarity index 88% rename from src/FFMpeg/Format/Video/InteractiveVideo.php rename to src/FFMpeg/Format/Video/Interactive.php index 1a41e29..4641d69 100644 --- a/src/FFMpeg/Format/Video/InteractiveVideo.php +++ b/src/FFMpeg/Format/Video/Interactive.php @@ -11,8 +11,6 @@ namespace FFMpeg\Format\Video; -use FFMpeg\Format\Video as BaseVideo; - /** * The interactive video interface. This provide a method to list available * codecs. This is usefull to build interactive development and switch between @@ -20,7 +18,7 @@ use FFMpeg\Format\Video as BaseVideo; * * @author Romain Neutron imprec@gmail.com */ -interface InteractiveVideo extends BaseVideo +interface Interactive extends Transcodable { /** diff --git a/src/FFMpeg/Format/Video/ResamplableVideo.php b/src/FFMpeg/Format/Video/Resamplable.php similarity index 93% rename from src/FFMpeg/Format/Video/ResamplableVideo.php rename to src/FFMpeg/Format/Video/Resamplable.php index 172d03f..d27ff9c 100644 --- a/src/FFMpeg/Format/Video/ResamplableVideo.php +++ b/src/FFMpeg/Format/Video/Resamplable.php @@ -20,7 +20,7 @@ use FFMpeg\Format\Video as BaseVideo; * * @author Romain Neutron imprec@gmail.com */ -interface ResamplableVideo extends BaseVideo +interface Resamplable extends BaseVideo { /** diff --git a/src/FFMpeg/Format/Video/ResizableVideo.php b/src/FFMpeg/Format/Video/Resizable.php similarity index 55% rename from src/FFMpeg/Format/Video/ResizableVideo.php rename to src/FFMpeg/Format/Video/Resizable.php index 346fc2a..4391f7f 100644 --- a/src/FFMpeg/Format/Video/ResizableVideo.php +++ b/src/FFMpeg/Format/Video/Resizable.php @@ -12,6 +12,7 @@ namespace FFMpeg\Format\Video; use FFMpeg\Format\Video as BaseVideo; +use FFMpeg\Format\Dimension; /** * The resizable video interface @@ -20,39 +21,16 @@ use FFMpeg\Format\Video as BaseVideo; * * @author Romain Neutron imprec@gmail.com */ -interface ResizableVideo extends BaseVideo +interface Resizable extends BaseVideo { - /** - * Returns the width setting. - * The return of this method should not depend on a media file size - * - * @return integer - */ - public function getWidth(); - - /** - * Returns the height setting - * The return of this method should not depend on a media file size - * - * @return integer - */ - public function getHeight(); - /** * Returns the computed dimensions for the resize, after operation. * This method return the actual dimensions that FFmpeg will use. * * @param integer $originalWidth * @param integer $originalHeight - * @return array An indexed array containing the width and the height + * @return Dimension A dimension */ public function getComputedDimensions($originalWidth, $originalHeight); - - /** - * Get the current resize mode name - * - * @return string - */ - public function getResizeMode(); } diff --git a/src/FFMpeg/Format/Video/Transcodable.php b/src/FFMpeg/Format/Video/Transcodable.php new file mode 100644 index 0000000..60c01ee --- /dev/null +++ b/src/FFMpeg/Format/Video/Transcodable.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FFMpeg\Format\Video; + +use FFMpeg\Format\Video as BaseVideo; +use FFMpeg\Format\Dimension; + +/** + * @author Romain Neutron imprec@gmail.com + */ +interface Transcodable extends BaseVideo +{ + + /** + * Returns the video codec + * + * @return string + */ + public function getVideoCodec(); +}