Search code examples
pythonhtmldjangoyoutubepytube

How to add the feature to display video formats with the resolution of user's choice to download


The below code is in views.py .I want to take a variable from the user instead of the '360p' after user pastes the video link. Is there a way to show how to get the resolution from the user.I have to take input from the user. The full code looks something like the below webpage 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()```




Solution

  • If you want to do it the "plain vanilla" way, i.e. without using django Forms you can just add a grup of radio buttons or a select to the form in your html, e.g.

    <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>
    

    In your view you can fetch the parameter the same way you are getting the url:

    res = request.GET.get('res')
    

    and then use "res" instead of your hard-coded string.