node.js - nodejs输出视频的实例

浏览:33日期:2022-09-12

问题描述

搜索了好久都没有找到比较详细的资料,只找到了 fs.createReadSream&fs.createWriteStream&pipe,也不太明白怎么用。有没有比较简单粗暴的输出案例呢;直接输出一个视频就行。(视频大小4g预算);

哎呀还有啊,怎么保存前端上传的视频呢(视频大小4G预算);

问题解答

回答1:

videoshow. PS:顺带说一句,要善用google和github,同事要善用英文搜索,不要用中文和百度.

回答2:

var fs = require(’fs’); var url = require('url');var server = require(’http’).createServer(function(req, res) {

if(req.url != '/favicon.ico'){ var pathname = url.parse(req.url).pathname; if(pathname == '/'){ res.writeHead(200, {’Content-Type’: ’video/mp4’}); var rs = fs.createReadStream(’./q0391tntxq6.mp4’); rs.pipe(res); rs.on(’end’,function(){ res.end(); console.log(’end call’); }); }else if(pathname == '/sp'){ var datas = fs.readFileSync('./1.html','utf-8') res.writeHead(200, {’Content-Type’: ’text/html’}); res.write(datas); res.end(' '); } }

}).listen(8080);

server.on(’error’,function(err){ console.log(’err’); });

//注意 //可以使用下面的代码替换上面的: rs.pipe(res); //但是当写的速度较读取慢时,会出现问题滴,当然对于在localhost 上 播放mp3文件是没有问题滴 //所以我们应该选择pipe来写流,pipe较好滴处理了读写不一致的问题 //rs.on(’data’,function(chunk){ res.write(chunk); });

//更多详细信息,参考:/a/119...

node部分------------------------------------------------------------------------

<html>

<head></head><body> <video controls='controls'> <source src='https://www.6hehe.com/' type='video/mp4'> nook </video></body>

</html>

html部分-------------------------------------------------------

相关文章: