how to download video from pytube module only when clicking html button in flask?

I am working on a project to download youtube video by pytube module. I want to download a specific resolution video only when user click on button in html.

How do I pass pytube steam to html and make downloadable button?

app.py

from flask import Flask, render_template, request
import os
from ydownload import Ymate

app = Flask(__name__)
user_query = ''
input_value = ''
base_url = 'https://www.youtube.com/watch?v='



#--------------------------------------------------
@app.route('/')
def youtube_link():
    if request.method == 'GET':
        link = 'e7BufAVwDiM'
        return render_template("main.html", input_value=base_url+link, user_query='ydownload', ymate=Ymate(link))

if name == "main": app.run(debug=True)

main.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Youtube</title>
</head>
<body>

    <div class="main">
        <div class="container">
            <div class="panel panel-default m-b-0">
                <div class="panel-body">
                    <div class="div-form">
                        <div class="hero hero-homepage">
                            <h1 class="hero-header">Download Unlimited Videos From Youtube</h1>
                            <div class="ads_banner m-t m-b">
                            </div>
                            <div>
                                <form class="form-inline form-search" action=""
                                    id="search-form" method="get" name="mc-embedded-subscribe-form">
                                    <input class="form-control input-lg" value="{{ input_value }}" id="txt-url"
                                        name="txt" type="text" placeholder="Search or Paste link here.....">
                                    <button type="submit" value="Start" id="btn-submit" class="btn btn-lg"
                                        style="color: white;">
                                        <span class="hidden-xs">Search </span>
                                        <span class="glyphicon glyphicon-arrow-right"></span>
                                    </button>
                                    <p class="small">You are accepting our
                                        <a class="active" href="" target="_blank">
                                            Terms of Use
                                        </a> by using our services.
                                    </p>
                                    <img class="hidden" id="loading_bar"
                                        src="" />
                                </form>
                            </div>
                        </div>
                    </div>

                    {% if 'ydownload' in user_query %}
                    <div class="container-fluid" id="query-response">
                        <div class="row">
                            <div class="col-sm-5" id="thumbnail-div">
                                <div class="row">
                                    <img src="{{ ymate.title_img_url()['img_url'] }}">
                                    <p><b>{{ ymate.title_img_url()['title'] }}</b></p>
                                </div>
                            </div>
                            <div class="col-sm-7" id="query-table">
                                <ul class="nav nav-tabs">
                                    <li class="nav-item">
                                        <a class="nav-link" data-toggle="tab" id="video_tab"><b>Video</b></a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" data-toggle="tab" id="audio_tab"><b>Audio</b></a>
                                    </li>
                                </ul>
                                <table class="table table-bordered" id="video">
                                    <thead>
                                        <tr>
                                            <th>Resolution</th>
                                            <th>FileSize</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for video in ymate.video_list() %}
                                        <tr>
                                            <td>{{ video }}</td>
                                            <td>{{ ymate.video_list()[video][0] }}</td>
                                            <td>
                                                <a href="/" download="" class="btn btn-success">
                                                    <i class="fa fa-download"></i>
                                                    <span class="btn-hidden">Download</span>
                                                </a>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>


                                <table class="table table-bordered" id="audio" style="display: none">
                                    <thead>
                                        <tr>
                                            <th>FileType</th>
                                            <th>FileSize</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for audio in ymate.audio_list() %}
                                        <tr>
                                            <td>{{ audio }}</td>
                                            <td>{{ ymate.audio_list()[audio][0] }}</td>
                                            <td>
                                                <a href="{{ ymate.audio_list()[audio][1] }}" class="btn btn-success">
                                                    <i class="fa fa-download"></i>
                                                    <span class="btn-hidden">Download</span>
                                                </a>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>

                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</body>
</html>

ydownload.py

from pytube import YouTube, Playlist
from pprint import pprint
from math import floor, log, pow

class Ymate:
    def __init__(self, url):
        main_url = f'https://www.youtube.com/watch?v={url}'
        self.video_ = YouTube(main_url)
        self.vid_steam = self.video_.streams

    def title_img_url(self):
        items = {   "img_url": self.video_.thumbnail_url,
                    "title": self.video_.title
                    }
        return items

    @staticmethod
    def convert_size(size_bytes):
        if size_bytes == 0:
            return "0B"
        size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
        i = int(floor(log(size_bytes, 1024)))
        p = pow(1024, i)
        s = round(size_bytes / p, 2)
        return "%s %s" % (s, size_name[i])

    def audio_list(self):
        audio_steam = self.vid_steam.filter(type='audio').order_by('abr').asc()
        audio_dict = {}
        for counter in range(len(audio_steam)):
            audio_dict.update({
                audio_steam[counter].abr: [self.convert_size(audio_steam[counter].filesize_approx), audio_steam[counter]]
                })
        return audio_dict

    def video_list(self):
        video_steam = self.vid_steam.filter(type='video').order_by('resolution').asc()
        vid_res = []
        res_counter = 0
        video_dict = {}
        for count in range(len(video_steam)):
            if video_steam[count].resolution not in vid_res:
                vid_res.append(video_steam[count].resolution)
                vid_list = video_steam.filter(resolution=vid_res[res_counter]).first()
                res_counter += 1
                video_dict.update({
                    vid_list.resolution: [self.convert_size(vid_list.filesize_approx), vid_list]
                })
        return video_dict

I want to learn how can I iterate pytube.steam into html and make a button to download specify resolution video in by the help of python or flask!

1 answer

  • answered 2021-04-21 23:01 Ulto 4

    In the flask file try adding a post method. This would understand when a button/form is submitted and can act appropriately. Here's some info to get you started.