nodejs copy视频到当前目录(stream)

2017-07-10  本文已影响688人  撸猫真君

方法一:

var fs=require('fs');

var readStream=fs.createReadStream('1.mp4');

var writeStream=fs.createWriteStream('1-stream.mp4');

readStream.on('data',function(chunk){

if( writeStream.write(chunk) ===false ){

console.log('still cached');

readStream.pause();

}

})

readStream.on('end',function(){

writeStream.end();

})

writeStream.on('drain',function(){

console.log('data drains');

readStream.resume();

})

方法二(pipe|管道):

var fs= require('fs');

fs.createReadStream('1.mp4').pipe(fs.createWriteStream('1-pipe.mp4'));

上一篇下一篇

猜你喜欢

热点阅读