VideoThumbnail.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Jobs\VideoPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Http\File;
  9. use Cache;
  10. use FFMpeg;
  11. use Storage;
  12. use App\Media;
  13. use App\Jobs\MediaPipeline\MediaStoragePipeline;
  14. use App\Util\Media\Blurhash;
  15. use App\Services\MediaService;
  16. use App\Services\StatusService;
  17. use Illuminate\Queue\Middleware\WithoutOverlapping;
  18. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  19. class VideoThumbnail implements ShouldQueue, ShouldBeUniqueUntilProcessing
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $media;
  23. public $timeout = 900;
  24. public $tries = 3;
  25. public $maxExceptions = 1;
  26. public $failOnTimeout = true;
  27. public $deleteWhenMissingModels = true;
  28. /**
  29. * The number of seconds after which the job's unique lock will be released.
  30. *
  31. * @var int
  32. */
  33. public $uniqueFor = 3600;
  34. /**
  35. * Get the unique ID for the job.
  36. */
  37. public function uniqueId(): string
  38. {
  39. return 'media:video-thumb:id-' . $this->media->id;
  40. }
  41. /**
  42. * Get the middleware the job should pass through.
  43. *
  44. * @return array<int, object>
  45. */
  46. public function middleware(): array
  47. {
  48. return [(new WithoutOverlapping("media:video-thumb:id-{$this->media->id}"))->shared()->dontRelease()];
  49. }
  50. /**
  51. * Create a new job instance.
  52. *
  53. * @return void
  54. */
  55. public function __construct(Media $media)
  56. {
  57. $this->media = $media;
  58. }
  59. /**
  60. * Execute the job.
  61. *
  62. * @return void
  63. */
  64. public function handle()
  65. {
  66. $media = $this->media;
  67. if($media->mime != 'video/mp4') {
  68. return;
  69. }
  70. $base = $media->media_path;
  71. $path = explode('/', $base);
  72. $name = last($path);
  73. try {
  74. $t = explode('.', $name);
  75. $t = $t[0].'_thumb.jpeg';
  76. $i = count($path) - 1;
  77. $path[$i] = $t;
  78. $save = implode('/', $path);
  79. $video = FFMpeg::open($base)
  80. ->getFrameFromSeconds(1)
  81. ->export()
  82. ->toDisk('local')
  83. ->save($save);
  84. $media->thumbnail_path = $save;
  85. $media->save();
  86. $blurhash = Blurhash::generate($media);
  87. if($blurhash) {
  88. $media->blurhash = $blurhash;
  89. $media->save();
  90. }
  91. if(config('media.hls.enabled')) {
  92. VideoHlsPipeline::dispatch($media)->onQueue('mmo');
  93. }
  94. } catch (Exception $e) {
  95. }
  96. if($media->status_id) {
  97. Cache::forget('status:transformer:media:attachments:' . $media->status_id);
  98. MediaService::del($media->status_id);
  99. Cache::forget('status:thumb:nsfw0' . $media->status_id);
  100. Cache::forget('status:thumb:nsfw1' . $media->status_id);
  101. Cache::forget('pf:services:sh:id:' . $media->status_id);
  102. StatusService::del($media->status_id);
  103. }
  104. MediaStoragePipeline::dispatch($media);
  105. }
  106. }