2
我試圖基本上得到什麼似乎是一個狀態對象的Youtube-DL中掛鉤的信息,然後我試圖保存它到數據庫。我有一個包含「文件名」等屬性的「歌曲」對象,我試圖在下載完成後保存,甚至可能會不斷更新數據庫並取得進展。YoutubeDL - 如何獲得下載完成後的狀態對象
有四種方法我能想到的要做到這一點,但我沒有能夠讓他們的工作
- 發送my_hook功能的數據庫和歌曲對象,然後將它保存所有在那裏,一旦狀態==完成。問題是我無法將其他參數傳遞給鉤子,除非我失去了一些東西
- 獲取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])