简体   繁体   English

从 Laravel 存储播放 m3u8 视频

[英]play m3u8 video from laravel storage

My question is the same as how to play m3u8 videos from laravel storage but this one did not get answers.我的问题与如何从 laravel 存储播放 m3u8 视频相同,但这个问题没有得到答案。

If I play the video from the public folder it does it without problems.如果我播放公共文件夹中的视频,它会毫无问题地播放。

but if I want to play it from storage this doesn't work.但如果我想从存储中播放它,这是行不通的。

    public function watch(Request $request, Episode $episode)
{

    $video = Storage::disk('videos')->get($episode->video);

    return new Response($video, 200, ['Content-Type' => 'application/x-mpegURL', 'isHls' => true]);
}

this is the definition of my disk in config/filesystems.php这是我的磁盘在 config/filesystems.php 中的定义

  'videos' => [
        'driver' => 'local',
        'root' => storage_path('app/videos'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

this is my conversion code (job)这是我的转换代码(工作)

     */
public function handle()
{
    $path = $this->episode->id . '.m3u8';
    $lowBitrate  = (new X264 ('aac'))->setKiloBitrate(500)->setVideoCodec('libx264');
    $midBitrate  = (new X264 ('aac'))->setKiloBitrate(1000)->setVideoCodec('libx264');
    $highBitrate = (new X264 ('aac'))->setKiloBitrate(3000)->setVideoCodec('libx264');

    FFMpeg::fromDisk('tmp')->open($this->episode->video)
        ->exportForHLS()
        ->dontSortFormats()
        ->setSegmentLength(10)
        ->toDisk('local')
        ->addFormat($lowBitrate, function($media) {
            $media->addFilter(function ($filters) {
                $filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
            });
        })
        ->addFormat($midBitrate, function($media) {
            $media->addFilter(function ($filters) {
                $filters->resize(new \FFMpeg\Coordinate\Dimension(1280, 960));
            });
        })
        ->addFormat($highBitrate, function($media) {
            $media->addFilter(function ($filters) {
                $filters->resize(new \FFMpeg\Coordinate\Dimension(1280, 960));
            });
        })
        ->save($path);

    $this->episode->update([
        'video' => $path,
    ]);

    FFMpeg::cleanupTemporaryFiles();

}

Your videos disk configuration is wrong.您的videos磁盘配置错误。 Try to store your videos this way:尝试以这种方式存储您的视频:

$episode->video = $request->file('video')->store('videos', 'local');

Then add a new route on routes/web.php :然后在routes/web.php上添加一个新路由:

Route::get('/episodes/{episode}/watch', 'EpisodeController@watch')->name('episodes.whatch');

The try this on your controller:在您的控制器上试试这个:

use Illuminate\Support\Facades\Storage

public function watch(Request $request, Episode $episode)
{
    return Storage::disk('local')->response(
        $episode->video,
        "optional_name_or_null",
        [
            'Content-Type' => 'application/x-mpegURL',
            'isHls' => true
        ]
    );

    //or alternatively use this one:
    /*
    return response()->file(
        Storage::disk('local')->path($episode->video),
        [
            'Content-Type' => 'application/x-mpegURL',
            'isHls' => true
        ]
    );
    */
}

Now you can access your video on: ' http://your-domain.com/episodes/1/watch '现在您可以访问您的视频:“ http://your-domain.com/episodes/1/watch

Note: Playing HLS/DASH videos are not natively supported on most of the browsers, try to use third-party plugins like this one to play these video formats.注意:大多数浏览器本身并不支持播放 HLS/DASH 视频,请尝试使用像这样的第三方插件来播放这些视频格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM