我正在使用ytdl-core和youtube-playlist制作YouTube播放列表下载器。我想列出播放列表中的所有标题。这是我的代码:
ytlist(playlist_url, 'url').then(res => {
var video_url = res.data.playlist;
var total_files = video_url.length;
video_url.forEach(function(url, index) {
ytdl.getInfo(url, (err, info) => {
console.log(index + " : " + info.title);
});
});
});
并且当我开始提供链接到播放列表的代码时,会发生这种情况:
7 : C418 - Minecraft - Minecraft Volume Alpha
3 : C418 - Death - Minecraft Volume Alpha
4 : C418 - Living Mice - Minecraft Volume Alpha
19 : C418 - Dog - Minecraft Volume Alpha
14 : C418 - Chris - Minecraft Volume Alpha
17 : C418 - Sweden - Minecraft Volume Alpha
16 : C418 - Excuse - Minecraft Volume Alpha
15 : C418 - Thirteen - Minecraft Volume Alpha
22 : C418 - Droopy likes Ricochet - Minecraft Volume Alpha
20 : C418 - Danny - Minecraft Volume Alpha
0 : C418 - Key - Minecraft Volume Alpha
21 : C418 - Beginning - Minecraft Volume Alpha
8 : C418 - Oxygène - Minecraft Volume Alpha
9 : C418 - Équinoxe - Minecraft Volume Alpha
2 : C418 - Subwoofer Lullaby - Minecraft Volume Alpha
12 : C418 - Wet Hands - Minecraft Volume Alpha
18 : C418 - Cat - Minecraft Volume Alpha
10 : C418 - Mice on Venus - Minecraft Volume Alpha
..... and so on
播放列表中的项目应从0开始,但不是。我意识到ytdl-core lib是异步的,但我不知道如何使其同步。你能帮助我吗?抱歉,格式错误。这是我的第一个问题。
ytlist(playlist_url, 'url').then(res => {
var video_url = res.data.playlist;
var total_files = video_url.length;
video_url.forEach(async function(url, index) {
const info = await ytdl.getInfo(url);
console.log(info);
});
});
异步/等待应该为您完成这项工作