有没有办法在从 youtube-dl 登录时包含从 rich 下载进度条?

问题描述 投票:0回答:1

我正在制作一个脚本,从文本文件中批量下载 youtube 视频,并希望通过丰富的内容来增加控制台日志记录的趣味性。然而,虽然我的系统确实使用 rich 进行输出,但它没有下载进度条。

这是代码的当前状态:

from __future__ import unicode_literals
from rich.logging import RichHandler
import logging
import youtube_dl

logging.basicConfig(level=logging.DEBUG,   format="%(message)s",  datefmt="[%X]",
    handlers=[RichHandler(level=logging.DEBUG, rich_tracebacks=True, markup=True ) ]
) 
log = logging.getLogger('rich')
def load(id): 
    address = 'http://www.youtube.com/watch?v=' + id
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',
            'preferredquality': '192'
        }],
        'outtmpl': 'audio/%(id)s.wav',
        "logger": log,
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([address])

我想知道是否有办法将日志记录的下载部分替换为某种进度条。

python logging youtube-dl rich
1个回答
0
投票

当然有办法,但是有点棘手。请参阅此讨论: 如何使用 youtube-dl 仅显示进度信息?

以下代码在下载音频时使用丰富的栏:

from __future__ import unicode_literals
import youtube_dl
from rich.progress import track

bar = False 
barvalues = []  
def updatebar(d, finish=False):
    ## rich rogress bar
    if finish:
       100 in bar # you need to finish 'manually'
       # Yeah new song!
    elif bar:
        p = int(round( float(d['downloaded_bytes']) / float(d['total_bytes']) *100))
        if p in range(100) and (not p in barvalues):
            barvalues.append(p) # prevent repeatin' otherwise fails
            int(p) in bar # display progress
    else:
        print("Bar not declared")

def hook(d):
    if d['status'] == 'finished':
        global bar
        updatebar(d, True)
        bar = False
        print("Converting ..")
    elif d['status'] == 'downloading':
        updatebar(d)

opts = {
    'cachedir': False, # avoid http 403: forbidden (sometimes anyway happen)
    'rm_cachedir': True, # avoid http 403: forbidden
    'outtmpl': 'mypath/%(title)s.%(ext)s',
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': '{}'.format("mp3"),
        'preferredquality': '{}'.format("320"),
    }],
    'progress_hooks': [hook]
}

def download(src):
    global bar
    bar = track(range(100), description="")
    with youtube_dl.YoutubeDL(opts) as ytdl:
        title = ytdl.extract_info(src, download=False).get('title', None)
        print("\nDownloading .. " + title)
        ytdl.cache.remove() # Important! avoid http 403: forbidden
        ytdl.download([src])
© www.soinside.com 2019 - 2024. All rights reserved.