-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
106 lines (77 loc) · 2.45 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
''''
Created by Vittal Badami
Desc : - This is free youtube video downloader using pytube library.
'''
from flask import *
from pytube import YouTube as yt
import os
from datetime import datetime
from flask import current_app
import shutil
app = Flask(__name__)
audio_no = 0
video_no = 0
i = 0
if not os.path.exists('audios'):
os.mkdir('audios')
if not os.path.exists('videos'):
os.mkdir('videos')
def download_audio(link):
global i
if len(os.listdir('audios')) > 5 or f'{i}_.mp4' in os.listdir('audios'):
shutil.rmtree('audios')
os.mkdir('audios')
url = yt(str(link))
video = url.streams.filter(only_audio=True).first()
out_file = video.download(output_path="./audios", filename=str(i)+'_.mp3')
os.system(
f'ffmpeg -loop 1 -r 1 -i static/images/flyer.jpg -i audios/{i}_.mp3 -c:a copy -shortest -c:v libx264 audios/{i}_.mp4')
os.remove(out_file)
if os.name=='posix':
ret_file = f'audios/{i}_.mp4'
else:
ret_file = f'audios//{i}_.mp4'
i = (i+1) % 5
with open("history.txt", "a") as myfile:
myfile.write(
"\n" + f"{datetime.now().strftime('%d/%m/%y__%H:%M:%S')} --> {link}" + "\n")
return ret_file
def download_video(link):
global i
if len(os.listdir('videos')) > 2:
shutil.rmtree('videos')
os.mkdir('videos')
url = yt(str(link))
video = url.streams.get_lowest_resolution()
out_file = video.download(output_path='./videos', filename=str(i)+'_.mp4')
if os.name=='posix':
ret_file = f'videos/{i}_.mp4'
else:
ret_file = f'videos//{i}_.mp4'
i = (i+1) % 2
with open("history.txt", "a") as myfile:
myfile.write(
"\n" + f"{datetime.now().strftime('%d/%m/%y__%H:%M:%S')} --> {link}" + "\n")
return ret_file
# home page
@app.route('/')
def hello_world():
return render_template('index.html')
# for audio downloading
@app.route('/submit_audio', methods=['POST'])
def submit_audio():
data = request.form.get('link')
print(data)
write_path = download_audio(data)
print(write_path)
return send_file(write_path, as_attachment=True)
# for video downloading
@app.route('/submit', methods=['POST', 'GET'])
def submit():
data = request.form.get('link')
print(data)
write_path = download_video(data)
print(write_path)
return send_file(write_path, as_attachment=True)
if __name__ == "__main__":
app.run(debug=False, port=5000, host="0.0.0.0")