search

Home  >  Q&A  >  body text

node.js - Example of nodejs output video

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中文网2757 days ago588

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:39:02

    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.

    reply
    0
  • PHPz

    PHPz2017-05-16 13:39:02

    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------------------------------------------------ --------

    reply
    0
  • Cancelreply