1
0

VideoThumbnail.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. class VideoThumbnail implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $media;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct(Media $media)
  23. {
  24. $this->media = $media;
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. $media = $this->media;
  34. if($media->mime != 'video/mp4') {
  35. return;
  36. }
  37. $base = $media->media_path;
  38. $path = explode('/', $base);
  39. $name = last($path);
  40. try {
  41. $t = explode('.', $name);
  42. $t = $t[0].'_thumb.jpeg';
  43. $i = count($path) - 1;
  44. $path[$i] = $t;
  45. $save = implode('/', $path);
  46. $video = FFMpeg::open($base)
  47. ->getFrameFromSeconds(0)
  48. ->export()
  49. ->toDisk('local')
  50. ->save($save);
  51. $media->thumbnail_path = $save;
  52. $media->save();
  53. } catch (Exception $e) {
  54. }
  55. if($media->status_id) {
  56. Cache::forget('status:transformer:media:attachments:' . $media->status_id);
  57. }
  58. }
  59. }