【问题标题】:Extract the title of a YouTube video Python提取 YouTube 视频的标题 Python
【发布时间】:2020-09-16 08:29:03
【问题描述】:

我用 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])

我在这里想要做的是通过使用链接而不是询问用户视频的标题来获取视频的标题。我尝试了几种方法,但它们对我不起作用。是否有捷径可寻?谢谢。

【问题讨论】:

    标签: python youtube extract screen-scraping youtube-dl


    【解决方案1】:

    您可以按以下方式使用 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)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2015-08-09
    • 2017-10-14
    • 1970-01-01
    • 2015-10-23
    • 2010-11-16
    相关资源
    最近更新 更多