如何使用 Node 将视频发送到客户端 JavaScript blob 然后显示它?

问题描述 投票:0回答:2

我想发送一个视频文件到客户端并使用.createObjectURL()显示视频。

节点服务器.js:

var fs = require("fs"),
http = require("http");

http.createServer(function (req, res) {

  if (req.url == "/") {
    res.writeHead(200, { "Content-Type": "text/html" });

    res.end('<video id="video" src="" autoplay controls loop width="200px" height="200px" muted></video>' +
      '<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>' +
      '<script src="blobvideo.js"></script>'); 
  }

  else if (req.url == "/blobvideo.js") {
    res.writeHead(200, { "Content-Type": "application/javascript" });
    fs.readFile('./blobvideo.js', function(err, data) {
      res.end(data.toString());
    });

  }

  else if (req.url == "/video") {
    fs.readFile('video.mp4', function(err, data) {
      res.end(data);
    });
  }
}).listen(3000);

客户端 blobvideo.js:

$.ajax( "/video" ).done(function(data) {
  var ab = new ArrayBuffer(data.length);
  var view = new Uint8Array(ab);

  for(var i = 0; i < data.length; ++i) {
    view[i] = data[i];
  }

  blob = new Blob([ab], { type: "video/mp4" });
  document.getElementById("video").src = (window.URL || window.webkitURL).createObjectURL(blob);
});

在此代码中,视频是整段发送的,并且视频不播放。我的问题:

  1. 如何修复此问题以播放视频?

  2. 如何将其更改为流式传输文件而不是等待整个视频下载?

编辑澄清

我想在客户端使用

Blob
.createObjectURL()
,因为我正在尝试构建WebRTC RTCPeerConnection的点对点视频实现,以便静态视频数据可以从客户端发送到另一个客户端,而无需发送通过服务器。

javascript node.js video-streaming
2个回答
0
投票

这是纯nodejs javascript,它可以流式传输视频/音频,无需外部库依赖项,并且不需要客户端代码...使用以下方式启动它:

node this_code.js

然后将您的客户端浏览器指向

http://localhost:8888

巨大的好处是浏览器客户端视频渲染 UI 小部件可以正常工作 -(能够跳转到某个随机媒体位置等)

var http = require('http'),
    fs = require('fs'),
    util = require('util');

// put any audio or video file here
var path = "/path/to/audio/or/video/file/local/to/server/cool.mp4";

var port = 8888;
var host = "localhost";

http.createServer(function (req, res) {

  var stat = fs.statSync(path);
  var total = stat.size;

  if (req.headers.range) {

    // meaning client (browser) has moved the forward/back slider
    // which has sent this request back to this server logic ... cool

    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(port, host);

console.log("Server running at http://" + host + ":" + port + "/");

享受,


-2
投票

我只需将 video.mp4 作为静态资源提供,并将视频元素的

src
设置为该视频的 URL。

© www.soinside.com 2019 - 2024. All rights reserved.