Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import io
|
3 |
+
import wave
|
4 |
+
import struct
|
5 |
+
from flask import Flask, render_template, request, send_file, jsonify
|
6 |
+
from werkzeug.utils import secure_filename
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
UPLOAD_FOLDER = '/tmp'
|
11 |
+
ALLOWED_EXTENSIONS = {'mp3', 'wav'}
|
12 |
+
|
13 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
14 |
+
|
15 |
+
def allowed_file(filename):
|
16 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
17 |
+
|
18 |
+
@app.route('/')
|
19 |
+
def index():
|
20 |
+
return render_template('index.html')
|
21 |
+
|
22 |
+
@app.route('/process', methods=['POST'])
|
23 |
+
def process_file():
|
24 |
+
if 'file' not in request.files:
|
25 |
+
return jsonify({'error': 'No file part'}), 400
|
26 |
+
file = request.files['file']
|
27 |
+
if file.filename == '':
|
28 |
+
return jsonify({'error': 'No selected file'}), 400
|
29 |
+
if file and allowed_file(file.filename):
|
30 |
+
filename = secure_filename(file.filename)
|
31 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
32 |
+
file.save(filepath)
|
33 |
+
|
34 |
+
factor = int(request.form['factor'])
|
35 |
+
is_decrypting = filename.lower().endswith('.mp3')
|
36 |
+
|
37 |
+
if is_decrypting:
|
38 |
+
output = deconvert_file(filepath, factor)
|
39 |
+
output_filename = 'deconverted_file.wav'
|
40 |
+
else:
|
41 |
+
output = convert_file(filepath, factor)
|
42 |
+
output_filename = 'converted_file.mp3'
|
43 |
+
|
44 |
+
os.remove(filepath)
|
45 |
+
|
46 |
+
return send_file(
|
47 |
+
io.BytesIO(output),
|
48 |
+
mimetype='audio/mpeg' if not is_decrypting else 'audio/wav',
|
49 |
+
as_attachment=True,
|
50 |
+
download_name=output_filename
|
51 |
+
)
|
52 |
+
return jsonify({'error': 'Invalid file type'}), 400
|
53 |
+
|
54 |
+
def convert_file(filepath, factor):
|
55 |
+
with wave.open(filepath, 'rb') as wav_file:
|
56 |
+
params = wav_file.getparams()
|
57 |
+
frames = wav_file.readframes(wav_file.getnframes())
|
58 |
+
|
59 |
+
audio_data = struct.unpack(f'{len(frames)//2}h', frames)
|
60 |
+
modified_data = [int(sample * factor / 100) for sample in audio_data]
|
61 |
+
|
62 |
+
output = io.BytesIO()
|
63 |
+
with wave.open(output, 'wb') as wav_output:
|
64 |
+
wav_output.setparams(params)
|
65 |
+
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
66 |
+
|
67 |
+
return output.getvalue()
|
68 |
+
|
69 |
+
def deconvert_file(filepath, factor):
|
70 |
+
with wave.open(filepath, 'rb') as wav_file:
|
71 |
+
params = wav_file.getparams()
|
72 |
+
frames = wav_file.readframes(wav_file.getnframes())
|
73 |
+
|
74 |
+
audio_data = struct.unpack(f'{len(frames)//2}h', frames)
|
75 |
+
modified_data = [int(sample * 100 / factor) for sample in audio_data]
|
76 |
+
|
77 |
+
output = io.BytesIO()
|
78 |
+
with wave.open(output, 'wb') as wav_output:
|
79 |
+
wav_output.setparams(params)
|
80 |
+
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
81 |
+
|
82 |
+
return output.getvalue()
|
83 |
+
|
84 |
+
if __name__ == '__main__':
|
85 |
+
app.run(debug=True)
|