gpt4 book ai didi

python - 如何使用 youtube-dl 只显示进度信息?

转载 作者:行者123 更新时间:2023-12-05 05:00:38 26 4
gpt4 key购买 nike

我正在用 python 制作一个脚本,从一个页面下载一些视频,还有一些 Youtube 视频,所以我使用 youtube-dl 来下载它们。

问题是终端的所有信息都太脏了,我想隐藏它们。隐藏一切,保存路径,当前状态,只保留进度信息。如果我将安静模式设置为True,则不会显示进度信息。

我的代码:

def ytbDown(path, name, url):
ydl_opts = {
'outtmpl': path + name,
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'quiet': False,
'warnings': 'no-warnings'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])

如果我使用该配置运行脚本,终端将以这种方式出现:

如果我将安静模式设置为 True,则不会出现任何内容,但我不想这样。我想保留进度状态(下载百分比、下载速度等)

有办法吗?感谢所有帮助,我正在使用 Python 3 和 Windows 7

最佳答案

from __future__ import unicode_literals
import youtube_dl

def my_hook(d):
if d['status'] == 'downloading':
print ("downloading "+ str(round(float(d['downloaded_bytes'])/float(d['total_bytes'])*100,1))+"%")
if d['status'] == 'finished':
filename=d['filename']
print(filename)

ydl_opts = {
'format': 'bestvideo[width<=1080]+bestaudio/best',
'quiet': True,
'no_warnings': True,
'progress_hooks': [my_hook]
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(sys.argv[1:])

可以用 ["www.youtube.com/....."] 替换 sys.argv[1:],包括方括号

引用关于嵌入的 youtube-dl 文档

https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl

关于python - 如何使用 youtube-dl 只显示进度信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63025605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com