VideoThumbnailToCloudPipeline.php 4.1 KB

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