媒体请求范围如何运作?

How the media request range works?

我在 html 中有视频,视频标签如 <video src='xxxx.mp4'>。但是有时视频加载很慢。

我检查了媒体请求,发现它在第一个视频请求时尝试加载 1MB 的数据。请求头如下,但没有范围设置。

而且有些视频的首请求尺寸很小,所以第一帧显示很快。视频媒体请求如何工作,我可以影响每次请求的大小吗?

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Connection: keep-alive
Cookie: UM_distinctid=17e6bd4dd5c30-0f869b25cdf7158-4c3e207f-151800-17e6bd4dd5d5ce
Host: concert-cdn.jzurl.cn
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
TE: trailers
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

如果您仅使用视频标签,则特定浏览器会实施逻辑来决定请求的尺寸范围。一些浏览器实际上首先请求整个文件,然后中止该请求,然后是单独的范围请求。您还会看到来自浏览器的请求没有结束范围,因为它 'looks' 通过文件 - 逻辑似乎再次表明,如果不需要其余数据,可以简单地取消请求。

如果你使用 Javascript 播放器,比如 video.js 等,播放器理论上可以控制这种类型的东西,但实际上对于 mp4 文件,我认为,许多播放器只是利用无论如何,浏览器 HTML 视频标签功能。

关注您想要实现的目标,您可以采取一些措施来加快初始播放速度。

首先检查您的服务器接受范围请求,听起来您已经完成了。

接下来,假设您正在流式传输 mp4 文件,请确保元数据位于开头 - 众所周知的 'MooV' 原子。有几种工具可以让您进行此更改,包括 ffmpeg:

The mov/mp4/ismv muxer supports fragmentation. Normally, a MOV/MP4 file has all the metadata about all packets stored in one location (written at the end of the file, it can be moved to the start for better playback by adding faststart to the movflags, or using the qt-faststart tool). A fragmented file consists of a number of fragments, where packets and metadata about these packets are stored together. Writing a fragmented file has the advantage that the file is decodable even if the writing is interrupted (while a normal MOV/MP4 is undecodable if it is not properly finished), and it requires less memory when writing very long files (since writing normal MOV/MP4 files stores info about every single packet in memory until the file is closed). The downside is that it is less compatible with other applications.

如果以上内容不能满足您的需求,那么您可能需要考虑使用自适应比特率流媒体协议。几乎所有主要的流媒体服务都使用这种方法,但它确实需要在服务器端做更多的工作,并且通常需要一个特殊的流媒体打包服务器,尽管有开源的可用(例如 https://github.com/shaka-project/shaka-packager)。

ABR 创建视频的多个不同带宽版本,并将每个版本分成相同时间大小的块,例如2秒块。客户端设备或播放器一次下载一个视频块,并从最适合当前网络条件的比特率中选择下一个块。它可以选择一个低带宽块以允许快速启动,您经常会在商业流媒体服务中看到这种情况,在这些服务中,视频质量在启动时较低,然后随着随后请求高带宽块而随着时间的推移而提高。

更多信息在这里: