在 Django 网站上将 youtube 视频下载到用户机器 [英] Download youtube video to user machine on Django website

查看:46
本文介绍了在 Django 网站上将 youtube 视频下载到用户机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与问题所说的差不多.现在我使用 pytube 有这个功能:

Pretty much what the question says. Right now I have this function using pytube:

    def download(request, video_url):
        url='http://www.youtube.com/watch?v='+str(video_url)
        homedir = os.path.expanduser("~")
        dirs = homedir + '/Downloads'
        download = YouTube(url).streams.first().download(dirs)   
        return redirect('../../')

但这会将视频下载到 pythonanywhere 中的目录 /Downloads/ - 我已将项目上传到的站点.

But this downloads the video to a directory /Downloads/ in pythonanywhere - the site that I have uploaded my project to.

如何将视频下载到用户的机器上?解决方案不必使用 pytube 包,因为我只想要一个答案.如果您也告诉我如何将文件下载为 .mp3 文件,那就太好了.

How do I download the video to the user's machine? The solution doesn't have to be using the pytube package, as I just want an answer. It will be great if you tell me how to download the file as an .mp3 file too.

提前致谢.

推荐答案

问题

当您在远程计算机(这是您在 pythonanywhere 的服务器)上运行程序时,所有操作都发生在该远程计算机上,而不是用户的机器上.

The issue

When you are running a program on a remote computer (this is your server at pythonanywhere) then all operations happen at that remote computer, not the user's machine.

因此,当您在远程计算机上下载某些内容时,它会下载到远程计算机(pythonanywhere).

Therefor, when you download something on a remote computer, it will be downloaded to the remote computer (pythonanywhere).

为了满足您的要求(将文件下载到用户的计算机,而不是服务器),您可能会执行以下操作:

To fulfil your requirements (download a file to the user's computer, not the server), you would probably do something like this:

  • 从本地 youtube 下载文件(就像您目前使用 YouTube....download() 所做的那样.
  • 将文件发送给用户.这就是 Gagan Aggarwal 的解决方案试图用 django 的 FileResponse
  • Download the file from youtube locally (as you currently do with YouTube....download().
  • Send the file to the user. This is what Gagan Aggarwal's solution is attempting to do with django's FileResponse

每次在浏览器中访问页面时,django 应用程序大致会执行以下操作:

Every time you visit your page in your browser, your django application does roughly the following:

  • 它收到来自用户的请求
  • 然后返回一个响应

通常 Django 应用程序会返回一个 HTML 页面.但是 HTML 页面只是一个文件,您正在下载的 Youtube 视频也是.然后您可以选择发回视频文件而不是网页.然后用户的浏览器将尝试打开该文件.

Usually a Django application will return an HTML page. But an HTML page is just a file, and so is your Youtube video that you are downloading. You can choose then to send back the video file instead of a web page. The user's browser will then try to open that file.

您的 Django 应用程序无法选择用户计算机上的下载位置.如果任何网站可以在任何位置下载您计算机上的文件,这将是一件危险的事情.

Your Django application cannot choose the download location on the user's computer. This would be a dangerous thing, if any website could just download files on your computer on any location.

但是,当您将文件发送给用户时,他们可以选择下载位置.

However, when you send the file to the user, they can choose where to download it.

如果您查看 FileResponse 的文档,你会看到你可以传递一个 as_attachment=True 参数.

If you look at the documentation for FileResponse, you will see that you can pass an as_attachment=True parameter.

这将使 Django 将文件作为附件"发送,这意味着它将尝试下载它而不是打开它.这将提示用户选择文件的下载位置.

This will make Django send a file as an "attachment", meaning that it will try to download it and not open it. This will prompt the user to choose the download location of the file.

这篇关于在 Django 网站上将 youtube 视频下载到用户机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆