-
-
Notifications
You must be signed in to change notification settings - Fork 627
[6.x] Asset video thumbnail generation #11841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
duncanmcclean
merged 27 commits into
statamic:master
from
JohnathonKoster:asset-video-thumbnail-generation
Jul 25, 2025
Merged
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8d88734
Generate thumbnails for video files
JohnathonKoster 13dc58b
Merge branch 'ui' into asset-video-thumbnail-generation
JohnathonKoster 5a897c2
Rename to mentally separate from console output
JohnathonKoster c36cc33
Refactor to method on base class
JohnathonKoster f2fb5d3
Auto discover ffmpeg, refactor config
JohnathonKoster 18dc53a
Better handling to ensure fallback SVG icon
JohnathonKoster 5089110
Update Ffmpeg.php
JohnathonKoster 064bf1f
Revert this
JohnathonKoster fce2ac1
Merge branch 'ui' into asset-video-thumbnail-generation
JohnathonKoster 102e1aa
Update ImageGenerator.php
JohnathonKoster 6672563
Update FolderAsset.php
JohnathonKoster a90c7d2
Merge branch 'ui' into asset-video-thumbnail-generation
JohnathonKoster 7084330
Merge branch 'ui' into asset-video-thumbnail-generation
JohnathonKoster d076e71
Adjust ordering of merged data
JohnathonKoster ec559bc
Merge branch 'ui' into pr/11841
duncanmcclean 36dac14
Merge branch 'master' into pr/11841
duncanmcclean 953d732
Merge branch 'master' into pr/11841
duncanmcclean a3d81ab
Move private methods to the bottom
duncanmcclean 7db2abb
Use a match statement here instead
duncanmcclean 32b7e4d
A couple of nitpicky changes
duncanmcclean c98e923
Move `video_thumbnails` config next to the other thumbnail-related op…
duncanmcclean ebb8e9a
return types
duncanmcclean 463d61d
Laravel Herd doesn't inherit the user's PATH, so we need to check the…
duncanmcclean 1d5585e
Convert `if` statements to a `match` statement
duncanmcclean 9b78d62
Formatting
duncanmcclean f4883fc
Merge branch 'master' into pr/11841
duncanmcclean cfd3c8a
Code cleanup 🧹
JohnathonKoster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Console\Processes; | ||
|
|
||
| use Statamic\View\Antlers\Language\Utilities\StringUtilities; | ||
|
|
||
| class Ffmpeg extends Process | ||
| { | ||
| protected string $startTimestamp = '00:00:00'; | ||
|
|
||
| public function startTimestamp(string $startTimestamp): static | ||
| { | ||
| $this->startTimestamp = $startTimestamp; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function extractThumbnail(string $path, string $outputFilePath) | ||
| { | ||
| $ffmpegBinary = $this->ffmpegBinary(); | ||
|
|
||
| if (! $ffmpegBinary) { | ||
| return null; | ||
| } | ||
|
|
||
| $output = $this->run($this->buildCommand($ffmpegBinary, $path, $outputFilePath)); | ||
|
|
||
| if (! file_exists($outputFilePath)) { | ||
| return null; | ||
| } | ||
|
|
||
| return $outputFilePath; | ||
| } | ||
|
|
||
| private function buildCommand(string $ffmpegBinary, string $path, string $output) | ||
| { | ||
| return collect([ | ||
| escapeshellarg($ffmpegBinary), | ||
| '-y', | ||
| '-ss', | ||
| escapeshellarg($this->startTimestamp), | ||
| '-i', | ||
| escapeshellarg($path), | ||
| '-vframes 1', | ||
| escapeshellarg($output), | ||
| ]) | ||
| ->join(' '); | ||
| } | ||
|
|
||
| public function ffmpegBinary() | ||
| { | ||
| if ($binary = config('statamic.assets.ffmpeg.binary')) { | ||
| return $binary; | ||
| } | ||
|
|
||
| $output = $this->run($this->isWindows() ? 'where ffmpeg' : 'which ffmpeg'); | ||
|
|
||
| if (str($output)->lower()->contains([ | ||
| 'could not find files for the given', | ||
| ])) { | ||
| return null; | ||
| } | ||
|
|
||
| return str(StringUtilities::normalizeLineEndings(trim($output))) | ||
| ->explode("\n") | ||
| ->first(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Imaging; | ||
|
|
||
| use Statamic\Console\Processes\Ffmpeg; | ||
| use Statamic\Contracts\Assets\Asset; | ||
| use Statamic\Facades\Path; | ||
|
|
||
| class ThumbnailExtractor | ||
| { | ||
| private Ffmpeg $ffmpeg; | ||
|
|
||
| public function __construct(Ffmpeg $ffmpeg) | ||
| { | ||
| $this->ffmpeg = $ffmpeg; | ||
| } | ||
|
|
||
| public static function enabled() | ||
| { | ||
| return config( | ||
| 'statamic.assets.video_thumbnails', | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| public static function cachePath() | ||
| { | ||
| return config( | ||
| 'statamic.assets.ffmpeg.cache_path', | ||
| storage_path('statamic/glide/ffmpeg') | ||
| ); | ||
| } | ||
|
|
||
| public static function getCachePath(Asset $asset) | ||
| { | ||
| $fileName = 'thumb_'.md5($asset->id()).'.jpg'; | ||
| $cacheDirectory = static::cachePath(); | ||
| $finalPath = Path::tidy($cacheDirectory.'/'.$fileName); | ||
|
|
||
| if (! file_exists($cacheDirectory)) { | ||
| mkdir($cacheDirectory, 0755, true); | ||
| } | ||
|
|
||
| return $finalPath; | ||
| } | ||
|
|
||
| public function generateThumbnail(Asset $asset) | ||
| { | ||
| $cachePath = static::getCachePath($asset); | ||
|
|
||
| if (file_exists($cachePath)) { | ||
| return $cachePath; | ||
| } | ||
|
|
||
| $ffmpegInput = null; | ||
|
|
||
| if (file_exists($asset->resolvedPath())) { | ||
| $ffmpegInput = $asset->resolvedPath(); | ||
| } elseif ($asset->container()->accessible()) { | ||
| $ffmpegInput = $asset->absoluteUrl(); | ||
| } else { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->ffmpeg->extractThumbnail( | ||
| $asset->absoluteUrl(), | ||
|
JohnathonKoster marked this conversation as resolved.
Outdated
|
||
| static::getCachePath($asset) | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.