我在 Python 程序中使用 youtube-dl 作为模块,而不是从命令行。它工作得很好,除了当我下载(具有讽刺意味的)一些 YouTube 视频时——特别是那些音频被编码为与视频分开的文件的视频。基本上,我需要最终文件的名称,但是我在重新混合之前获得了两个文件的名称。根据 youtube-dl 文档,您可以使用钩子获取最终文件名:
文件名:最终文件名(始终存在)
tmpfilename:我们当前正在写入的文件名
似乎“文件名”应该让我知道从下载和组合的音频和视频创建的文件的名称。但它不这样做。
这是我的代码:
def my_hook(d):
if d['status'] == 'finished':
print "\nYour video has been downloaded!"
filelocation = d['filename']
print filelocation
def download_video(url, archival_location, asset):
global mountpoint
# Put Location and filename to save in a variable
destination = archival_location + asset + ".%(ext)s"
# options for youtubedl
ydl_opts = {
'logger' : MyLogger(),
'progress_hooks': [my_hook],
'outtmpl' : destination
}
try:
# Lets download it now
with YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print ""
except:
print "Error with downloader. Most likely Unsupported URL"
#check and see if we mounted a drive
if mounted:
unmount(mountpoint)
# Exit the program
exit()
该程序还有更多内容,但这是相关的部分。这是从 YouTube 下载视频时的输出:
您的视频已下载!/Volumes/RR261-GOING-POSTAL_1/RR261_Archival_Footage/RR261_A999_Erase_Please.f243.webm
您的视频已下载!/Volumes/RR261-GOING-POSTAL_1/RR261_Archival_Footage/RR261_A999_Erase_Please.f141.m4a
现在我的程序将失败,因为变量filelocation
指向 m4a 文件,一旦 ffmpeg 执行它的操作,该文件就会消失。如何让 youtube-dl 报告最终文件?还是我以错误的方式解决这个问题?是否有另一种方法来获取 ffmpeg 创建的路径和文件名?我的程序不调用 ffmpeg,而 youtube-dl 模块调用了,而且我从标准输出看不到它在做什么。