【问题标题】:grabbing video title from yt-dlp command line output从 yt-dlp 命令行输出中获取视频标题
【发布时间】:2022-01-04 18:45:16
【问题描述】:
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 中制作的第一个项目的一部分。我缺少对许多概念的理解,任何帮助将不胜感激。

【问题讨论】:

    标签: python python-3.x youtube


    【解决方案1】:

    致谢:answer 提问: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
    

    【讨论】:

      猜你喜欢
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2010-11-16
      • 2011-05-21
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多