簡體   English   中英

如何將帶有Node的視頻發送到客戶端JavaScript blob然后顯示它?

[英]How do I send video with Node to a client JavaScript blob and then display it?

我想將視頻文件發送到客戶端並使用.createObjectURL()顯示視頻。

節點server.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的對等視頻實現,以便靜態視頻數據可以從客戶端發送到另一個客戶端而不通過它發送服務器。

我只是將video.mp4作為靜態資源提供,並將視頻元素的src設置為該視頻的URL。

這是純粹的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 + "/");

請享用,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM