I have made a youtube video downloader app with django and pafy.
The problem in the final link which I am getting is something like https://r5---sn-gwpa-jj0z-blah-blah.
These links are opening files in the browser how to make them downloadable on clients machine?
The Template I am using to get the download link is,
{% for resolution,extension,file_size,url in streams %}
<tr>
<td>{{ resolution }}</td>
<td>{{ file_size }}</td>
<td>{{ extension }}</td>
<td>
<a href="{{ url }}" download="{{ title }}.{{ extension }}" type="application/octet-stream" >
<span class="glyphicon glyphicon-download-alt" ></span> Download
</a>
</td>
</tr>
{% endfor %}
Is it possible to get the direct links from youtube-dl, I did a lot of searching but didn't found a valid answer nothing worked for me?
Update
Views.py
import pafy
from django.http import HttpResponse
from django.shortcuts import render
from django.template.defaultfilters import filesizeformat
from django.views import generic
def url_corrector(url):
correct_url = url
if 'm.' in url:
correct_url = correct_url.replace('m.', '')
if 'youtu.be' in url:
video_id = url.split('/')[-1]
correct_url = 'https://www.youtube.com/watch?v=' + video_id
return correct_url
class Download(generic.View):
# """The view which handles the url and returns the download links."""
template_name = 'download.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self, request, *args, **kwargs):
url = request.POST['url']
url = url_corrector(url) # Converting the urls in a format that is supported by pafy.
# if len(url.split("=")[-1]) != 11: # Checks whether the url contains the 11 character unique video id.
# return HttpResponse('Enter correct url.')
video = pafy.new(url) # creates a pafy object for a given youtube url.
stream = video.streams # both video and audio
video_audio_streams = list() # list of all the videos which also contain audio
for s in stream:
video_audio_streams.append(
[s.resolution, s.extension, filesizeformat(s.get_filesize()), s.url + "&title=" + video.title])
stream_video = video.videostreams # only video
video_streams = list() # list of all the dash video formats(only video)
for s in stream_video:
video_streams.append(
[s.resolution, s.extension, filesizeformat(s.get_filesize()), s.url + "&title=" + video.title])
stream_audio = video.audiostreams # only audio
audio_streams = list() # list of all the dash audio formats(only audio)
for s in stream_audio:
audio_streams.append(
[s.resolution, s.extension, filesizeformat(s.get_filesize()), s.url + "&title=" + video.title])
return render(request, self.template_name,
{'url': request.POST['url'], 'title': video.title, 'streams': video_audio_streams,
'desc': video.description, 'likes': video.likes,
'dislikes': video.dislikes, 'thumb': video.bigthumbhd,
'duration': video.duration, 'views': video.viewcount,
'stream_video': video_streams, 'stream_audio': audio_streams})
pafy is just giving the direct links to the video files. Video is on google servers, so even if you have download
attribute in your html the it won't work because the video is not on same domain therefore you can't control the headers. What you can do is download the video file temporarily on your server and serve it to the user.
EDIT:
One possible workaround is alt + click
on download links. It will download the file instead of playing it. I tried triggering alt+click
on normal clicks too using js but it won't work. So you will have to alt+click
manually every time.