提取 YouTube 视频的标题 Python
Extract the title of a YouTube video Python
我在 python 中编写了一小段代码来从 YouTube 视频中提取音频。这是代码:
from __future__ import unicode_literals
import youtube_dl
link = input("Enter the video link:")
name = input("Enter the title of the video:")
path = f'D:\{name}.mp3'
ydl_opts = {
'outtmpl':path,
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
我想在这里做的是使用 link 获取视频的标题,而不是向用户询问视频的标题。我尝试了几种方法,但它们对我没有用。是否有捷径可寻?谢谢
您可以通过以下方式使用extract_info方法:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
video_title = info_dict.get('title', None)
我在 python 中编写了一小段代码来从 YouTube 视频中提取音频。这是代码:
from __future__ import unicode_literals
import youtube_dl
link = input("Enter the video link:")
name = input("Enter the title of the video:")
path = f'D:\{name}.mp3'
ydl_opts = {
'outtmpl':path,
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
我想在这里做的是使用 link 获取视频的标题,而不是向用户询问视频的标题。我尝试了几种方法,但它们对我没有用。是否有捷径可寻?谢谢
您可以通过以下方式使用extract_info方法:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
video_title = info_dict.get('title', None)