node.js - Example of nodejs output video
PHP中文网
PHP中文网 2017-05-16 13:37:02
0
2
463

After searching for a long time, I couldn't find any more detailed information. I only found fs.createReadSream&fs.createWriteStream&pipe.
I don't quite understand how to use it. Is there any simpler and more crude output case? Just output a video directly. (Video size 4g budget);

Oh, there is one more thing, how to save the video uploaded by the front end (video size 4G budget);

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
伊谢尔伦

videoshow.
PS: By the way, you should make good use of Google and GitHub, and colleagues should make good use of English search instead of Chinese and Baidu.

PHPzhong

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');
});

//Note
//You can use the following code to replace the above: rs.pipe(res);
//But when the writing speed is slower than the reading speed, problems will occur. Of course, for playing mp3 files on localhost There is no problem
//So we should choose pipe to write the stream. Pipe can better handle the problem of inconsistent reading and writing
//rs.on('data',function(chunk){ res.write(chunk); });

//For more details, refer to: /a/119...

node part------------------------------------------------- --------------------------

<html>

<head>

</head>
<body>
    <video width="320" height="240" controls="controls">
      <source src="/" type="video/mp4">
    nook
    </video>
</body>

</html>

html part------------------------------------------------ --------

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!