0

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

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

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

我用来获取下载链接的模板是,

{% 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 获取直接链接,我进行了很多搜索,但没有找到有效的答案,对我没有任何帮助?

更新

视图.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})
4

1 回答 1

0

pafy 只是提供视频文件的直接链接。视频位于谷歌服务器上,因此即使download您的 html 中有属性,它也无法正常工作,因为视频不在同一个域中,因此您无法控制标题。您可以做的是在您的服务器上临时下载视频文件并将其提供给用户。

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

于 2018-10-08T08:09:44.257 回答