如何对媒体片段的任何部分进行范围请求? [英] How to make range request for any portion of a media fragment?

查看:106
本文介绍了如何对媒体片段的任何部分进行范围请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们想要生成 HTTP / 1.0 HTTP / 1.1 请求,例如字节20000到100000或给定媒体资源的秒数20到35秒,例如 .webm .ogg audio或 .mp4 视频,其中响应将是一个能够在没有媒体资源的其他部分的情况下播放的离散媒体片段,我们将如何实现这一目标?

If we wanted to make an HTTP/1.0 or HTTP/1.1 request, for example bytes 20000 to 100000 or seconds 20 through 35 seconds of a given media resource for example, .webm or .ogg audio or .mp4 video, where the response would be a discrete media fragment capable of playback without other portions of the media resource, how would we achieve this?

例如

let headers = new Headers();
let range = 1024 * 1024;
headers.append("Range", "bytes=0-" + range); 
let request = new Request(url, {headers:headers});
fetch(request)
.then(response => response.blob())
.then(data => audio.src = URL.createObjectURL(data));

加载和媒体播放

let headers = new Headers();
let range = 1024 * 1024;
headers.append("Range", "bytes=" + range + "-" + range * 2);
let request = new Request(url, {headers:headers});
fetch(request)
.then(response => response.blob())
.then(data => audio.src = URL.createObjectURL(data));

成功记录 200 206 响应状态,但不会在< audio> 元素中呈现媒体播放。

logs successful 200 and 206 response statuses, though does not render media playback at <audio> element.

如何为给定媒体创建范围请求,该请求仅返回所请求的资源范围作为能够在没有媒体的其他部分的情况下播放的离散资源,其中所请求的媒体片段可以是媒体的任何离散部分资源?

How to create a range request for a given media, which returns only the requested range of the resource as a discrete resource capable of playback without other portions of the media, where requested media fragment can be any discrete portion of the media resource?

推荐答案

你根本做不到。

你绝对是需要您的媒体文件(元数据)的标题才能让您的浏览器能够解码它所包含的数据。

You absolutely need the headers of your media file (metadata) for your browser to be able to decode the data it contains.

不同的媒体格式将具有不同的解析规则,其中包含大量的以不同方式排序的数据只获得原始数据的一部分将破坏整个数据结构。因此,对于某些文件格式,您可以通过仅提供文件的开头来播放媒体的开头,但所有媒体格式都不允许它,甚至不期望开始结束阅读。

Different media formats will have different parsing rules, with chunks of data ordered differently and getting only a portion of the raw data will break the whole structure of the data. So with some file formats, you might be able to play the beginning of an media by supplying only the beginning of the file, but all media formats don't allow it and don't even expect an start to end reading.

可以做的是使用 src 的nofollow noreferrer>时间范围参数

What can be done though is to use the timerange parameter of the MediaElement's src:

#t = [starttime] [,endtime]

const url = 'https://upload.wikimedia.org/wikipedia/commons/4/4b/011229beowulf_grendel.ogg';

btn.onclick = e => {
  // fast input check for the demo
  if (max.value > aud.duration)
    max.value = aud.duration;
  if (min.value > max.value)
    min.value = max.value - 1;
  // construct our timerange parameter
  let range = '#t=' + min.value + ',' + max.value;
  // append it to our original url
  aud.src = url + range;
}
btn.onclick();

<audio id="aud" aud controls></audio><br>
<label>start: <input id="min" type="number" value="10" min="0" max="119"></label>
<label>end: <input id="max" type="number" value="20" min="1" max="120"></label>
<button id="btn">re-load</button>

这篇关于如何对媒体片段的任何部分进行范围请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆