I'm trying to setup a very basic html5 page that loads a .mp4 video that is 20MB. It appears that the browser needs to download the entire thing rather than just playing the first part of the video and streaming in the rest.
This post is the closest thing I've found while searching... I tried both Hand Brake and Data Go Round by neither appeared to make a difference:
Any ideas on how to do this or if it's possible?
Here is the code I'm using:
<video controls="controls"> <source src="/video.mp4" type="video/mp4" /> Your browser does not support the video tag. </video>
Ensure that your web server is reporting the correct Content-Type (video/mp4). Ensure that your web server is configured to serve byte range requests. Ensure that your web server is not applying gzip or deflate compression on top of the compression in the mp4 file.
Mp4 is a great format for downloadable videos but it's no fit for video streaming.
HTML5 and the video formats like mp4, WebM, Theora, etc are two entirely different things. HTML5 is the framework in which the video is displayed and delivered.
On a PC running Windows 10, select Start > File Explorer, or select File Explorer from the taskbar. Press and hold (or right-click) the MP4 file you want to play, select Open with, and then choose your preferred file player from the list of applications.
moov
(metadata) is before the mdat
(audio/video data). This is also called "fast start" or "web optimized". For example, Handbrake has a "Web Optimized" checkbox, and ffmpeg and avconv have the output option -movflags faststart
.You can check the headers being sent by your web server using curl -I http://yoursite/video.mp4
or using the developer tools in your browser (Chrome, Firefox) (reload the page if it is cached). The HTTP Response Header should include Content-Type: video/mp4 and Accept-Ranges: bytes, and no Content-Encoding:.
Here is the solution I used to create a Web API Controller in C# (MVC) that will serve video files with Byte Ranges (partial requests). Partial requests allow a browser to only download as much of the video as it needs to play rather than downloading the entire video. This makes it far more efficient.
Note this only works in recent versions.
var stream = new FileStream(videoFilename, FileMode.Open, FileAccess.Read , FileShare.Read); var mediaType = MediaTypeHeaderValue.Parse($"video/{videoFormat}"); if (Request.Headers.Range != null) { try { var partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent); partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, mediaType); return partialResponse; } catch (InvalidByteRangeException invalidByteRangeException) { return Request.CreateErrorResponse(invalidByteRangeException); } } else { // If it is not a range request we just send the whole thing as normal var fullResponse = Request.CreateResponse(HttpStatusCode.OK); fullResponse.Content = new StreamContent(stream); fullResponse.Content.Headers.ContentType = mediaType; return fullResponse; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With