Skip to content

Commit

Permalink
Add gif creator command
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyanYosifov committed Jul 15, 2018
1 parent c570291 commit 104b166
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 12 deletions.
80 changes: 80 additions & 0 deletions core/Commands/CreateImagesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace FFMpegLib\Commands;

use FFMpegLib\Interfaces\CommandInterface;
use FFMpegLib\Traits\Validation;
use FFMpegLib\Inspectors\VideoInspector;
use FFMpegLib\Util\Time;
use FFMpegLib\Commands\Exceptions\VideoLengthError;

class CreateImagesCommand implements CommandInterface {
use Validation;

protected $args;
protected $validation;
protected $imageFullPathName;
protected $framesCreated;

public function __construct
(
$videoFile = null,
$imageFullPathName = '',
$startPosition = '00:00:00',
$endPosition = '00:00:03',
Time $videoDuration
) {
ffmpegInitialized();
$this->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;
}
}
9 changes: 9 additions & 0 deletions core/Commands/Exceptions/NotGifFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace FFMpegLib\Commands\Exceptions;

class NotGifFileException extends \Exception {
public function __construct($file = '') {
parent::__construct('Please enter a file with gif extension not ' . $file);
}
}
9 changes: 9 additions & 0 deletions core/Commands/Exceptions/VideoLengthError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace FFMpegLib\Commands\Exceptions;

class VideoLengthError extends \Exception {
public function __construct($passedTime = '', $videoTime = '') {
parent::__construct('Passed length ' . $passedTime . ' is longer than video\'s ' . $videoTime);
}
}
44 changes: 44 additions & 0 deletions core/Commands/GifCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace FFMpegLib\Commands;

use FFMpegLib\Interfaces\CommandInterface;
use FFMpegLib\Traits\Validation;
use FFMpegLib\Commands\Exceptions\NotGifFileException;

class GifCommand implements CommandInterface {
use Validation;

protected $args;
protected $validation;

public function __construct($imagesFirstName = 'image%d.png', $outputGifFile = 'video.gif') {
ffmpegInitialized();

if (!preg_match('~\.gif$~', $outputGifFile)) {
throw new NotGifFileException($outputGifFile);
}

$this->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;
}
}
7 changes: 0 additions & 7 deletions core/Generators/GIFGenerator.php

This file was deleted.

18 changes: 13 additions & 5 deletions core/Util/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class Time {
* Supported time formats currently are 00:00:00
* @param TimeFormatInterface $timeFormat
*/
protected function __construct(TimeFormatInterface $timeFormat) {
$this->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();
}
Expand All @@ -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() {
Expand All @@ -50,7 +58,7 @@ public function getTimeInMinutes() {


public function getFullTime() {
return $this->timeFormat->getFormattedTime();
return $this->timeFormat;
}

protected function getNormalizedTime($to = 'seconds') {
Expand Down
12 changes: 12 additions & 0 deletions core/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

?>

0 comments on commit 104b166

Please sign in to comment.