Kei*_*lly 5 python youtube youtube-dl
我在 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()
Run Code Online (Sandbox Code Playgroud)
该程序还有更多内容,但这是相关的部分。以下是从 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 模块调用,而且我没有从标准输出看到它在做什么。
| 归档时间: |
|
| 查看次数: |
1571 次 |
| 最近记录: |