From 104b16689ec5e088c28272f7d4064eff37c4aa4f Mon Sep 17 00:00:00 2001 From: Kaloyan98 Date: Sun, 15 Jul 2018 15:55:58 +0300 Subject: [PATCH] Add gif creator command --- core/Commands/CreateImagesCommand.php | 80 +++++++++++++++++++ .../Exceptions/NotGifFileException.php | 9 +++ core/Commands/Exceptions/VideoLengthError.php | 9 +++ core/Commands/GifCommand.php | 44 ++++++++++ core/Generators/GIFGenerator.php | 7 -- core/Util/Time.php | 18 +++-- core/helpers.php | 12 +++ 7 files changed, 167 insertions(+), 12 deletions(-) create mode 100644 core/Commands/CreateImagesCommand.php create mode 100644 core/Commands/Exceptions/NotGifFileException.php create mode 100644 core/Commands/Exceptions/VideoLengthError.php delete mode 100644 core/Generators/GIFGenerator.php diff --git a/core/Commands/CreateImagesCommand.php b/core/Commands/CreateImagesCommand.php new file mode 100644 index 0000000..fe0d0fd --- /dev/null +++ b/core/Commands/CreateImagesCommand.php @@ -0,0 +1,80 @@ +validation = false; + $this->imageFullPathName = formatImageNameFoFFMpeg($imageFullPathName); + $endPositionTime = Time::createFromString($endPosition); + + $startPositionTime = Time::createFromString($startPosition); + + if ($startPositionTime->getTimeInSeconds() > $videoDuration->getTimeInSeconds()) { + $startPosition = "00:00:00"; + $startPositionTime = Time::createFromString($startPosition); + } + + if ($endPositionTime->getTimeInSeconds() > $videoDuration->getTimeInSeconds()) { + throw new VideoLengthError($endPosition, $videoDuration->getFullTime()); + } + + // create an instance of inpsector + $videoInspector = new VideoInspector($videoFile); + + // check if file is supported + $videoInspector->checkFile(); + + $secondsCombinedFromStartAndEnd = $endPositionTime->getSeconds() - $startPositionTime->getSeconds(); + + $MAX_FRAMES = 25; + $this->framesCreated = $MAX_FRAMES * $secondsCombinedFromStartAndEnd; + + $this->args = [ + 'ffmpeg', + '-ss ' . $startPosition, + '-to ' . $endPosition, + '-i ' . $videoFile, + '-frames ' . $this->framesCreated, + $this->imageFullPathName, + '2>&1', + ]; + } + + public function getCommandArgs() { + return implode(' ', $this->args); + } + + public function checkOutput($output) { + if (!$output) { + $this->validation = false; + } + + $this->validation = true; + } + + public function framesCreated() { + return $this->framesCreated; + } +} \ No newline at end of file diff --git a/core/Commands/Exceptions/NotGifFileException.php b/core/Commands/Exceptions/NotGifFileException.php new file mode 100644 index 0000000..d436415 --- /dev/null +++ b/core/Commands/Exceptions/NotGifFileException.php @@ -0,0 +1,9 @@ +validation = false; + $imagesFirstName = formatImageNameFoFFMpeg($imagesFirstName); + + $this->args = [ + 'ffmpeg', + '-i ' . $imagesFirstName, + $outputGifFile, + '2>&1', + ]; + } + + public function getCommandArgs() { + return implode(' ', $this->args); + } + + public function checkOutput($output) { + if (!$output) { + $this->validation = false; + } + + $this->validation = true; + } +} \ No newline at end of file diff --git a/core/Generators/GIFGenerator.php b/core/Generators/GIFGenerator.php deleted file mode 100644 index 29b889b..0000000 --- a/core/Generators/GIFGenerator.php +++ /dev/null @@ -1,7 +0,0 @@ -timeFormat = $timeFormat; + protected function __construct(TimeFormatInterface $timeFormatObject = null, $timeFormat = '00:00:00') { + if ($timeFormatObject) { + $this->timeFormat = $timeFormatObject->getFormattedTime(); + } else { + $this->timeFormat = $timeFormat; + } $this->time = []; $this->initializeTime(); } @@ -24,8 +28,12 @@ protected function __construct(TimeFormatInterface $timeFormat) { * @param TimeFormatInterface $timeFormat * @return new Time */ - public static function create(TimeFormatInterface $timeFormat) { - return new static($timeFormat); + public static function create(TimeFormatInterface $timeFormatObject) { + return new static($timeFormatObject); + } + + public static function createFromString($timeFormat = '00:00:00') { + return new static(null, $timeFormat); } public function getSeconds() { @@ -50,7 +58,7 @@ public function getTimeInMinutes() { public function getFullTime() { - return $this->timeFormat->getFormattedTime(); + return $this->timeFormat; } protected function getNormalizedTime($to = 'seconds') { diff --git a/core/helpers.php b/core/helpers.php index 27adc8f..ec956b4 100644 --- a/core/helpers.php +++ b/core/helpers.php @@ -6,4 +6,16 @@ function ffmpegInitialized() { return Initializer::isFFMpegInitialized(); } +function formatImageNameFoFFMpeg($imageFileName = '') { + if (!$imageFileName) { + return $imageFileName; + } + + $imageBaseDir = pathinfo($imageFileName, PATHINFO_DIRNAME); + $imageName = pathinfo($imageFileName, PATHINFO_FILENAME); + $imageExtension = pathinfo($imageFileName, PATHINFO_EXTENSION); + + return $imageBaseDir . DIRECTORY_SEPARATOR . $imageName . '%d.' . $imageExtension; +} + ?> \ No newline at end of file