gpt4 book ai didi

node.js - YouTube - MP4 到 MP3 搞砸了

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

它应该下载播放列表中的所有视频并将它们转换为 mp3。但这所做的只是使 mp4 和 1 个空 mp3 的数字高于最大 mp4。我的新手大脑不知道如何解决这个问题......

var ytpl = require('ytpl');
var fs = require('fs-extra');
var path = require('path');
var ffmpeg = require('fluent-ffmpeg');
var binaries = require('ffmpeg-static');
var ytdl = require('ytdl-core');

var output_dir = path.join(__dirname+"/dl");

ytpl("PL8n8S4mVUWvprlN2dCAMoIo6h47ZwR_gn", (err, pl) => {
if(err) throw err;

let c = 0;

pl.items.forEach((i) => {
ytdl(i.url_simple+"", { filter: 'audioonly' }).pipe(fs.createWriteStream(output_dir+"/"+c+".mp4")).on('finish', () => {
console.log("Finished MP4 DL, starting conversion...");
ffmpeg(output_dir+"/"+c+".mp4")
.setFfmpegPath(binaries.path)
.format('mp3')
.audioBitrate(320)
.output(fs.createWriteStream(output_dir+"/"+c+".mp3"))
.on('end', () => {
console.log("Finished MP3 Convert...");
})
.run();
});
c++;
});

});

最佳答案

使用 ytdl-core 的示例进行了修复Link
固定代码:

var ytpl = require('ytpl');
var fs = require('fs-extra');
var path = require('path');
var ffmpeg = require('fluent-ffmpeg');
var binaries = require('ffmpeg-static');
var sanitize = require('sanitize-filename');
var ytdl = require('ytdl-core');

var output_dir = path.join(__dirname+"/dl");

ytpl("PL8n8S4mVUWvprlN2dCAMoIo6h47ZwR_gn", (err, pl) => {
if(err) throw err;

for (const i of pl.items) {
let stream = ytdl(i.id, {
filter: 'audioonly'
});

ffmpeg(stream)
.audioBitrate(320)
.save(`${output_dir}/${sanitize(i.title + " (by " + i.author.name + ")")}.mp3`)
.on('end', () => {
console.log("Done! Downloaded \""+i.title + " (by " + i.author.name + ")"+"\"");
});
}
});

关于node.js - YouTube - MP4 到 MP3 搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997856/

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