Download youtube video to user machine on Django website

Question:

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('../../')

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

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.

Thanks in advance.

Asked By: Georgi Dimitrov

||

Answers:

I also issued Same Problem but i reduce this problem 80 percent…
So, See the my solution i hope your help this solution 🙂

first Import Module

from django.http import FileResponse

Then Code…

return FileResponse(open(YouTube(url).streams.first().download(skip_existing=True),'rb'))

You can not use return redirect function, you can use FileResponse function

Answered By: Gagan Aggarwal

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.

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

A solution

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

  • 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

Why would this work?

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

  • It receives a request from the user
  • It then returns a response

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.

How can I choose the download location?

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.

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

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.

Answered By: Geekfish

I was struggling with this one too. I read what others added here, but that did not work for me.

Then I found the missing piece at this website. In order to download the file, you have to include as_attachment=Ture to the FileResponse.

So, The final answer would be as follow:

return FileResponse(open(youtube.download(skip_existing=True),'rb'), as_attachment=True)

This will allow the file to get downloaded at the "Downloads" directory on the user’s device; not on your pythonanywhere server.

NOTE: the user will stay on this page even after downloading the file.

Answered By: bakursait