ZG4921
ZG4921

Reputation: 115

grabbing video title from yt-dlp command line output

from yt_dlp import YoutubeDL

with YoutubeDL() as ydl:
    ydl.download('https://youtu.be/0KFSuoHEYm0')

this is the relevant bit of code producing the output.

what I would like to do is grab the 2nd last line from the output below, specifying the video title.

I have tried a few variations of

output = subprocess.getoutput(ydl)

as well as

output = subprocess.Popen( ydl, stdout=subprocess.PIPE ).communicate()[0]

the output I am attempting to capture is the 2nd last line here:

[youtube] 0KFSuoHEYm0: Downloading webpage
[youtube] 0KFSuoHEYm0: Downloading android player API JSON
[info] 0KFSuoHEYm0: Downloading 1 format(s): 22
[download] Destination: TJ Watt gets his 4th sack of the game vs. Browns [0KFSuoHEYm0].mp4
[download] 100% of 13.10MiB in 00:01 

There is also documentation on yt-dlp on how to pull title from metadata or include as something in the brackets behind YoutubeDL(), but I can not quite figure it out.

This is part of the first project I am making in python. I am missing an understanding of many concepts any help would be much appreciated.

Upvotes: 9

Views: 15625

Answers (1)

Credits: answer to question: How to get information from youtube-dl in python ??


Modify your code as follows:

from yt_dlp import YoutubeDL

with YoutubeDL() as ydl: 
  info_dict = ydl.extract_info('https://youtu.be/0KFSuoHEYm0', download=False)
  video_url = info_dict.get("url", None)
  video_id = info_dict.get("id", None)
  video_title = info_dict.get('title', None)
  print("Title: " + video_title) # <= Here, you got the video title

This is the output:

#[youtube] 0KFSuoHEYm0: Downloading webpage
#[youtube] 0KFSuoHEYm0: Downloading android player API JSON
#Title: TJ Watt gets his 4th sack of the game vs. Browns

Upvotes: 16

Related Questions