media->id; } /** * Get the middleware the job should pass through. * * @return array */ 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(); // 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. 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); } // Video is not size-optimized, so the raw size charged at upload already // reflects the on-disk footprint; no quota correction is needed here. MediaStoragePipeline::dispatch($media); } }