pixelfed/app/Jobs/VideoPipeline/VideoThumbnail.php

123 lines
3.2 KiB
PHP
Raw Normal View History

2018-11-04 03:17:56 +00:00
<?php
namespace App\Jobs\VideoPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2019-12-25 02:34:16 +00:00
use Illuminate\Http\File;
use Cache;
2018-11-04 03:17:56 +00:00
use FFMpeg;
2019-12-25 02:34:16 +00:00
use Storage;
2018-11-04 03:17:56 +00:00
use App\Media;
use App\Jobs\MediaPipeline\MediaStoragePipeline;
use App\Util\Media\Blurhash;
use App\Services\MediaService;
use App\Services\StatusService;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
2018-11-04 03:17:56 +00:00
class VideoThumbnail implements ShouldQueue, ShouldBeUniqueUntilProcessing
2018-11-04 03:17:56 +00:00
{
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()];
}
2018-11-04 03:17:56 +00:00
/**
* 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;
2019-05-27 00:36:04 +00:00
if($media->mime != 'video/mp4') {
return;
}
2018-11-04 03:17:56 +00:00
$base = $media->media_path;
$path = explode('/', $base);
$name = last($path);
try {
$t = explode('.', $name);
2019-06-09 18:53:15 +00:00
$t = $t[0].'_thumb.jpeg';
2018-11-04 03:17:56 +00:00
$i = count($path) - 1;
$path[$i] = $t;
$save = implode('/', $path);
2019-05-27 00:36:04 +00:00
$video = FFMpeg::open($base)
->getFrameFromSeconds(1)
2019-05-27 00:36:04 +00:00
->export()
->toDisk('local')
->save($save);
2018-11-04 03:17:56 +00:00
$media->thumbnail_path = $save;
$media->save();
$blurhash = Blurhash::generate($media);
if($blurhash) {
$media->blurhash = $blurhash;
$media->save();
}
if(config('media.hls.enabled')) {
VideoHlsPipeline::dispatch($media)->onQueue('mmo');
}
2018-11-04 03:17:56 +00:00
} catch (Exception $e) {
}
2019-12-25 02:34:16 +00:00
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);
2019-12-25 02:34:16 +00:00
}
MediaStoragePipeline::dispatch($media);
2018-11-04 03:17:56 +00:00
}
}