有没有一种方法可以流式下载正在nodejs服务器上转换的mp3文件?

时间:2019-02-18 04:48:28

标签: node.js ffmpeg

我正在寻找一种将URL发送到nodejs服务器并以mp3文件下载响应用户的方法。

我搜索了一些示例,并阅读了有关请求和响应的信息,但我不确定问题出在哪里。

这是HTML的Javascript:

    var downloadBtn = document.querySelector('.download_button');
    var URLinput = document.querySelector('#myUrl');

    downloadBtn.addEventListener('click', () => {
        console.log(`URL: ${URLinput.value}`);
        sendURL(URLinput.value);
    });

    function sendURL(URL) {
        window.location.href = `http://localhost:4000/download?URL=${URL}`;
    }

这是Nodejs服务器的Javascript:

const express = require('express');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();
const ffmpeg = require('fluent-ffmpeg')
app.use(cors());

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

app.get('/download', (req,res) => {
var URL = req.query.URL;

res.header('Content-Disposition', 'attachment; filename="file.mp3"');
let stream = ytdl(URL, { 
  quality: 'highestaudio',
}); //HERE THE STREAM FILE IS SELECTED TO BE CONVERTED TO MP3

ffmpeg(stream)
  .audioBitrate(128)
  .pipe(res); // HERE IS CONVERTED AND WHERE I WANT IT TO SEND IT AS A DOWNLOAD TO THE USER.
});

我希望它能够流式下载文件,但相反,它将我带到nodejs服务器页面的/ download / url_to_vid

0 个答案:

没有答案