|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs\VideoPipeline;
|
|
|
|
|
|
|
|
|
|
use App\Jobs\MediaPipeline\MediaStoragePipeline;
|
|
|
|
|
use App\Models\Media;
|
|
|
|
|
use App\Services\MediaService;
|
|
|
|
|
use App\Services\StatusService;
|
|
|
|
|
use App\Util\Media\Blurhash;
|
|
|
|
|
use FFMpeg;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class VideoThumbnail implements ShouldBeUniqueUntilProcessing, ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
|
|
protected $media;
|
|
|
|
|
|
|
|
|
|
public $timeout = 900;
|
|
|
|
|
|
|
|
|
|
public $tries = 3;
|
|
|
|
|
|
|
|
|
|
public $maxExceptions = 1;
|
|
|
|
|
|
|
|
|
|
public $failOnTimeout = true;
|
|
|
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The number of seconds after which the job's unique lock will be released.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
public $uniqueFor = 3600;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the unique ID for the job.
|
|
|
|
|
*/
|
|
|
|
|
public function uniqueId(): string
|
|
|
|
|
{
|
|
|
|
|
return 'media:video-thumb:id-'.$this->media->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the middleware the job should pass through.
|
|
|
|
|
*
|
|
|
|
|
* @return array<int, object>
|
|
|
|
|
*/
|
|
|
|
|
public function middleware(): array
|
|
|
|
|
{
|
|
|
|
|
return [(new WithoutOverlapping("media:video-thumb:id-{$this->media->id}"))->shared()->dontRelease()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(Media $media)
|
|
|
|
|
{
|
|
|
|
|
$this->media = $media;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
$media = $this->media;
|
|
|
|
|
if ($media->mime != 'video/mp4') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$base = $media->media_path;
|
|
|
|
|
$path = explode('/', $base);
|
|
|
|
|
$name = last($path);
|
|
|
|
|
try {
|
|
|
|
|
$t = explode('.', $name);
|
|
|
|
|
$t = $t[0].'_thumb.jpeg';
|
|
|
|
|
$i = count($path) - 1;
|
|
|
|
|
$path[$i] = $t;
|
|
|
|
|
$save = implode('/', $path);
|
|
|
|
|
$video = FFMpeg::open($base)
|
|
|
|
|
->getFrameFromSeconds(1)
|
|
|
|
|
->export()
|
|
|
|
|
->toDisk('local')
|
|
|
|
|
->save($save);
|
|
|
|
|
|
|
|
|
|
$media->thumbnail_path = $save;
|
|
|
|
|
$media->save();
|
|
|
|
|
|
Fix videos never reaching cloud storage by downscaling in Blurhash
Blurhash::generate() allocates one PHP array per pixel of the source. At
roughly 255 bytes per pixel (measured: 224 MB peak for a 720x1280 frame) a
1920x1080 frame approaches half a gigabyte.
Image thumbnails survive this because they are capped at 640x640 in
Image::__construct() *and* run under that constructor's
ini_set('memory_limit', '1024M'). Video thumbnails get neither: FFmpeg saves
them at the source video's resolution, and VideoThumbnail never raises the
limit. So a video whose frame is 1080p or larger exhausts memory_limit.
That is a PHP fatal, not an \Exception, which has three consequences:
- the catch block in VideoThumbnail::handle() does not catch it
- the job never lands in failed_jobs, so nothing reports a problem
- MediaStoragePipeline::dispatch() on the last line of handle() never runs
The video therefore stays on local disk permanently while images beside it
replicate normally. Reported in #2652 (2021-02-13) and diagnosed correctly in
that thread on 2021-11-04.
Two changes:
1. Blurhash::generate() downscales to 128px on the long edge before sampling.
The result is a 4x4-component DCT, so full-resolution sampling adds
essentially nothing: measured against the full-resolution hash, mean
per-channel deviation of the decoded 24x24 preview is ~7.5/255 at a 32px
sample, ~4.5/255 at 64px, ~2.5/255 at 128px, and no better at 256px. Peak
memory for the frame above drops from 224 MB to 6 MB.
This removes the ceiling for every caller rather than moving it, which is
all that raising memory_limit would have done. Existing stored hashes are
not recomputed, so nothing already published changes appearance.
2. VideoThumbnail wraps the blurhash in its own try/catch, so a decorative
step can no longer skip the replication dispatch. Change 1 covers the
fatal; this covers any ordinary exception.
Verified on a live instance with S3 cloud storage: a 1920x1080 video that
previously stranded now generates a blurhash, uploads original and thumbnail
to the bucket, sets cdn_url/thumbnail_url/replicated_at, and removes the local
copies. Existing images re-hash to visually identical previews.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2 months ago
|
|
|
// Isolated from the thumbnail work on purpose. The blurhash is decorative;
|
|
|
|
|
// MediaStoragePipeline at the end of this method is not, and until now any
|
|
|
|
|
// failure here skipped it, leaving the video on local disk forever with no
|
|
|
|
|
// failed_jobs row to show for it (pixelfed#2652).
|
|
|
|
|
try {
|
|
|
|
|
$blurhash = Blurhash::generate($media);
|
|
|
|
|
if ($blurhash) {
|
|
|
|
|
$media->blurhash = $blurhash;
|
|
|
|
|
$media->save();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// \Throwable, not \Exception: the failure this guard exists for is a
|
|
|
|
|
// memory_limit exhaustion, which surfaces as \Error (or a fatal), not
|
|
|
|
|
// \Exception. Catching only \Exception here would let the very case
|
|
|
|
|
// that strands the video (pixelfed#2652) slip through and skip the
|
|
|
|
|
// MediaStoragePipeline dispatch below.
|
Fix videos never reaching cloud storage by downscaling in Blurhash
Blurhash::generate() allocates one PHP array per pixel of the source. At
roughly 255 bytes per pixel (measured: 224 MB peak for a 720x1280 frame) a
1920x1080 frame approaches half a gigabyte.
Image thumbnails survive this because they are capped at 640x640 in
Image::__construct() *and* run under that constructor's
ini_set('memory_limit', '1024M'). Video thumbnails get neither: FFmpeg saves
them at the source video's resolution, and VideoThumbnail never raises the
limit. So a video whose frame is 1080p or larger exhausts memory_limit.
That is a PHP fatal, not an \Exception, which has three consequences:
- the catch block in VideoThumbnail::handle() does not catch it
- the job never lands in failed_jobs, so nothing reports a problem
- MediaStoragePipeline::dispatch() on the last line of handle() never runs
The video therefore stays on local disk permanently while images beside it
replicate normally. Reported in #2652 (2021-02-13) and diagnosed correctly in
that thread on 2021-11-04.
Two changes:
1. Blurhash::generate() downscales to 128px on the long edge before sampling.
The result is a 4x4-component DCT, so full-resolution sampling adds
essentially nothing: measured against the full-resolution hash, mean
per-channel deviation of the decoded 24x24 preview is ~7.5/255 at a 32px
sample, ~4.5/255 at 64px, ~2.5/255 at 128px, and no better at 256px. Peak
memory for the frame above drops from 224 MB to 6 MB.
This removes the ceiling for every caller rather than moving it, which is
all that raising memory_limit would have done. Existing stored hashes are
not recomputed, so nothing already published changes appearance.
2. VideoThumbnail wraps the blurhash in its own try/catch, so a decorative
step can no longer skip the replication dispatch. Change 1 covers the
fatal; this covers any ordinary exception.
Verified on a live instance with S3 cloud storage: a 1920x1080 video that
previously stranded now generates a blurhash, uploads original and thumbnail
to the bucket, sets cdn_url/thumbnail_url/replicated_at, and removes the local
copies. Existing images re-hash to visually identical previews.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2 months ago
|
|
|
if (config('app.dev_log')) {
|
|
|
|
|
Log::error('Video blurhash generation failed: '.$e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config('media.hls.enabled')) {
|
|
|
|
|
VideoHlsPipeline::dispatch($media)->onQueue('mmo');
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
if (config('app.dev_log')) {
|
|
|
|
|
Log::error('Video thumbnail generation failed: '.$e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($media->status_id) {
|
|
|
|
|
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
|
|
|
|
|
MediaService::del($media->status_id);
|
|
|
|
|
Cache::forget('status:thumb:nsfw0'.$media->status_id);
|
|
|
|
|
Cache::forget('status:thumb:nsfw1'.$media->status_id);
|
|
|
|
|
Cache::forget('pf:services:sh:id:'.$media->status_id);
|
|
|
|
|
StatusService::del($media->status_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MediaStoragePipeline::dispatch($media);
|
|
|
|
|
}
|
|
|
|
|
}
|