Sergidev commited on
Commit
42bca7e
·
verified ·
1 Parent(s): 67b3880

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -34,21 +34,28 @@ def process_file():
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):
@@ -56,7 +63,7 @@ def convert_file(filepath, factor):
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()
@@ -71,7 +78,7 @@ def deconvert_file(filepath, factor):
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()
 
34
  factor = int(request.form['factor'])
35
  is_decrypting = filename.lower().endswith('.mp3')
36
 
37
+ try:
38
+ if is_decrypting:
39
+ output = deconvert_file(filepath, factor)
40
+ output_filename = 'deconverted_file.wav'
41
+ mimetype = 'audio/wav'
42
+ else:
43
+ output = convert_file(filepath, factor)
44
+ output_filename = 'converted_file.mp3'
45
+ mimetype = 'audio/mpeg'
46
 
47
+ os.remove(filepath)
48
+
49
+ return send_file(
50
+ io.BytesIO(output),
51
+ mimetype=mimetype,
52
+ as_attachment=True,
53
+ download_name=output_filename
54
+ )
55
+ except Exception as e:
56
+ os.remove(filepath)
57
+ return jsonify({'error': str(e)}), 500
58
 
 
 
 
 
 
 
59
  return jsonify({'error': 'Invalid file type'}), 400
60
 
61
  def convert_file(filepath, factor):
 
63
  params = wav_file.getparams()
64
  frames = wav_file.readframes(wav_file.getnframes())
65
 
66
+ audio_data = struct.unpack(f'{len(frames)//params.sampwidth}h', frames)
67
  modified_data = [int(sample * factor / 100) for sample in audio_data]
68
 
69
  output = io.BytesIO()
 
78
  params = wav_file.getparams()
79
  frames = wav_file.readframes(wav_file.getnframes())
80
 
81
+ audio_data = struct.unpack(f'{len(frames)//params.sampwidth}h', frames)
82
  modified_data = [int(sample * 100 / factor) for sample in audio_data]
83
 
84
  output = io.BytesIO()