I have this flask backend, which in the future will generate mp3 files dynamically upon user request but for now it’s just a way to route functions.
The problem I am facing is that when the user goes to the frontend there is a button they click for download, this works and it does correctly download the file. But when it downloads it converts the file for some reason to mp4 format and the audio quality is terrible (like low and compressed sounding). I am wondering if this is some browser issue or something else. Id like to know why the frontend converts it to mp4 and why the audio quality is bad.
Here is my flask backend code
from flask import Flask, send_file, render_template
import os
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/run-python', methods=['GET'])
def run_python():
# Define the file path
file_path = os.path.join('static', 'test_file.mp3')
# Check if the file exists
if os.path.exists(file_path):
# Send the file
return send_file(file_path, as_attachment=True, mimetype='audio/mpeg', download_name='test_file.mp3')
else:
return "File not found", 404
if __name__ == '__main__':
app.run(debug=True)
And here is my index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Run Python Example</title>
</head>
<body>
<h1>Run Python Code</h1>
<button id="runPythonButton">Download File</button>
<script>
document.getElementById('runPythonButton').addEventListener('click', function() {
// Open the file in a new tab or prompt the download
window.location.href = '/run-python';
});
</script>
</body>
</html>
And I have the mp3 in static/test_file.mp3
When I play the song here it sounds perfect, but once it gets downloaded through the frontend it that files sound quality is poor.
Thanks
Tried multiple files and same error