Search code examples
pythonpafy

Is there a way to get YT url or video ID from playlist with pafy?


I am trying to make a program that takes YT playlist and play all it's content.

I've installed all components needed for pafy to run with python3. Everything I've tried works as it's expected, except the bellow part of the code.

plurl = "https://www.youtube.com/playlist?list=PL634F2B56B8C346A2"
playlist = pafy.get_playlist(plurl)
url = playlist['items'][21]['pafy'].getbest().url
video = pafy.new(url)

When pafy.new() is called, gives an error because of too long url:

Need 11 character video id or the URL of the video. Got https://r2---sn-bavc5aoxu-nv4l.googlevideo.com/videoplayback?ms=au%2Crdu&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&mv=m&mt=1554899146&requiressl=yes&ip=37.157.173.53&pl=19&id=o-AGQZkyoEvykUGae7O4v_Ycmuj4jJBYdgafcfLBQ5S4Dd&mn=sn-bavc5aoxu-nv4l%2Csn-nv47lnsr&mm=31%2C29&source=youtube&lmt=1387649403290510&ei=POGtXJzdIo_ugAeEiL_wAQ&c=WEB&key=yt6&mime=video%2Fmp4&gir=yes&itag=18&clen=5461830&fvip=2&expire=1554920860&ratebypass=yes&dur=206.100&initcwndbps=1573750&ipbits=0&signature=AAA8B36CD3B402F587F874956595ACB928806C4F.D36C0A79E7F1727DB872425E696DBFC550AA7DF6 

Is there a way I can get normal url or video ID ?


Solution

  • The videoid is also available in the url object. You can use

    dir(<object>) 
    

    to see what properties are available.

    id = playlist['items'][2]['pafy'].videoid
    video = pafy.new('https://www.youtube.com/watch?v='+id)
    

    use try and catch before using pafy.new , as some of the videos might not be available in the region.