繁体   English   中英

如何从 https 链接下载 MP4 文件并在 Node.js 中重定向?

[英]How can I download MP4 file from https link with redirect in Node.js?

我再次遇到一个我无法解决的问题,我正在学习 Node.js 并且有很多我不知道的事情。 事情是这样的,我有一个名为 downloader.js 的文件

const https = require ('https')
const fs = require ('fs')
const path = require ('path')

function downloader (url, callback) {

    const filename = path.basename (url)

    const req = https.get (url, (res) => {
        const file = fs.createWriteStream (`$ {filename} .mp4`)
        res.pipe (file)

        file.on ("error", (error) => {
            console.log (`There was an error writing the file. Details: $ {error}`)
        })

        file.on ("close", () => {
            callback (filename)
        })

        file.on ('finish', () => {
            file.close ()
            console.log ("Completely downloaded.")
        })
    })

    req.on ("error", (error) => {
        console.log (`Error downloading file. Details: $ {error}`)
    })
}

module.exports = {
    downloader
}

And I have a video, which I get the download url from a call to the Zoom api, with the following link: https://us02web.zoom.us/rec/download/06L34fQudvvSaAGYJ6Chk6RC0fuV-ebwyu9Ar_Ihrm4WRmD3xbpPAjnYfIFInOBz1PBPPAjnYPhoJlf4p3xbpPAjnYFIFIXMD3xbpPAjnYFIFIXMD3xbpPAjnYphoJlf4

调用下载器 function 时,一切正常,我下载的是 mp4 格式的文件,但问题是它不是 mp4 格式的完整 700MB 视频,而是 mp4 格式的 html 文件。

在这里你可以明白我的意思

我不明白为什么文件不能正确下载。 当我使用任何其他类型的文件(例如 jpeg 图像)执行此操作时,它会毫无问题地下载它,但使用 mp4 格式我无法下载文件。 你认为正在发生什么?

鉴于您发布的链接指向错误页面,指出该记录不存在,我无法确认此解决方案是否有效。

但是,假设它的功能类似于其他录制链接,当您在 web 浏览器中导航到该位置时,您将自动重定向到新的 uri 以自动开始下载。

假设是这种情况,您实际上正在寻找的是如何在外部 API 请求上遵循重定向。

我会推荐 npm axios package,因为它会自动跟随重定向。

然后,您可以执行以下操作:

const axios = require('axios');
const fs = require('fs');
const path = require('path');

function downloader(url, callback) {
    axios({
        method: 'get',
        url: url,
        responseType: 'stream'
    })
        .then(function (response) {
            return new Promise((resolve, reject) => {
                const filename = path.basename(url);
                const file = fs.createWriteStream(`${filename}.mp4`);
                response.data.pipe(file);

                file.on("error", (error) => {
                    return reject(`There was an error writing the file. Details: $ {error}`);
                });

                file.on('finish', () => {
                    file.close();
                });

                file.on('close', () => {
                    return resolve(filename);
                });
            });
        })
        .catch(function (error) {
            // handle error
            console.log(error);
        })
        .then(function (filename) {
            callback(filename);
        })
}

module.exports = {
    downloader
};

You could also directly return the axios promise and make your downloader function return a Promise instead of a function that takes a callback.

如果您希望保留https变量,您也可以尝试跟随重定向package。

暂无
暂无

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

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