-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (71 loc) · 2.74 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
from flask import Flask, render_template, request, redirect
import os
from werkzeug.utils import secure_filename
import yt_dlp
from SVMFinal import extract_feature
import time
import ssl
import urllib.request
import tempfile
import io
import pickle
import json
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB
app.config['ALLOWED_EXTENSIONS'] = {'.wav', '.mp3', '.aif'}
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
ssl._create_default_https_context = ssl._create_unverified_context
@app.route('/')
def main():
return render_template('main.html')
@app.route('/upload', methods=['POST'])
def upload():
if request.method =='POST':
file = request.files['music-file'] # This is the file NAME in the HTML
if file:
filename = secure_filename(file.filename)
file.save(filename)
extension = os.path.splitext(filename)[1].lower()
if extension not in app.config['ALLOWED_EXTENSIONS']:
message = 'Error: This is not an mp3, wav, or aif...'
response = ''
else:
response = extract_feature(filename)
message = ''
os.remove(filename)
else:
message = 'Error: There was no file submitted...'
response = ''
return render_template('main.html', message=message, response=response)
else:
pass
@app.route('/youtube_upload', methods=['POST'])
def youtube_upload():
if request.method == 'POST':
# try:
youtube_link = request.form.get('youtube-link') # Access the value of the 'youtube-link' input field
if youtube_link:
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(youtube_link, download=True)
filename = ydl.prepare_filename(info_dict)
filename = filename[0:-4] + 'mp3'
response = extract_feature(filename)
try:
os.remove(filename)
except:
pass
else:
return render_template('main.html')
return render_template('main.html', response=response)
if __name__ == '__main__':
app.run(port=8083, debug=True)