以下代码位于views.py中。在用户粘贴视频链接后,我想从用户那里获取一个变量,而不是“360p”。有没有办法显示如何从用户那里获得分辨率。我必须从用户那里获取输入。
完整的代码类似于下面的网页
https://www.hexascholars.com/code-snippet/youtube-video-download-using-django-and-python/
def get_download(request):
if request.method == 'GET':
if(request.GET.get('url')):
url = request.GET['url']
try:
yt = YouTube(url)
title = yt.title
stream = yt.streams.filter(res='360p').first()
path = download_path()
stream.download(path)
message = "Download Complete!"
video = Video()```
最佳答案
如果您想以“普通 Vanilla ”的方式进行操作,即不使用Django表单,则只需在HTML表单中添加一个单选按钮组或一个选择即可,例如
<form method="GET" action="/your/url/">
<div class="form-group">
<label>Enter Youtube URL</label>
<input type="url" class="form-control" required="required" name="link">
</div>
<div class="form-group">
<label>Resolution</label>
<fieldset>
<input type="radio" name="res" id="res1" value="360p">
<label for="res1">360p</label>
<input type="radio" name="res" id="res2" value="720p">
<label for="res1">720p</label>
</fieldset>
</div>
<button type="submit" class="save btn btn-success">Download</button>
</form>
在您看来,您可以通过获取网址的相同方式来获取参数:
res = request.GET.get('res')
然后使用“res”代替您的硬编码字符串。
关于python - 如何添加功能以显示视频格式以及用户选择的下载分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959378/