How can I know the title of a Youtube video while downloading?

Asked

Viewed 82 times

1

I’m doing a show that downloads Youtube videos from a channel. But I don’t want to download all the videos, but only those that have a specific word. How can I do this?

import youtube_dl
import os

ydl_opts = {}
os.chdir(os.getcwd())
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                           
      ydl.download(['https://www.youtube.com/channel/UCVJqulBlIchaIsGFK-hCKFQ'])

1 answer

2


The solution is to use ydl.extract_info:

import youtube_dl

ydl = youtube_dl.YoutubeDL()
result = ydl.extract_info('https://www.youtube.com/channel/UCVJqulBlIchaIsGFK-hCKFQ', download=False)

for i in range(len(result['entries'][0]['entries'])):
      print(result['entries'][0]['entries'][i]['title'])

Returns:

Eletrônicos São Divertidos
Galinhas verdes soltam laser
Magicas1
Minecraft parte 2
Artes #1
bonéco de neve aspen
snow mess aspen
can s top filling
Dudu versus Treinador no Clash Royale!
Dudu Contra Barça no Fifa 15!
Dudu Detona no Fifa 15 (Parte 2)
Dudu Detona no Fifa 15 (Parte 1)

To properly use this method it is important to understand the structure of the object result. It is a dictionary in which the keys are the linked object data (video, playlist, channel). In the present case, the keys of result sane:

dict_keys(['_type', 'entries', 'id', 'title', 'description', 'extractor', 'webpage_url', 'webpage_url_basename', 'extractor_key'])

Note that entries is a key that contains the channel’s video list. For each video within the channel there is another dictionary with the video information. See an example using the first video:

print(result['entries'][0].keys())

Returns:

dict_keys(['_type', 'entries', 'id', 'title', 'description', 'extractor', 'webpage_url', 'webpage_url_basename', 'extractor_key'])

Now it is easy to make the conditional download to the name. Just add:

for i in range(len(result['entries'][0]['entries'])):
      if 'Eletrônicos' in result['entries'][0]['entries'][i]['title']:
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                  ydl.download([result['entries'][0]['entries'][i]['webpage_url']])
      else:
            print("I will not download {}".format(result['entries'][0]['entries'][i]['title']))
  • Thank you Lucas worked

  • Imagine. If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.