YouTube obtain utilizing youtube-dl embedded with Python

codecs logo
January 22, 2021 0 Comments

YouTube obtain utilizing youtube-dl embedded with Python – 2020

codecs logo

Bookmark and Share


bogotobogo.com website search:


Embedding youtube-dl

youtube-dl is callable from any programming language. On this tutorial, we’ll name it from our Python code.

youtube-dl‘s README.md reveals us a quick introduction to embedding.

Additionally, take a look at my earlier tutorial: Downloading YouTube, Vimeo and so forth Movies utilizing youtube-dl.

Our first embedded code

The next code is likely to be the only. It simply obtain a YouTube video.

# ydl1.py
from __future__ import unicode_literals
import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.obtain(['https://www.youtube.com/watch?v=dP15zlyra3c'])

Run it:

$ python ydl1.py
[youtube] dP15zlyra3c: Downloading webpage
[youtube] dP15zlyra3c: Downloading video information webpage
[youtube] dP15zlyra3c: Extracting video info
[youtube] dP15zlyra3c: Downloading DASH manifest
[youtube] dP15zlyra3c: Downloading DASH manifest
[download] Vacation spot: Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f137.mp4
[download] 100% of 13.25MiB in 00:08
[download] Vacation spot: Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f141.m4a
[download] 100% of 1.78MiB in 00:04
[ffmpeg] Merging codecs into "Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.mp4"
Deleting unique file Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f137.mp4 (cross -k to maintain)
Deleting unique file Fox Snow Dive - Yellowstone - BBC Two-dP15zlyra3c.f141.m4a (cross -k to maintain)

Utilizing choices

The record of choices is obtainable at youtube_dl/YoutubeDL.py.

We’ll use the next record of choices:

  1. format: format code
  2. outtmpl: Template for output names.
  3. noplaylist: Obtain single video as a substitute of a playlist if unsure.
#ydl2.py
from __future__ import unicode_literals
import youtube_dl

def my_hook(d):
    if d['status'] == 'completed':
        print('Carried out downloading, now changing ...')

ydl_opts = {
    'format': 'bestaudio/finest',       
    'outtmpl': '%(id)s',        
    'noplaylist' : True,        
    'progress_hooks': [my_hook],  
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.obtain(['https://www.youtube.com/watch?v=pwp1CH5R-w4'])

Run the code:

$ python ydl2.py
[youtube] pwp1CH5R-w4: Downloading webpage
[youtube] pwp1CH5R-w4: Downloading video information webpage
[youtube] pwp1CH5R-w4: Extracting video info
[youtube] pwp1CH5R-w4: Downloading DASH manifest
[youtube] pwp1CH5R-w4: Downloading DASH manifest
[download] Vacation spot: pwp1CH5R-w4
[download] 100% of 10.82MiB in 00:13
Carried out downloading, now changing ...
[ffmpeg] Correcting container in "pwp1CH5R-w4"

The file is saved with id (pwp1CH5R-w4) as its title. The format is mp4a.

Getting meta info

How about getting details about a video?

Retrieving meta information is relatively easy.

The code seems to be 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', obtain=False) 

print 'add 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 'period    : %s' %(meta['duration'])
print 'title       : %s' %(meta['title'])
print 'description : %s' %(meta['description'])

Observe that we’re getting the meta information with out downloading the video (obtain=False).

$ python ydl3.py
[youtube] 9bZkp7q19f0: Downloading webpage
[youtube] 9bZkp7q19f0: Downloading video information webpage
[youtube] 9bZkp7q19f0: Extracting video info
[youtube] 9bZkp7q19f0: Downloading DASH manifest
add date : 20120715
uploader    : officialpsy
views       : 2496817588
likes       : 10369540
dislikes    : 1427375
id          : 9bZkp7q19f0
format      : 137 - 1920x1080 (DASH video)+141 - audio solely (DASH audio)
period    : 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 #강남스타일

Extra about [email protected]
http://www.psypark.com/
http://www.youtube.com/officialpsy
http://www.fb.com/officialpsy

http://iTunes.com/PSY
http://sptfy.com/PSY
http://weibo.com/psyoppa

Leave a Reply

Your email address will not be published. Required fields are marked *