ZG4*_*921 6 python youtube python-3.x
from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
ydl.download('https://youtu.be/0KFSuoHEYm0')
Run Code Online (Sandbox Code Playgroud)
这是产生输出的相关代码位。
我想做的是从下面的输出中获取倒数第二行,指定视频标题。
我尝试过一些变体
output = subprocess.getoutput(ydl)
Run Code Online (Sandbox Code Playgroud)
也
output = subprocess.Popen( ydl, stdout=subprocess.PIPE ).communicate()[0]
Run Code Online (Sandbox Code Playgroud)
我试图捕获的输出是这里的倒数第二行:
from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
ydl.download('https://youtu.be/0KFSuoHEYm0')
Run Code Online (Sandbox Code Playgroud)
yt-dlp 上还有关于如何从元数据中提取标题或将其包含在 YoutubeDL() 后面的括号中的文档,但我不太明白。
这是我用 python 制作的第一个项目的一部分。我缺少对许多概念的理解,任何帮助将不胜感激。
Mar*_*yes 11
学分:回答问题:How to get information from youtube-dl in python ??
修改您的代码如下:
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
Run Code Online (Sandbox Code Playgroud)
这是输出:
#[youtube] 0KFSuoHEYm0: Downloading webpage
#[youtube] 0KFSuoHEYm0: Downloading android player API JSON
#Title: TJ Watt gets his 4th sack of the game vs. Browns
Run Code Online (Sandbox Code Playgroud)