-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
42 lines (31 loc) · 1009 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, render_template, request, send_file
from pytubefix import YouTube
import io
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/download')
def download():
# Get the URL of the YouTube video
url = request.args.get('url')
format = request.args.get('format')
yt = YouTube(url)
if format == 'mp4':
# Download the video
video = yt.streams.get_highest_resolution()
file_stream = io.BytesIO()
video.stream_to_buffer(file_stream)
file_stream.seek(0)
elif format == 'mp3':
mp3_stream = yt.streams.get_audio_only()
file_stream = io.BytesIO()
mp3_stream.stream_to_buffer(file_stream)
file_stream.seek(0)
# Send the file to the browser
return send_file(file_stream, as_attachment=True, download_name=f'{yt.title}.{format}')
@app.route('/error')
def error():
return render_template('error.html', message='Please fill in all fields!')
if __name__ == '__main__':
app.run()