Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, send_from_directory
|
2 |
+
from spleeter.separator import Separator
|
3 |
+
import os
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
UPLOAD_FOLDER = 'uploads'
|
8 |
+
OUTPUT_FOLDER = 'output' # Usar uma pasta de sa铆da separada 茅 uma boa pr谩tica
|
9 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
10 |
+
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
11 |
+
|
12 |
+
# Garante que as pastas existam
|
13 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
14 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
15 |
+
|
16 |
+
@app.route('/', methods=['GET', 'POST'])
|
17 |
+
def index():
|
18 |
+
if request.method == 'POST':
|
19 |
+
if 'audiofile' not in request.files:
|
20 |
+
return render_template('index.html', error='Nenhum arquivo enviado.')
|
21 |
+
file = request.files['audiofile']
|
22 |
+
if file.filename == '':
|
23 |
+
return render_template('index.html', error='Nenhum arquivo selecionado.')
|
24 |
+
if file:
|
25 |
+
filename = secure_filename(file.filename)
|
26 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
27 |
+
file.save(filepath)
|
28 |
+
|
29 |
+
# Define o diret贸rio de sa铆da para os stems
|
30 |
+
output_path = os.path.join(app.config['OUTPUT_FOLDER'], os.path.splitext(filename)[0])
|
31 |
+
|
32 |
+
separator = Separator('spleeter:2stems')
|
33 |
+
separator.separate_to_file(filepath, app.config['OUTPUT_FOLDER'])
|
34 |
+
|
35 |
+
# Nome da pasta de sa铆da para os links
|
36 |
+
output_foldername = os.path.splitext(filename)[0]
|
37 |
+
|
38 |
+
return render_template('index.html',
|
39 |
+
vocals_path=f'{output_foldername}/vocals.wav',
|
40 |
+
accompaniment_path=f'{output_foldername}/accompaniment.wav')
|
41 |
+
return render_template('index.html')
|
42 |
+
|
43 |
+
@app.route('/download/<path:filename>')
|
44 |
+
def download_file(filename):
|
45 |
+
return send_from_directory(app.config['OUTPUT_FOLDER'], filename, as_attachment=True)
|
46 |
+
|
47 |
+
if __name__ == '__main__':
|
48 |
+
app.run(debug=True)
|