如何使用node.js下载视频mp4文件?

时间:2013-03-30 00:19:33

标签: javascript node.js express

我想让用户从我的AWS S3存储桶下载视频。视频格式为MP4:

app.get("/download_video", function(req,res) {
    filename = "s3.xxx.amazon.com/bucketname/folder/video_example.mp4";
    // im stuck on what i can do here
});

有很多关于如何使用nodejs在线下载图像和文本文件的示例,但我在视频上找不到任何内容。

2 个答案:

答案 0 :(得分:1)

尝试一下

const fetch = require('node-fetch');
const fs = require('fs');
 
const response = await fetch(yourUrl);
const buffer = await response.buffer();

 fs.writeFile(`./videos/name.mp4`, buffer, () => 
   console.log('finished downloading video!'));                                          

答案 1 :(得分:0)

使用严格

const Fs = require('fs')

const Path = require('path')

const Listr = require('listr')

const Axios = require('axios')

功能一(任务){

任务

.run()


.then(process.exit)


.catch(process.exit)

}

if(process.argv){

const任务= [

{

  title: 'Downloading',


  task: async (ctx, task) => {


    const url = 'https://s3.xxx.amazon.com/bucketname/folder/video_example.mp4"'


    const path = Path.resolve(__dirname, 'media', 'video.mp4')



    const response = await Axios({
      method: 'GET',
      url: url,
      responseType: 'stream'
    })

    response.data.pipe(Fs.createWriteStream(path))

    return new Promise((resolve, reject) => {
      response.data.on('end', () => {
        resolve()
      })

      response.data.on('error', err => {
        reject(err)
      })
    })
  }
}

]

一个(新列表器(任务)) }