Package: kafy
FeaturesRetreive metadata such as viewcount, duration, rating, author, thumbnail, keywordsDownload video or audio at requested resolution / bitrate / format / filesizeCommand line tool (ytdl) for downloading directly from the command lineRetrieve the URL to stream the video in a player such as vlc or mplayerWorks with age-restricted videos and non-embeddable videosSmall, standalone, single importable module file (kafy.py)Select highest quality stream for download or streamingDownload video only (no audio) in m4v or webm formatDownload audio only (no video) in ogg or m4a formatRetreive playlists and playlist metadataWorks with Python 2.6+ and 3.3+Optionally depends on youtube-dl (recommended; more stable)Usage ExamplesHere is how to use the module in your own python code.  For command line tool
(ytdl) instructions, see further below>>>importkafycreate a video instance from a YouTube url:>>>url="https://www.youtube.com/watch?v=9AUu6Ddq78Y">>>video=kafy.new(url)get certain attributes:>>>video.title'Python for Beginners - Learn Python in 1 Hour'>>>video.rating5.0>>>video.viewcount,video.author,video.length(2557209, 'Programming with Mosh', 10394)>>>video.duration,video.likes,video.dislikes('02:53:14', 25, 0)>>>print(video.description)Speaker: Python for Beginnerslist available streams for a video:>>>streams=video.streams>>>forsinstreams:...print(s)...normal:mp4@1280x720
normal:webm@640x360
normal:mp4@640x360
normal:flv@320x240
normal:3gp@320x240
normal:3gp@176x144show all formats, file-sizes and their download url:>>>forsinstreams:...print(s.resolution,s.extension,s.get_filesize(),s.url)...1280x720 mp4 2421958510 https://r1---sn-aiglln7e.googlevideo.com/videoplayba[...]
640x360 webm 547015732 https://r1---sn-aiglln7e.googlevideo.com/videoplaybac[...]
640x360 mp4 470655850 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
320x240 flv 345455674 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
320x240 3gp 208603447 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
176x144 3gp 60905732 https://r1---sn-aiglln7e.googlevideo.com/videoplayback?[...]get best resolution regardless of file format:>>>best=video.getbest()>>>best.resolution,best.extension('1280x720', 'mp4')get best resolution for a particular file format:
(mp4, webm, flv or 3gp)>>>best=video.getbest(preftype="webm")>>>best.resolution,best.extension('640x360', 'webm')get url, for download or streaming in mplayer / vlc etc:>>>best.url'http://r12---sn-aig7kner.c.youtube.com/videoplayback?expire=1369...Download video and show progress:>>>best.download(quiet=False)3,734,976 Bytes [0.20%] received. Rate: [ 719 KB/s].  ETA: [3284 secs]Download video, use specific directory and/or filename:>>>filename=best.download(filepath="/tmp/")>>>filename=best.download(filepath="/tmp/Game."+best.extension)Get audio-only streams (m4a and/or ogg vorbis):>>>audiostreams=video.audiostreams>>>forainaudiostreams:...print(a.bitrate,a.extension,a.get_filesize())...256k m4a 331379079
192k ogg 172524223
128k m4a 166863001
128k ogg 108981120
48k m4a 62700449Download the 2nd audio stream from the above list:>>>audiostreams[1].download()Get the best quality audio stream:>>>bestaudio=video.getbestaudio()>>>bestaudio.bitrate'256'Download the best quality audio file:>>>bestaudio.download()show all media types for a video (video+audio, video-only and audio-only):>>>allstreams=video.allstreams>>>forsinallstreams:...print(s.mediatype,s.extension,s.quality)...normal mp4 1280x720
normal webm 640x360
normal mp4 640x360
normal flv 320x240
normal 3gp 320x240
normal 3gp 176x144
video m4v 1280x720
video webm 1280x720
video m4v 854x480
video webm 854x480
video m4v 640x360
video webm 640x360
video m4v 426x240
video webm 426x240
video m4v 256x144
video webm 256x144
audio m4a 256k
audio ogg 192k
audio m4a 128k
audio ogg 128k
audio m4a 48kInstallationkafy can be installed usingpip:$[sudo]pipinstallkafyor use avirtualenvif you don’t want to install it system-wide:$virtualenvvenv$sourcevenv/bin/activate$pipinstallkafyCommand Line Tool (ytdl) Usageusage:ytdl[-h][-i][-s][-t{audio,video,normal,all}[{audio,video,normal,all}...]][-nN][-b][-a]urlYouTubeDownloadToolpositionalarguments:urlYouTubevideoURLtodownloadoptionalarguments:-h,--helpshowthishelpmessageandexit-iDisplayvidinfo-sDisplayavailablestreams-t{audio,video,normal,all}[{audio,video,normal,all}...]Streamtypestodisplay-nNSpecifystreamtodownloadbystreamnumber(use-stolistavailablestreams)-bDownloadthebestqualityvideo(ignores-n)-aDownloadthebestqualityaudio(ignores-n)ytdl ExamplesDownload best available resolution (-b):$ytdl-b"https://www.youtube.com/watch?v=9AUu6Ddq78Y"Download best available audio stream (-a)
(note; the full url is not required, just the video id will suffice):$ytdl-acyMHZVT91Dwget video info (-i):$ytdl-icyMHZVT91Dwlist available dowload streams:$ytdlcyMHZVT91DwStreamTypeFormatQualitySize---------------------------1normalwebm[640x360]33MB2normalmp4[640x360]23MB3normalflv[320x240]14MB4normal3gp[320x240]9MB5normal3gp[176x144]3MB6audiom4a[48k]2MB7audiom4a[128k]5MB8audioogg[128k]5MB9audioogg[192k]7MB10audiom4a[256k]10MBDownload mp4 640x360 (ie. stream number 2):$ytdl-n2cyMHZVT91DwDownload m4a audio stream at 256k bitrate:$ytdl-n10cyMHZVT91Dw