问题描述
遵循此链接,我只是将webm格式更改为fMP4,如下所示.但这是行不通的.原始的test.webm文件可以正常工作.
Following this link, I just change the webm format to fMP4 as below. But it doesn't work. The original test.webm file works fine.
对于webm,配置如下:
for webm, the config as below:
//source: http://html5-demos.appspot.com/static/test.webm
var FILE = "test.webm"
var codec = 'video/webm; codecs="vorbis,vp8"';
对于fMP4,按如下所示更改配置:
for fMP4, change the config as below:
//source http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-85.mp4
var FILE = "car-20120827-85.mp4";
var codec = 'video/mp4; codecs="mp4a,avc"';
//var codec = 'video/mp4; codecs="mp4a.40.2,avc1.640028"';
我想fMP4的mime可能是错误的,但经过互联网上的大量搜索后,我仍无法弄清.
I guess the mime for fMP4 maybe wrong but I can not figure out it after many search from internet.
<!DOCTYPE html>
<html>
<body>
<section>
<video controls autoplay width="320" height="240"></video>
<pre id="log"></pre>
</section>
<script>
//source: http://html5-demos.appspot.com/static/test.webm
//var FILE = "test.webm"
//var codec = 'video/webm; codecs="vorbis,vp8"';
//source http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-85.mp4
var FILE = "car-20120827-85.mp4";
var codec = 'video/mp4; codecs="mp4a,avc"';
//var codec = 'video/mp4; codecs="mp4a.40.2,avc1.640028"';
var NUM_CHUNKS = 5;
var video = document.querySelector('video');
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if (!!!window.MediaSource) {
alert('MediaSource API is not available');
}
var mediaSource = new MediaSource();
video.src = window.URL.createObjectURL(mediaSource);
function callback(e) {
var sourceBuffer = mediaSource.addSourceBuffer(codec);
logger.log('mediaSource readyState: ' + this.readyState);
GET(FILE, function(uInt8Array) {
var file = new Blob([uInt8Array], {type: 'video/webm'});
var chunkSize = Math.ceil(file.size / NUM_CHUNKS);
logger.log('num chunks:' + NUM_CHUNKS);
logger.log('chunkSize:' + chunkSize + ', totalSize:' + file.size);
// Slice the video into NUM_CHUNKS and append each to the media element.
var i = 0;
(function readChunk_(i) {
var reader = new FileReader();
// Reads aren't guaranteed to finish in the same order they're started in,
// so we need to read + append the next chunk after the previous reader
// is done (onload is fired).
reader.onload = function(e) {
sourceBuffer.appendBuffer(new Uint8Array(e.target.result));
logger.log('appending chunk:' + i);
if (i == NUM_CHUNKS - 1) {
mediaSource.endOfStream();
} else {
if (video.paused) {
video.play(); // Start playing after 1st chunk is appended.
}
readChunk_(++i);
}
};
var startByte = chunkSize * i;
var chunk = file.slice(startByte, startByte + chunkSize);
reader.readAsArrayBuffer(chunk);
})(i); // Start the recursive call by self calling.
});
}
mediaSource.addEventListener('sourceopen', callback, false);
mediaSource.addEventListener('webkitsourceopen', callback, false);
mediaSource.addEventListener('webkitsourceended', function(e) {
logger.log('mediaSource readyState: ' + this.readyState);
}, false);
function GET(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
if (xhr.status != 200) {
alert("Unexpected status code " + xhr.status + " for " + url);
return false;
}
callback(new Uint8Array(xhr.response));
};
}
</script>
<script>
function Logger(id) {
this.el = document.getElementById('log');
}
Logger.prototype.log = function(msg) {
var fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode(msg));
fragment.appendChild(document.createElement('br'));
this.el.appendChild(fragment);
};
var logger = new Logger('log');
</script>
</body>
</html>
推荐答案
问题是fMP4
是与webm
不同的格式.虽然可以将webm
文件拆分为任意定义的块,但使用fMP4
则不能.
The problem is that fMP4
is a different format than webm
. While you can split a webm
file in chunks defined arbitrarily, you can't do so with fMP4
.
fMP4
文件只是一堆盒子",基本上是具有type
,length
和content
的结构(可以包含其他盒子,使其成为分层的).您必须先解析文件,然后首先为sourceBuffer分配一个moov
框(一个初始化段),然后再分配一系列可以解码和播放的moof mdat
框.
The fMP4
file is just a bunch of "boxes" which are basically structures with a type
, length
and content
(which can contain other boxes, making it hierarchical). You must parse the file and give the sourceBuffer a moov
box first (an initialization segment), and then a sequence of moof mdat
boxes that can be decoded and played out.
您可能必须阅读fMP4
格式(又名ISO BMFF
)的内容.
You probably have to read a bit on the fMP4
format (aka ISO BMFF
).
这篇关于MediaSourceExtension fMP4流媒体播放失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!