【问题标题】:problem with downloading videos with ytdl-core express使用 ytdl-core express 下载视频的问题
【发布时间】:2020-05-05 12:16:45
【问题描述】:

我正在尝试构建一个简单的 node express 应用来使用 ytdl-core 从 youtube 下载视频,但由于某种原因我的代码无法正常工作。 我几乎尝试了所有方法,应用程序收到请求但没有执行任何操作,页面似乎已刷新但没有开始下载。

script.js

var confirmText = "Downloading videos from YouTube is against the YouTube Policy.\n"
                    + "The only videos that your allowed to download is your own.";


function downloadVideo() {
    if (confirm(confirmText)){
        var URLinput = document.getElementById("URL-input").value;
        var req = new XMLHttpRequest();
        req.open('get', '/download?url=' + URLinput, false);
        req.send();
    }
}

index.js

const express = require('express');
const path = require('path');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();

app.use(cors());
app.use(express.static("public"));

app.listen(4000, () => {
    console.log('Server Works !!! At port 4000');
});

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.get('/download', (req, res) => {
    var url_input = req.query.url;
    console.log(url_input);
    res.header('Content-Disposition', 'attachment; filename="video.mp4"');
    ytdl(url_input, {format: 'mp4'}).pipe(res);
});

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', '404.html'));
});

【问题讨论】:

    标签: node.js express node-modules


    【解决方案1】:

    我也遇到了同样的问题,试着把这个放在你的script.js,它对我有用。

    window.location.replace(/*server url root*/ '/download?url=' + URLInput);
    

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 2016-12-13
      • 1970-01-01
      • 2022-09-30
      • 2019-09-23
      • 2022-06-17
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多