Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel VideoStream.
<?php
Route::get('/player', function () {
$video = "video/os_simpsons_s25e22_720p.mp4";
$mime = "video/mp4";
$title = "Os Simpsons";
return view('player')->with(compact('video', 'mime', 'title'));
});
Route::get('/video/{filename}', function ($filename) {
// Pasta dos videos.
$videosDir = base_path('resources/assets/videos');
if (file_exists($filePath = $videosDir."/".$filename)) {
$stream = new \App\Http\VideoStream($filePath);
return response()->stream(function() use ($stream) {
$stream->start();
});
}
return response("File doesn't exists", 404);
});
<?php
namespace App\Http;
/**
* Description of VideoStream
*
* @author Rana
* @link https://gist.github.com/vluzrmos/d5682ad426525196d069
*/
class VideoStream
{
/* Visite o link https://gist.github.com/vluzrmos/d5682ad426525196d069 para ver o conteúdo desse arquivo. */
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{$title}}</title>
<link href="//vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">
</head>
<body>
<video id="example_video_1" class="video-js vjs-default-skin vjs-big-play-centered"
controls preload="auto" height="600" width="980">
<source src="{{url($video)}}" type="{{$mime}}" />
</video>
<script src="//vjs.zencdn.net/4.12/video.js"></script>
<script>
videojs(document.getElementById('example_video_1'), {}, function() {
// This is functionally the same as the previous example.
});
</script>
</body>
</html>
@laravelish

This comment has been minimized.

Copy link

laravelish commented Oct 9, 2015

this is realy usefull. Thanks.

@noptongbai

This comment has been minimized.

Copy link

noptongbai commented Feb 17, 2016

Thanks you

@huungnguyen

This comment has been minimized.

Copy link

huungnguyen commented Jul 29, 2016

thank you. it helped me a lot

@dioimamwibisana

This comment has been minimized.

Copy link

dioimamwibisana commented Sep 8, 2016

Thank you so much, it helped me too

@ggary9424

This comment has been minimized.

Copy link

ggary9424 commented Sep 24, 2016

Thanks you. Very helpful.

@fendiseptiawan0709

This comment has been minimized.

Copy link

fendiseptiawan0709 commented Feb 18, 2017

Is this support for playing 'avi' video extension?

@MohammedAl-Mahdawi

This comment has been minimized.

Copy link

MohammedAl-Mahdawi commented Mar 24, 2017

Thank you

@dropoutninja

This comment has been minimized.

Copy link

dropoutninja commented Jun 19, 2017

thanks a lot

@bvdzung

This comment has been minimized.

Copy link

bvdzung commented Sep 20, 2017

thanks a lot. very good.

@AlexandrBabich

This comment has been minimized.

Copy link

AlexandrBabich commented Oct 13, 2017

Hello guys. I had same error in IE11. To solve this issue you need add header("Accept-Ranges: bytes"); after 83 string in VideoStream class.
This header is important for IE browsers.

@savepong

This comment has been minimized.

Copy link

savepong commented Feb 27, 2018

Thank you so much.

@nteej

This comment has been minimized.

Copy link

nteej commented Jun 25, 2018

Thanks a lot.

@nerg4l

This comment has been minimized.

Copy link

nerg4l commented Nov 13, 2018

I would recommend to use \ResponseFactory::download or \Symfony\Component\HttpFoundation\BinaryFileResponse directly, because Content-Range header already supported by them.

Route::get('/video/{filename}', function ($filename) {
    $videosDir = base_path('resources/assets/videos');
    $filePath = $videosDir.'/'.$filename

    if (file_exists($filePath)) {
        return abort(404);
    }

    $name = basename($filePath);
    return response()->download($filePath, $name, [
        'Content-Type' => 'video/mp4',
    ]);
});
Route::get('/video/{filename}', function ($filename) {
    $videosDir = base_path('resources/assets/videos');
    $filePath = $videosDir.'/'.$filename

    if (file_exists($filePath)) {
        return abort(404);
    }

    $response = new BinaryFileResponse($filePath, 200, [
        'Content-Type' => 'video/mp4',
    ]);
    $name = basename($filePath);
    $response->setContentDisposition('attachment', $name, str_replace('%', '', Str::ascii($name)));

    return $response;
});

The second version is basicly just an unwrapped version of the first one.

@techmadeeasy

This comment has been minimized.

Copy link

techmadeeasy commented Apr 7, 2020

Hey guys, this is working thank you for the help. Just one question how can you use this solution to stream a live video.. from a software like OBS or Wirecast. Basically you have a camera connected to your computer and you want to stream the video. thank you

@cmarixdev

This comment has been minimized.

Copy link

cmarixdev commented Jun 12, 2020

Hey guys, this is working thank you for the help. Just one question how can you use this solution to stream a live video.. from a software like OBS or Wirecast. Basically you have a camera connected to your computer and you want to stream the video. thank you

Please drop the solution here if you get it. I'm looking for the similar concept of live video streaming

@michaelcalayag

This comment has been minimized.

Copy link

michaelcalayag commented Jun 18, 2020

This is really useful... thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.