protonemedia / laravel-ffmpeg

This package provides an integration with FFmpeg for Laravel. Laravel's Filesystem handles the storage of the files.
https://protone.media/en/blog/how-to-use-ffmpeg-in-your-laravel-projects
MIT License
1.66k stars 194 forks source link

How to get REALTIME HLS encoding "onProgress value" from my Laravel Job to my index.blade.php in Laravel 8? #385

Closed ChangChiLung closed 2 years ago

ChangChiLung commented 2 years ago

Hi, Pascal..

I'm struggling for weeks to find out how to get onProgress value and then show it in my index.blade.php page. When i echo $percentage and run the Job with Artisan Command i can see the value updated in my Command Prompt, but, how to grab it and show it to my index page? I'm searching for any tutorials out there, but found nothing usefull enough.

I use Vue.js (should i use JQuery AJAX?) & Laravel 8 Job Batch, here is my Job's code: ` **class ConvertVideo1440p implements ShouldQueue { use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $video;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct(Video $video)
{
    $this->video = $video;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    set_time_limit(0);
   // 1440p Video >>> 2560×1440 (2K Video)
    $bitrate_1440p  = (new X264('aac'))->setKiloBitrate(10400);

    $marked_video = FFMpeg::fromDisk($this->video->Tmp_Disk)
        ->open($this->video->Tmp_Path)
        ->addWatermark(function (WatermarkFactory $watermark) {
            $watermark->fromDisk('public')
                ->open('imgs/Resized/Logos/My_Logo_50Black_White_1A.png')
                ->right(25)
                ->top(25);
        })
        ->export()
        ->inFormat($decoratedFormat)
        ->toDisk('tmp')
        ->save('watermarked/' . $this->video->New_Name . '.mp4');

    if ($marked_video == true) {
        FFMpeg::fromDisk('tmp')
            ->open('watermarked/' . $this->video->New_Name . '.mp4')
            ->exportForHLS()
            ->onProgress(function ($percentage, $remaining, $rate) {
                session()->put('ProgressPercentage', $percentage);
            })
            ->addFormat($bitrate_1440p)
            ->toDisk('public')
            ->save('videos/stream_videos/' . $this->video->New_Name . '.m3u8');

        // update the DataBase so we know the convertion is done!
        $update_stream_video_data = $this->video->update([
            'Stream_Extension' => 'm3u8',
            'Streaming_Disk' => 'public',
            'Streaming_Path' => 'app/public/videos/stream_videos/' . $this->video->New_Name . '.m3u8',
            'Streaming_URL' => '/storage/videos/stream_videos/' . $this->video->New_Name . '.m3u8',
            'Converted_For_Streaming_at' => Carbon::now()
        ]);

        if ($update_stream_video_data == true) {
            return 'SUCCESS';
        }
    }
}

}** ` Can you PLEASE make some tutorial about it in YouTube just like before?

I'm just a Crazy NewBie who dreams to be the next Toni Sparkz and i'm really stucked up right now.. Please help me, Pascal.. Thanks before...

hamedmax73 commented 2 years ago

simply you can save it in your database or every thing you need. just pass it into an property sample:

   ->onProgress(function ($percentage, $remaining, $rate) {
                var_dump($percentage);
//this method used to prevent too many query (10 percent steps)
                if ($percentage - $this->start_progress >= 10 || $percentage > 90) {
                    $this->start_progress = $percentage;
                    $this->queueProgress($percentage);
                    $this->video->update([
                        'progress' => [
                            'percentage' => $percentage,
                            'remaining' => $remaining,
                            'rate' => $rate,
                        ],
                    ]);
                }
            })