How do I retrieve individual video URLs from a playlist, using youtube-dl within a python script?

浪尽此生 提交于 2019-12-03 04:06:36

I'm working on cleaning this up so more, but I discovered the answer in a section of another answer...

To get individual links from a playlist url:

ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s', 'quiet':True,})
video = ""

with ydl:
    result = ydl.extract_info \
    (yt_url,
    download=False) #We just want to extract the info

    if 'entries' in result:
        # Can be a playlist or a list of videos
        video = result['entries']

        #loops entries to grab each video_url
        for i, item in enumerate(video):
            video = result['entries'][i]

the youtube_dl.YoutubeDL seems to return JSON data from the YouTube API yt_url is a variable for either a video or playlist.

If the returned data has "entries" it's a playlist - then I loop those each entry (enumerate entries with i(ndex)) -- from there I can do what I want with the urls or other info.

result['entries'][i]['webpage_url']     #url of video
result['entries'][i]['title']           #title of video
result['entries'][i]['uploader']        #username of uploader
result['entries'][i]['playlist']        #name of the playlist
result['entries'][i]['playlist_index']  #order number of video 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!