使用 youtube-dl 从播放列表列表中获取视频信息 [英] Get video information from a list of playlist with youtube-dl

查看:42
本文介绍了使用 youtube-dl 从播放列表列表中获取视频信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 youtube-dl 从 youtube 的播放列表列表中获取一些信息.我已经编写了这段代码,但它需要的不是视频信息而是播放列表信息(例如播放列表标题而不是播放列表中的视频标题).我不明白为什么.

I'm tryng to get some informations from a list of playlists in youtube with youtube-dl. I've written this code but what it takes is not the video's informations but the playlist informations (e.g. the playlist title instead of the video title in the playlist). I can't understand why.

input_file = open("url")
for video in input_file:
    print(video)
ydl_opts = {
    'ignoreerrors': True
}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl: 
                info_dict = ydl.extract_info(video, download=False)
                for i in info_dict:
                    video_thumbnail = info_dict.get("thumbnail"),
                    video_id = info_dict.get("id"),
                    video_title = info_dict.get("title"),
                    video_description = info_dict.get("description"),
                    video_duration = info_dict.get("duration")

任何帮助将不胜感激.

推荐答案

你调用的变量 video 实际上保存的是播放列表信息,而不是视频信息.您可以在播放列表的 entries 属性中找到单个视频信息的列表.

The variable you call video actually holds the playlist information, not the video information. You can find a list of the individual video information in the playlist's entries attribute.

有关可能的修复,请参见下文.我将您的 video 变量重命名为 playlist 并自由地重写它并添加输出:

See below for a possible fix. I renamed your video variable to playlist and took the freedom to rewrite it a bit and add output:

ydl_opts = {
    'ignoreerrors': True,
    'quiet': True
}

input_file = open("url")

for playlist in input_file:

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:

        playlist_dict = ydl.extract_info(playlist, download=False)

        for video in playlist_dict['entries']:

            print()

            if not video:
                print('ERROR: Unable to get info. Continuing...')
                continue

            for prop in ['thumbnail', 'id', 'title', 'description', 'duration']:
                print(prop, '--', video.get(prop))

这篇关于使用 youtube-dl 从播放列表列表中获取视频信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆