Skip to content Skip to sidebar Skip to footer

How Do I Retrieve Individual Video URLs From A Playlist, Using Youtube-dl Within A Python Script?

I'm trying to: Use youtube-dl within a Python script to download videos periodically Organize/name the videos dynamically by youtube data i.e. %(title)s Extract audio/MP3 and move

Solution 1:

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 

Post a Comment for "How Do I Retrieve Individual Video URLs From A Playlist, Using Youtube-dl Within A Python Script?"