node.js - 有没有办法流式下载正在 nodejs 服务器上转换的 mp3 文件?
问题描述
我正在寻找一种将 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
解决方案
推荐阅读
- javascript - 为什么我在 Fetch api 删除请求中收到此数据属性错误?
- firebase - Firestore 性能
- ruby-on-rails - 安装 gem capybara-webkit 时出错,出现以下错误:Gem::Ext::BuildError: ERROR: Failed to build gem native extension
- dashboard - 如何与多个客户端共享 quicksight 嵌入式仪表板
- python - 在for循环中赋值之前引用的局部变量
- r - GSL中的多元线性回归是否正确?
- android - Android Google Map SDK 工具栏问题——不启动外部地图
- python - 如何在 python 中获取 GPU 的模型并将其保存为字符串?
- mysql - 如果我不能进行索引和分区,如何使这个 MySQL 查询工作得更快?
- r - 通过关键短语对这样的文本进行子集化的最佳方法是什么?