YouTube download using youtube-dl embedded with Python - 2020
youtube-dl is callable from any programming language. In this tutorial, we'll call it from our Python code.
youtube-dl's README.md shows us a brief introduction to embedding.
Also, have a look at my previous tutorial: Downloading YouTube, Vimeo etc Videos using youtube-dl.
The following code might be the simplest. It just download a YouTube video.
# ydl1.py from __future__ import unicode_literals import youtube_dl ydl_opts = {} with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])
Run it:
$ python ydl1.py [youtube] dP15zlyra3c: Downloading webpage [youtube] dP15zlyra3c: Downloading video info webpage [youtube] dP15zlyra3c: Extracting video information [youtube] dP15zlyra3c: Downloading DASH manifest [youtube] dP15zlyra3c: Downloading DASH manifest [download] Destination: Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f137.mp4 [download] 100% of 13.25MiB in 00:08 [download] Destination: Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f141.m4a [download] 100% of 1.78MiB in 00:04 [ffmpeg] Merging formats into "Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.mp4" Deleting original file Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f137.mp4 (pass -k to keep) Deleting original file Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f141.m4a (pass -k to keep)
The list of options is available at youtube_dl/YoutubeDL.py.
We'll use the following list of options:
- format: format code
- outtmpl: Template for output names.
- noplaylist: Download single video instead of a playlist if in doubt.
#ydl2.py from __future__ import unicode_literals import youtube_dl def my_hook(d): if d['status'] == 'finished': print('Done downloading, now converting ...') ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': '%(id)s', 'noplaylist' : True, 'progress_hooks': [my_hook], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(['https://www.youtube.com/watch?v=pwp1CH5R-w4'])
Run the code:
$ python ydl2.py [youtube] pwp1CH5R-w4: Downloading webpage [youtube] pwp1CH5R-w4: Downloading video info webpage [youtube] pwp1CH5R-w4: Extracting video information [youtube] pwp1CH5R-w4: Downloading DASH manifest [youtube] pwp1CH5R-w4: Downloading DASH manifest [download] Destination: pwp1CH5R-w4 [download] 100% of 10.82MiB in 00:13 Done downloading, now converting ... [ffmpeg] Correcting container in "pwp1CH5R-w4"
The file is stored with id (pwp1CH5R-w4) as its name. The format is mp4a.
How about getting information about a video?
Retrieving meta info is rather simple.
The code looks like this:
#ydl3.py from __future__ import unicode_literals import youtube_dl ydl_opts = {} with youtube_dl.YoutubeDL(ydl_opts) as ydl: meta = ydl.extract_info( 'https://www.youtube.com/watch?v=9bZkp7q19f0', download=False) print 'upload date : %s' %(meta['upload_date']) print 'uploader : %s' %(meta['uploader']) print 'views : %d' %(meta['view_count']) print 'likes : %d' %(meta['like_count']) print 'dislikes : %d' %(meta['dislike_count']) print 'id : %s' %(meta['id']) print 'format : %s' %(meta['format']) print 'duration : %s' %(meta['duration']) print 'title : %s' %(meta['title']) print 'description : %s' %(meta['description'])
Note that we're getting the meta info without downloading the video (download=False).
$ python ydl3.py [youtube] 9bZkp7q19f0: Downloading webpage [youtube] 9bZkp7q19f0: Downloading video info webpage [youtube] 9bZkp7q19f0: Extracting video information [youtube] 9bZkp7q19f0: Downloading DASH manifest upload date : 20120715 uploader : officialpsy views : 2496817588 likes : 10369540 dislikes : 1427375 id : 9bZkp7q19f0 format : 137 - 1920x1080 (DASH video)+141 - audio only (DASH audio) duration : 252 title : PSY - GANGNAM STYLE(강남스타일) M/V description : PSY - DADDY(feat. CL of 2NE1) M/V @ https://youtu.be/FrG4TEcSuRg PSY - 나팔바지(NAPAL BAJI) M/V @ https://youtu.be/tF27TNC_4pc PSY - 7TH ALBUM '칠집싸이다' on iTunes @ http://smarturl.it/PSY_7THALBUM PSY - GANGNAM STYLE(강남스타일) on iTunes @ http://smarturl.it/PsyGangnam #PSY #싸이 #GANGNAMSTYLE #강남스타일 More about PSY@ http://www.psypark.com/ http://www.youtube.com/officialpsy http://www.facebook.com/officialpsy http://twitter.com/psy_oppa http://iTunes.com/PSY http://sptfy.com/PSY http://weibo.com/psyoppa http://twitter.com/ygent_official
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization