我从 Nodejs 构建了一个流服务。这是我的服务器代码 server.js
var http = require('http'),
fs = require('fs'),
util = require('util');
http.createServer(function (req, res) {
var path = __dirname+'/movie.mp4';
var stat = fs.statSync(path);
var total = stat.size;
if (req.headers['range']) {
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total-1;
var chunksize = (end-start)+1;
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);
var file = fs.createReadStream(path, {start: start, end: end});
res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4' });
file.pipe(res);
} else {
console.log('ALL: ' + total);
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
fs.createReadStream(path).pipe(res);
}
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
这是我的Page.html
<!DOCTYPE html>
<html>
<body>
<video scr="http://localhost:1337" preload autoplay controls></video>
</body>
</html>
视频标签不显示任何视频,但是当我输入地址 http://localhost:1337 时在我的浏览器地址中,它播放视频。
如何在我的页面的视频标签中获取视频?也许我应该使用一些 js 代码客户端?
最佳答案
已解决。我的视频标签“src”而不是“scr”存在语法错误
关于javascript - 从nodejs流中获取video.mp4视频标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873320/