我有以下代码下载.mp4文件,但它花了每个视频太长的时间。这与我的互联网没有任何关系,因为wget像一把尖刀一样挤过它,而下面的代码像一头老母牛一样笨拙地向前走。
我认为代码是实时查看视频并将其写入计算机,对吗?有没有其他方法可以下载整个视频,或者通过任何其他模块加快下载速度?
def download_file(link,temp):
r = requests.get(link,stream=True)
folder_name = r"K:\Archive\Videos"
existing_files = [file.replace(".mp4","") for file in os.listdir(folder_name)]
if temp not in existing_files:
with open(f"{folder_name}/{temp}.mp4","wb") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)浏览一下代码,我开始认为可能是检查文件是否存在的部分占用了时间。我在文件夹中有400,000个文件,所以可能这种检查机制效率不高?
发布于 2020-11-14 14:30:41
我认为你的评估是正确的。枚举400,000个文件需要花费大量时间。因此,您应该将其缓存在全局缓存中。
existing_files = None
def download_files(link,temp):
global existing_files
if not existing_files:
existing_files = [file.replace(... etchttps://stackoverflow.com/questions/64831283
复制相似问题