如何在Django webapp中通过pafy和youtube-dl强制下载文件

How to force download files through pafy and youtube-dl in Django webapp

我用 django 和 pafy 制作了一个 youtube 视频下载器应用程序。

我得到的最终 link 中的问题类似于 https://r5---sn-gwpa-jj0z-blah-blah

这些 link 正在浏览器中打开文件如何使它们可以在客户端计算机上下载?

我用来下载 link 的模板是,

{% 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 %}

是否可以直接从 youtube-dl 获取 links,我进行了大量搜索但没有找到有效的答案,对我来说没有任何用处?

更新

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 只是提供视频文件的直接链接。视频在 google 服务器上,所以即使你的 html 中有 download 属性,它也不会工作,因为视频不在同一个域中,因此你无法控制 headers。您可以做的是 download 将视频文件暂时放在您的服务器上并提供给用户。

编辑:
一种可能的解决方法是 alt + click 下载链接。它将下载文件而不是播放它。我也尝试使用 js 在正常点击时触发 alt+click,但它不起作用。所以你每次都必须手动alt+click