【问题标题】:YoutubeDL - How to get a status object after download has completedYoutubeDL - 下载完成后如何获取状态对象
【发布时间】:2016-01-08 03:38:24
【问题描述】:

我基本上是在尝试从似乎是在 Youtube-DL 中的钩子的状态对象中获取信息,然后我试图将其保存到数据库中。我有一个带有“文件名”等属性的“歌曲”对​​象,一旦下载完成,我会尝试保存,甚至可能会不断更新数据库。

我可以想到四种方法来做到这一点,但我无法让它们发挥作用

  • 向 my_hook 函数发送一个 db 和 song 对象,然后在 status == 完成后将其全部保存在那里。问题是我无法将其他参数传递给钩子,除非我遗漏了什么
  • 获取 my_hook 函数以返回 d 然后保存,问题是我认为我无法访问它会返回(youtube-dl 源)
  • 获取 ydl.download([song.url]) 以返回我可以处理的状态对象,但我认为它不会这样做
  • 我不想这样做,但我可以输出一个 .json 文件并从那里获取它,或者只是猜测文件的名称,因为我最初是在口述它:(

代码如下:

def my_hook(d):
    if d['status'] == 'finished':
        file_tuple = os.path.split(os.path.abspath(d['filename']))
        print("Done downloading {}".format(file_tuple[1]))
    if d['status'] == 'downloading':
        print(d['filename'], d['_percent_str'], d['_eta_str'])

class MyLogger(object):
    def debug(self, msg):
        pass

    def warning(self, msg):
        pass

    def error(self, msg):
        print(msg)


class Downloader(object):
    def get_opts(self):
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': os.path.join(app.config['VIDEOS_FOLDER'], '%(id)s.%(ext)s'),
            'logger': MyLogger(),
            'progress_hooks': [my_hook],
        }
        return ydl_opts

    def download(self, song):
        ydl = youtube_dl.YoutubeDL(self.get_opts())
        ydl.download([song.url])

【问题讨论】:

    标签: python python-3.x youtube-dl


    【解决方案1】:

    我在这里得到了答案:https://github.com/rg3/youtube-dl/issues/7120

    基本上我的歌曲文件到歌曲请求的一对多模型是错误的 - 重写这种关系允许我使用挂钩来更新数据库。

    【讨论】:

      猜你喜欢
      • 2013-10-25
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多