generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new Timeline class * new Keyframe class * generateTimeline script * generateSnapshots > generateEasings * move generated easing snapshots * make directory for generated timeline snapshots * adds dummy test for Timeline class * updates readme
- Loading branch information
1 parent
733a8ce
commit 306208e
Showing
10 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
use ProjektGopher\FFMpegTween\Timeline; | ||
use ProjektGopher\FFMpegTween\Keyframe; | ||
use ProjektGopher\FFMpegTween\Enums\Ease; | ||
use ProjektGopher\FFMpegTween\Timing; | ||
|
||
echo 'Generating video sample using Timeline...'.PHP_EOL; | ||
|
||
$timeline = new Timeline(); | ||
$timeline->keyframe((new Keyframe()) | ||
->value('-th') | ||
->hold(Timing::seconds(1)) | ||
); | ||
$timeline->keyframe((new Keyframe()) | ||
->value('(main_h/2)-(th/2)') | ||
->ease(Ease::OutBounce) | ||
->duration(Timing::seconds(2)) | ||
->hold(Timing::seconds(1)) | ||
); | ||
$timeline->keyframe((new Keyframe()) | ||
->value('main_h') | ||
->ease(Ease::InElastic) | ||
->duration(Timing::seconds(2)) | ||
); | ||
|
||
$input = "-f lavfi -i \"color=c=black:s=256x256:d=1\""; | ||
$filter = "-filter_complex \"[0:v] loop=-1:1 [bg]; [bg] drawtext=text='Timeline':fontcolor=white:x=(main_w/2)-(tw/2):y={$timeline}\""; | ||
$codecs = '-codec:a copy -codec:v libx264 -crf 25 -pix_fmt yuv420p'; | ||
$duration = '-t 8'; // in seconds | ||
$out = "tests/Snapshots/Timelines/drawtext_y_enter-OutBounce_exit-InElastic.mp4"; | ||
$redirect = '2>&1'; // redirect stderr to stdout | ||
|
||
$cmd = "ffmpeg -y {$input} {$filter} {$codecs} {$duration} {$out} {$redirect}"; | ||
|
||
// TEMPORARY | ||
dump($timeline); | ||
echo $cmd; | ||
// die(); | ||
|
||
(array) $output = []; | ||
(int) $code = 0; | ||
exec($cmd, $output, $code); | ||
|
||
if ($code !== 0) { | ||
echo PHP_EOL; | ||
echo "Failed to generate snapshot for Timeline class.".PHP_EOL; | ||
echo "Command: {$cmd}".PHP_EOL; | ||
echo "Output: ".PHP_EOL; | ||
echo implode(PHP_EOL, $output).PHP_EOL; | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace ProjektGopher\FFMpegTween; | ||
|
||
use ProjektGopher\FFMpegTween\Enums\Ease; | ||
|
||
class Keyframe | ||
{ | ||
public string $value; | ||
|
||
public ?Ease $ease = null; | ||
|
||
public ?Timing $duration = null; | ||
|
||
public ?Timing $hold = null; | ||
|
||
public function value(string $value): self | ||
{ | ||
$this->value = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function ease(Ease $ease): self | ||
{ | ||
$this->ease = $ease; | ||
|
||
return $this; | ||
} | ||
|
||
public function duration(Timing $duration): self | ||
{ | ||
$this->duration = $duration; | ||
|
||
return $this; | ||
} | ||
|
||
public function hold(Timing $hold): self | ||
{ | ||
$this->hold = $hold; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace ProjektGopher\FFMpegTween; | ||
|
||
class Timeline | ||
{ | ||
private array $keyframes = []; | ||
|
||
private array $tweens = []; | ||
|
||
public function keyframe(Keyframe $keyframe): self | ||
{ | ||
/** | ||
* The first keyframe should never have an ease, or duration. | ||
*/ | ||
if (count($this->keyframes) === 0) { | ||
if ($keyframe->ease !== null) { | ||
throw new \Exception('The first keyframe should never have an ease.'); | ||
} | ||
if ($keyframe->duration !== null) { | ||
throw new \Exception('The first keyframe should never have a duration.'); | ||
} | ||
} | ||
|
||
$this->keyframes[] = $keyframe; | ||
|
||
return $this; | ||
} | ||
|
||
public function buildTweens(): void | ||
{ | ||
(int) $current_time = 0; | ||
|
||
foreach ($this->keyframes as $index => $keyframe) { | ||
if (! $keyframe instanceof Keyframe) { | ||
throw new \Exception('Keyframe is not of type Keyframe.'); | ||
} | ||
|
||
// Skip the first keyframe, as the values will be baked into the next tween. | ||
if ($index !== 0) { | ||
$this->tweens[] = (new Tween()) | ||
->from($this->getKeyframeByIndex($index - 1)->value) | ||
->to($keyframe->value) | ||
->delay(Timing::seconds($current_time)) | ||
->duration($keyframe->duration) | ||
->ease($keyframe->ease); | ||
} | ||
|
||
$current_time += $keyframe->hold?->seconds; | ||
$current_time += $keyframe->duration?->seconds; | ||
} | ||
} | ||
|
||
public function getKeyframeByIndex(int $index): Keyframe | ||
{ | ||
return $this->keyframes[$index]; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if (count($this->tweens) === 0) { | ||
$this->buildTweens(); | ||
} | ||
|
||
// clone the array so we don't modify the original | ||
$tweens = $this->tweens; | ||
|
||
// Initialize the timeline with the first tween. | ||
$timeline = array_shift($tweens); | ||
while ($tween = array_shift($tweens)) { | ||
// If the current time is greater than this tween's delay, | ||
// use the tween. Otherwise, use the previous timeline. | ||
$timeline = "if(gt(t\,{$tween->getDelay()})\,{$tween}\,{$timeline})"; | ||
} | ||
|
||
return $timeline; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
test('happy path', function () { | ||
expect(true)->toBeTrue(); | ||
}); |