Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
834 B
1
Indexable
Never
from flask import Flask, send_file, stream_with_context
import os
import time

app = Flask(__name__)

mp3_directory = '/path/to/your/mp3/directory'  # Replace this with the path to your MP3 directory

def generate():
    while True:
        for filename in os.listdir(mp3_directory):
            if filename.endswith('.mp3'):
                file_path = os.path.join(mp3_directory, filename)
                with open(file_path, 'rb') as f:
                    yield f.read()
                    time.sleep(1)  # Add a delay to control the streaming speed

@app.route('/stream')
def stream():
    return app.response_class(
        stream_with_context(generate()),
        mimetype='audio/mpeg',
        headers={'Content-Disposition': 'attachment; filename=current_song.mp3'}
    )

if __name__ == '__main__':
    app.run(debug=True)