我正在使用 Django 和 pafy 制作 Youtube 到 mp3 下载器应用程序,
很长一段时间以来,我一直被两个问题困扰,
Pafy 默认提供 webm 和 mp4a 我想将它们转换为 mp3 文件而不将它们下载到我的服务器,可以吗?我可以更改 mp3 文件的缩略图吗?
pafy 提供的链接不是他们开始在浏览器中播放的下载链接。如何将它们转换为可下载的链接,我尝试了一些组合但总是出错。
这是我的文件
视图.py
class download(generic.View):
template_name = 'download_mp3.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.
video = pafy.new(url) # creates a pafy object for a given youtube url.
stream_audio = video.audiostreams # only audio
audio_streams = list() # list of all the dash audio formats(only audio)
for a in stream_audio:
audio_streams.append(
[a.bitrate, a.extension, filesizeformat(a.get_filesize()), a.url + "&title=" + video.title])
return render(request, self.template_name,
{'url': request.POST['url'],
'title': video.title,
'thumb': video.bigthumbhd,
'duration': video.duration, 'views': video.viewcount,
'stream_audio': audio_streams})
网址.py
urlpatterns = [
path('', views.download.as_view(), name='youtube_to_mp3')
]
下载.html
<table class="table table-hover text-center">
<p align="center">Audio Only</p>
<thead>
<tr class="text-center">
<th class="text-center">Bitrate</th>
<th class ="text-center">Extension</th>
<th class="text-center">File Size</th>
<th class="text-center">Download Link</th>
<th class="text-center">Download </th>
</tr>
</thead>
<tbody>
{% for file_size,bitrate,extension,url in stream_audio %}
<tr>
<td>{{ file_size }}</td>
<td>{{ bitrate }} </td>
<td>{{ extension }}</td>
<td><a href="{{ url }}" download="{{ title }}.{{ extension }}"><span
class="glyphicon glyphicon-download-alt"></span> Download</a></td>
<td><a href="{{ download }} "></a> </td>
<!-- <td><a href="{{ download_url }} ">Direct</a> </td> -->
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
{% endif %}