从 yt-dlp 命令行输出抓取视频标题
grabbing video title from yt-dlp command line output
from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
ydl.download('https://youtu.be/0KFSuoHEYm0')
这是生成输出的相关代码位。
我想做的是从下面的输出中获取倒数第二行,指定视频标题。
我尝试了一些变体
output = subprocess.getoutput(ydl)
以及
output = subprocess.Popen( ydl, stdout=subprocess.PIPE ).communicate()[0]
我试图捕获的输出是这里的倒数第二行:
[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
关于 yt-dlp 也有关于如何从元数据中提取标题或作为内容包含在 YoutubeDL() 后面的括号中的文档,但我不太明白。
这是我在 python 中制作的第一个项目的一部分。我缺少对许多概念的理解任何帮助将不胜感激。
致谢:answer to question: 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
这是输出:
#[youtube] 0KFSuoHEYm0: Downloading webpage
#[youtube] 0KFSuoHEYm0: Downloading android player API JSON
#Title: TJ Watt gets his 4th sack of the game vs. Browns
from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
ydl.download('https://youtu.be/0KFSuoHEYm0')
这是生成输出的相关代码位。
我想做的是从下面的输出中获取倒数第二行,指定视频标题。
我尝试了一些变体
output = subprocess.getoutput(ydl)
以及
output = subprocess.Popen( ydl, stdout=subprocess.PIPE ).communicate()[0]
我试图捕获的输出是这里的倒数第二行:
[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
关于 yt-dlp 也有关于如何从元数据中提取标题或作为内容包含在 YoutubeDL() 后面的括号中的文档,但我不太明白。
这是我在 python 中制作的第一个项目的一部分。我缺少对许多概念的理解任何帮助将不胜感激。
致谢:answer to question: 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
这是输出:
#[youtube] 0KFSuoHEYm0: Downloading webpage
#[youtube] 0KFSuoHEYm0: Downloading android player API JSON
#Title: TJ Watt gets his 4th sack of the game vs. Browns