1
0
Fork 0
pixelfed/app/Jobs/VideoPipeline/VideoThumbnail.php

88 lines
2.4 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;
class VideoThumbnail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media;
/**
* 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)
2019-06-02 00:09:57 +00:00
->getFrameFromSeconds(0)
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();
} catch (Exception $e) {
}
2019-12-25 02:34:16 +00:00
if(config('pixelfed.cloud_storage') == true) {
$path = storage_path('app/'.$media->media_path);
$thumb = storage_path('app/'.$media->thumbnail_path);
$p = explode('/', $media->media_path);
$monthHash = $p[2];
$userHash = $p[3];
$storagePath = "public/m/{$monthHash}/{$userHash}";
$file = Storage::disk(config('filesystems.cloud'))->putFile($storagePath, new File($path), 'public');
$url = Storage::disk(config('filesystems.cloud'))->url($file);
$thumbFile = Storage::disk(config('filesystems.cloud'))->putFile($storagePath, new File($thumb), 'public');
$thumbUrl = Storage::disk(config('filesystems.cloud'))->url($thumbFile);
$media->thumbnail_url = $thumbUrl;
$media->cdn_url = $url;
$media->optimized_url = $url;
$media->save();
}
if($media->status_id) {
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
}
2018-11-04 03:17:56 +00:00
}
}