VideoHlsPipeline.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Jobs\VideoPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use FFMpeg\Format\Video\X264;
  10. use FFMpeg;
  11. use Cache;
  12. use App\Services\MediaService;
  13. use App\Services\StatusService;
  14. use Illuminate\Queue\Middleware\WithoutOverlapping;
  15. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  16. class VideoHlsPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $media;
  20. public $timeout = 900;
  21. public $tries = 3;
  22. public $maxExceptions = 1;
  23. public $failOnTimeout = true;
  24. public $deleteWhenMissingModels = true;
  25. /**
  26. * The number of seconds after which the job's unique lock will be released.
  27. *
  28. * @var int
  29. */
  30. public $uniqueFor = 3600;
  31. /**
  32. * Get the unique ID for the job.
  33. */
  34. public function uniqueId(): string
  35. {
  36. return 'media:video-hls:id-' . $this->media->id;
  37. }
  38. /**
  39. * Get the middleware the job should pass through.
  40. *
  41. * @return array<int, object>
  42. */
  43. public function middleware(): array
  44. {
  45. return [(new WithoutOverlapping("media:video-hls:id-{$this->media->id}"))->shared()->dontRelease()];
  46. }
  47. /**
  48. * Create a new job instance.
  49. */
  50. public function __construct($media)
  51. {
  52. $this->media = $media;
  53. }
  54. /**
  55. * Execute the job.
  56. */
  57. public function handle(): void
  58. {
  59. $depCheck = Cache::rememberForever('video-pipeline:hls:depcheck', function() {
  60. $bin = config('laravel-ffmpeg.ffmpeg.binaries');
  61. $output = shell_exec($bin . ' -version');
  62. if($output && preg_match('/ffmpeg version ([^\s]+)/', $output, $matches)) {
  63. $version = $matches[1];
  64. return (version_compare($version, config('laravel-ffmpeg.min_hls_version')) >= 0) ? 'ok' : false;
  65. } else {
  66. return false;
  67. }
  68. });
  69. if(!$depCheck || $depCheck !== 'ok') {
  70. return;
  71. }
  72. $media = $this->media;
  73. $bitrate = (new X264)->setKiloBitrate(config('media.hls.bitrate') ?? 1000);
  74. $mp4 = $media->media_path;
  75. $man = str_replace('.mp4', '.m3u8', $mp4);
  76. FFMpeg::fromDisk('local')
  77. ->open($mp4)
  78. ->exportForHLS()
  79. ->setSegmentLength(16)
  80. ->setKeyFrameInterval(48)
  81. ->addFormat($bitrate)
  82. ->save($man);
  83. $media->hls_path = $man;
  84. $media->hls_transcoded_at = now();
  85. $media->save();
  86. MediaService::del($media->status_id);
  87. usleep(50000);
  88. StatusService::del($media->status_id);
  89. return;
  90. }
  91. }