Skip to content

Commit

Permalink
big improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfiokpskiopdf committed Apr 23, 2021
1 parent 1b8376b commit 3f65c6f
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 27 deletions.
Binary file modified __pycache__/backend.cpython-39.pyc
Binary file not shown.
64 changes: 56 additions & 8 deletions backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pytube import YouTube
import ffmpeg
import datetime
import os

class Downloader:
def __init__(self):
Expand All @@ -9,19 +10,66 @@ def set_url(self, url):
self.youtube = YouTube(url)

def get_videos(self):
return [stream for stream in self.youtube.streams.filter(only_video=True)]
return [stream for stream in self.youtube.streams.filter(only_video=True, video_codec="vp9")]

def get_audios(self):
return [stream for stream in self.youtube.streams.filter(only_audio=True)]

def get_audio_videos(self):
return [stream for stream in self.youtube.streams.filter(progressive=True)]

def combine_audio_video(self, audio, video):
pass
#video_stream = ffmpeg.input(video.download().split("//")[-1])
#audio_stream = ffmpeg.input(audio.download().split("//")[-1])
#ffmpeg.output(audio_stream, video_stream, 'out.mp4').run()
def find_video(self, resolution, fps):
video = None

for stream in self.get_videos():
if str(stream.resolution) == resolution:
if str(stream.fps) == fps:
video = stream
break

return video

def find_audio(self, bitrate):
audio = None

for stream in self.get_audios():
print(bitrate)
print(stream.abr)
if str(stream.abr) == bitrate:
audio = stream
break

print(audio)
return audio

def find_audio_videos(self, resolution, fps, bitrate):
video = None

for stream in self.get_audio_videos():
if str(stream.resolution) == resolution:
if str(stream.fps) == fps:
if str(stream.abr) == bitrate:
video = stream
break

print(video)
return video

def download(self, video):
print(video)
local_path = video.download()
fname = local_path.split("//")[-1]

return fname

def get_title(self):
return self.youtube.title

def get_thumbnail(self):
return self.youtube.thumbnail_url

def get_views(self):
return self.youtube.views

#fix this
def get_duration(self):
return str(datetime.timedelta(seconds=self.youtube.length))
44 changes: 33 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

app = Flask(__name__)
downloader = Downloader()
app.secret_key = "37825789567878456784867878680"

@app.context_processor
def override_url_for():
Expand All @@ -23,19 +24,40 @@ def dated_url_for(endpoint, **values):
@app.route("/", methods=["POST", "GET"])
def home():
if request.method == "POST":
url = request.form["ur"]
downloader.set_url(url)
videos = downloader.get_videos()
audios = downloader.get_audios()
audiovideos = downloader.get_audio_videos()
if "video_button" in request.form:
quality_list = request.form["video_button"].split(",")
video = downloader.find_video(quality_list[0], quality_list[1])
fname = downloader.download(video)
return send_file(fname, as_attachment=True)
elif "audio_button" in request.form:
video = downloader.find_audio(request.form["audio_button"])
fname = downloader.download(video)
return send_file(fname, as_attachment=True)
elif "audiovideo_button" in request.form:
quality_list = request.form["audiovideo_button"].split(",")
video = downloader.find_audio_videos(quality_list[0], quality_list[1], quality_list[2])
fname = downloader.download(video)
return send_file(fname, as_attachment=True)
else:
url = request.form["ur"]
session["url"] = url
downloader.set_url(session["url"])
videos = downloader.get_videos()
audios = downloader.get_audios()
audiovideos = downloader.get_audio_videos()
title = downloader.get_title()
thumbnail = downloader.get_thumbnail()
views = downloader.get_views()
duration = downloader.get_duration()

print(videos)
print(audios)
print(audiovideos)
print(duration)

return render_template("index.html")
return render_template("index.html", url=session["url"], videos=videos, audios=audios, audiovideos=audiovideos, title=title, thumbnail=thumbnail, views=views, duration=duration)
else:
return render_template("index.html")
if "url" in session:
return render_template("index.html", url=session["url"])
else:
return render_template("index.html", url="")

@app.route("/about")
def about():
Expand All @@ -46,5 +68,5 @@ def contact():
return "contact"

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")
app.run(debug=True)

2 changes: 1 addition & 1 deletion static/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
--hover: #4b4b4b;
--main-text-color: #ffffff;
--secondary-text-color: #aaaaaa;
--red: #ff0000;
--red: #c00;
--search-color: #121212;
--search-color-2: #313131;
--search-color-3: #585858;
Expand Down
118 changes: 113 additions & 5 deletions static/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
color: var(--secondary-text-color);
}

.center-container form {
.center-container .form {
margin-top: 2rem;
background-color: var(--secondary-bg-color);
padding: 1.5rem 2rem;
Expand All @@ -29,7 +29,7 @@
}

.center-container form input[type=search] {
padding: 1.5rem 2rem;
padding: 1.5% 2%;
box-sizing: border-box;
border: 1px solid var(--main-bg-color);
background-color: var(--search-color);
Expand All @@ -45,15 +45,123 @@
color: var(--secondary-text-color);
}

.center-container form button {
padding: 1.5rem 2rem;
.center-container form .search_button {
padding: 1.5% 2%;
flex: 1;
background-color: var(--search-color-2);
border: 1px solid var(--main-bg-color);
color: var(--search-color-3);
font-size: 1.1rem;
}

.center-container form button:hover {
.center-container form .search_button:hover {
color: var(--main-text-color);
}

.result_container {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 90%;
gap: 2%;
}

.downloads {
display: grid;
grid-template-areas: "video videoaudio audio";
height: 100%;
grid-template-rows: auto auto auto;
margin-top: 2%;
flex: 3;
}

.video {
grid-area: video;
}

.videoaudio {
grid-area: videoaudio;
}

.audio {
grid-area: audio;
}

.downloads h2 {
margin-bottom: 5%;
color: var(--secondary-text-color)
}

.result_box {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}

.result_box p {
margin-right: 2%;
color: var(--secondary-text-color);
}

.result_box button {
padding: 10% 20%;
text-transform: uppercase;
color: var(--main-text-color);
background-color: var(--red);
border: none;
border-radius: 25px;
cursor: pointer;
}

.main_text {
color: var(--main-text-color) !important;
}

.videoaudio p {
color: var(--main-text-color) !important;
}

.information {
margin-top: 2%;
margin-right: auto;
display: flex;
flex-direction: column;
flex: 2;
}

.information h2 {
margin-right: auto;
}

.information img {
max-width: 100%;
max-height: 100%;
margin-right: auto;
margin-bottom: 2%;
}

.information p {
margin-right: auto;
}

@media only screen and (max-width: 1370px) {
.downloads {
grid-template-areas: "videoaudio"
"video"
"audio";
width: 100%;
}
}

@media only screen and (max-width: 600px) {
.information {
display: none;
}
}

@media only screen and (min-width: 600px) {
.downloads {
margin-left: auto;
}
}
48 changes: 46 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,52 @@
<h1 class="title">YT Buddy</h1>
<p class="subtitle">Convert and download YouTube video and audio online, for <b>free</b>.</p>
<form class="form" action="#" method="POST">
<input type="search" placeholder="Paste youtube link here" name="ur" required>
<button type="submit">Convert</button>
<input type="search" placeholder="Paste youtube link here" name="ur" value="{{ url }}" required>
<button class="search_button" type="submit">Convert</button>
</form>
{% if audiovideos is defined %}
<div class="result_container">
<div class="information">
<h2>{{ title }}</h2>
<img src="{{ thumbnail }}">
<p>Views: <b>{{ views }}</b> </p>
<p>Duration: <b>{{ duration }}</b></p>
</div>
<div class="downloads">
<div class="video">
<h2>Video Only</h2>
{% for video in videos %}
<div class="result_box">
<p class="text">{{ video.resolution }}, {{ video.fps }} fps</p>
<form action="#" method="POST">
<button type="submit" name="video_button" value="{{ video.resolution }},{{ video.fps }}">Download</button>
</form>
</div>
{% endfor %}
</div>
<div class="videoaudio">
<h2 class="main_text">Video & Audio (<b>Recommended</b>)</h2>
{% for av in audiovideos %}
<div class="result_box">
<p class="text">{{ av.resolution }}, {{ av.fps }} fps, {{ av.abr }}</p>
<form action="" method="POST">
<button type="submit" name="audiovideo_button" value="{{ av.resolution }},{{ av.fps }},{{ av.abr }}"><b>Download</b></button>
</form>
</div>
{% endfor %}
</div>
<div class="audio">
<h2>Audio Only</h2>
{% for audio in audios %}
<div class="result_box">
<p class="text">{{ audio.abr }}</p>
<form action="#" method="POST">
<button type="submit" name="audio_button" value="{{ audio.abr }}">Download</button>
</form>
</div>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endblock %}

0 comments on commit 3f65c6f

Please sign in to comment.