如何在 Heroku 上查找下载文件的位置
How to find location of downloaded files on Heroku
我正在使用 YouTube-dl 与 Python 和 Flask 下载 youtube 视频,并 return 使用 send_file
功能。
当运行在本地我一直使用获取文件路径:
username = getpass.getuser()
directories = os.listdir(rf'C:\Users\{username}')
然后我用 YouTube-dl 下载视频:
youtube_dl.YoutubeDL().download([link])
然后我根据视频代码在目录中搜索文件:
files = [file for file in directories]
code = link.split('v=')[1]
for file in files:
if file.endswith('.mp4') is True:
try:
code_section = file.split('-')[1].split('.mp4')[0]
if code in code_section:
return send_file(rf'C:\Users\{username}\{file}')
except:
continue
最后,我return文件:
return send_file(rf'C:\Users\{username}\{file}')
找到下载文件的位置,但是,在 Heroku 上,这不起作用 - 只是目录不存在。我如何找到文件的下载位置?有我可以调用的功能吗?或者它有固定的路径吗?
或者,是否可以使用 YouTube-dl 设置下载位置?
由于 heroku 是 运行 Linux 而不是 windows,您可以尝试将文件下载到当前工作目录,然后从那里发送它。
主要调整是在 YoutubeDL 应用中设置一些选项:
import os
opts = {
"outtmpl": f"{os.getcwd()}/(title)s.(ext)s"
}
youtube_dl.YoutubeDL(opts).download([link])
这会将文件下载到您当前的工作目录。
然后你可以使用 return send_file(file)
.
从你的工作目录上传它
我正在使用 YouTube-dl 与 Python 和 Flask 下载 youtube 视频,并 return 使用 send_file
功能。
当运行在本地我一直使用获取文件路径:
username = getpass.getuser()
directories = os.listdir(rf'C:\Users\{username}')
然后我用 YouTube-dl 下载视频:
youtube_dl.YoutubeDL().download([link])
然后我根据视频代码在目录中搜索文件:
files = [file for file in directories]
code = link.split('v=')[1]
for file in files:
if file.endswith('.mp4') is True:
try:
code_section = file.split('-')[1].split('.mp4')[0]
if code in code_section:
return send_file(rf'C:\Users\{username}\{file}')
except:
continue
最后,我return文件:
return send_file(rf'C:\Users\{username}\{file}')
找到下载文件的位置,但是,在 Heroku 上,这不起作用 - 只是目录不存在。我如何找到文件的下载位置?有我可以调用的功能吗?或者它有固定的路径吗?
或者,是否可以使用 YouTube-dl 设置下载位置?
由于 heroku 是 运行 Linux 而不是 windows,您可以尝试将文件下载到当前工作目录,然后从那里发送它。
主要调整是在 YoutubeDL 应用中设置一些选项:
import os
opts = {
"outtmpl": f"{os.getcwd()}/(title)s.(ext)s"
}
youtube_dl.YoutubeDL(opts).download([link])
这会将文件下载到您当前的工作目录。
然后你可以使用 return send_file(file)
.