简体   繁体   English

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

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

Again I have a problem that I cannot solve, I am learning Node.js and there are many things that I do not know.我再次遇到一个我无法解决的问题,我正在学习 Node.js 并且有很多我不知道的事情。 The thing is the following, I have the following file called downloader.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 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

When calling the downloader function, all good, I download the file in mp4 format but, the problem is that it is not the full 700MB video in mp4 format, but it is an html document in mp4 format.调用下载器 function 时,一切正常,我下载的是 mp4 格式的文件,但问题是它不是 mp4 格式的完整 700MB 视频,而是 mp4 格式的 html 文件。

Here you can see what I mean在这里你可以明白我的意思

I do not understand why the file does not download correctly.我不明白为什么文件不能正确下载。 When I do it with any other type of file such as a jpeg image, it downloads it without problems, but with the mp4 format I can't get the file to download.当我使用任何其他类型的文件(例如 jpeg 图像)执行此操作时,它会毫无问题地下载它,但使用 mp4 格式我无法下载文件。 What do you think is happening?你认为正在发生什么?

Given that the link you posted points to an Error page saying the recording does not exist, I was unable to confirm this solution works.鉴于您发布的链接指向错误页面,指出该记录不存在,我无法确认此解决方案是否有效。

However, assuming it functions similar to other recording links, when navigating to the location in a web browser, you are automatically redirected to a new uri to automatically start the download.但是,假设它的功能类似于其他录制链接,当您在 web 浏览器中导航到该位置时,您将自动重定向到新的 uri 以自动开始下载。

Assuming this is the case, what you are actually looking for is how to follow redirects on an external API request.假设是这种情况,您实际上正在寻找的是如何在外部 API 请求上遵循重定向。

I would recommend the npm axios package, since it will automatically follow redirects.我会推荐 npm axios package,因为它会自动跟随重定向。

You can then do something like:然后,您可以执行以下操作:

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. You could also directly return the axios promise and make your downloader function return a Promise instead of a function that takes a callback.

You could also try the follow-redirects package if you were looking to keep the https variable.如果您希望保留https变量,您也可以尝试跟随重定向package。

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

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