简体   繁体   English

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

[英]how can i download a video mp4 file using node.js?

I want to let users download a video from my AWS S3 bucket.我想让用户从我的 AWS S3 存储桶下载视频。 The video format is MP4:视频格式为 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
});

There are a lot of examples on how to download images and textfiles online using nodejs, but I can't find anything on videos.有很多关于如何使用 nodejs 在线下载图像和文本文件的示例,但我在视频上找不到任何内容。

use strict

const Fs = require('fs')
const Path = require('path')
const Listr = require('listr')
const Axios = require('axios')

function one (tasks) {

  tasks.run()
   .then(process.exit)
   .catch(process.exit)
}


if (process.argv) {

  const tasks = [{
    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)
        })
      })
    }
  }]

  one(new Listr(tasks))
}

Try this试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM