gpt4 book ai didi

node.js - ffmpeg mp3 通过 Node js 流式传输

转载 作者:行者123 更新时间:2023-12-04 23:27:29 30 4
gpt4 key购买 nike

var fs = require('fs');
var child = require('child_process');
var http=require('http')
var input_file = fs.createReadStream('./remo.mp3');
http.createServer(function (req,res) {
var args = ['-ss',120,
'-i', 'remo.mp3',
'-f','mp3',
'pipe:1' // Output on stdout
];
var trans_proc = child.spawn('ffmpeg', args);
res.writeHead(200, {
'Content-Type': 'audio/mpeg'
});

trans_proc.stdout.pipe(res)
trans_proc.stderr.on('data',function (err) {
console.log(err.toString());
})
}).listen(2000)

我正在尝试剪切 mp3 并流式传输到浏览器,但在浏览器中它显示损坏的文件

最佳答案

我不知道您是否在询问 下载一个使用 Node 的 youtube 视频作为 mp3 - 但是当我正在研究它时,这个线程突然出现在谷歌之上。所以,如果不是,也许它可以为您指明正确的方向或在 future 帮助某人...... Mods - 抱歉,如果我做的不对。

Additional StackOverflow Reference

但是我使用下面的代码来做下载一个 youtube vid 作为 mp3(下载 youtube vid/转换为 mp3/下载):

module.exports.toMp3 = function(req, res, next){
var id = req.params.id; // extra param from front end
var title = req.params.title; // extra param from front end
var url = 'https://www.youtube.com/watch?v=' + id;
var stream = youtubedl(url); //include youtbedl ... var youtubedl = require('ytdl');

//set response headers
res.setHeader('Content-disposition', 'attachment; filename=' + title + '.mp3');
res.setHeader('Content-type', 'audio/mpeg');

//set stream for conversion
var proc = new ffmpeg({source: stream});

//currently have ffmpeg stored directly on the server, and ffmpegLocation is the path to its location... perhaps not ideal, but what I'm currently settled on. And then sending output directly to response.
proc.setFfmpegPath(ffmpegLocation);
proc.withAudioCodec('libmp3lame')
.toFormat('mp3')
.output(res)
.run();
proc.on('end', function() {
console.log('finished');
});

};

关于node.js - ffmpeg mp3 通过 Node js 流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952422/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com