Last active
September 14, 2016 21:07
-
-
Save astropika/457e3ba56d32196f88338386afcf8cf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import glob | |
import os | |
import requests | |
from io import BytesIO | |
from flask import Flask, send_file, request, Response, stream_with_context | |
from flask_restful import Resource, Api | |
from flask.ext.cors import CORS | |
from pydub import AudioSegment | |
from collections import deque | |
from itertools import islice | |
app = Flask(__name__) | |
api = Api(app) | |
CORS(app) | |
class AudioList(Resource): | |
def get(self): | |
files = glob.glob("audio/*") | |
return {'files': files} | |
def generate(audio,fmt,codec,window_size=1000): | |
cursor=0 | |
chunktime=(window_size / 1000) | |
end=len(audio) | |
au_buf=BytesIO() | |
while cursor+window_size < end: | |
au_buf.seek(0) | |
audio[cursor:cursor+window_size].export(au_buf, format=fmt,codec=codec) | |
cursor+=window_size | |
au_buf.seek(0) | |
yield au_buf.read() | |
def request_conv(url,fmt,codec,gain=None): | |
b_size=102400 | |
r = requests.get(url, stream=True) | |
r.raw.decode_content = True | |
in_buf=BytesIO() | |
au_buf=BytesIO() | |
for chunk in r.iter_content(b_size): | |
print("converting chunk") | |
in_buf=BytesIO() | |
in_buf.write(chunk) | |
in_buf.seek(0) | |
in_fmt=r.headers['content-type'].split('/')[1] | |
orig = AudioSegment.from_file(in_buf, format=in_fmt) | |
if gain: | |
orig=orig.apply_gain(gain) | |
orig.export(au_buf, format=fmt,codec=codec) | |
au_buf.seek(0) | |
yield au_buf.read() | |
@app.route('/convert/<path:url>') | |
def stream_convert(url): | |
try: | |
brate=request.args['bitrate'] | |
except: | |
brate="128k" | |
try: | |
fmt=request.args['fmt'].lower() | |
except: | |
fmt="mp3" | |
try: | |
codec=request.args['codec'].lower() | |
except: | |
codec="libmp3lame" | |
fmt_mime={ | |
"mp3":"audio/mpeg", | |
"ogg":"audio/ogg", | |
"mp4":"audio/mp4" | |
} | |
try: | |
gain=request.args['gain'] | |
except: | |
gain=None | |
return Response(request_conv(url,fmt,codec,gain),mimetype=fmt_mime[fmt]) | |
@app.route('/audio/<path:path>') | |
def serve_audio(path): | |
try: | |
brate=request.args['bitrate'] | |
except: | |
brate="128k" | |
try: | |
fmt=request.args['fmt'].lower() | |
except: | |
fmt="mp3" | |
try: | |
codec=request.args['codec'].lower() | |
except: | |
codec="libmp3lame" | |
fmt_mime={ | |
"mp3":"audio/mpeg", | |
"ogg":"audio/ogg", | |
"mp4":"audio/mp4" | |
} | |
try: | |
gain=request.args['gain'] | |
except: | |
gain=None | |
orig = AudioSegment.from_file("./audio/"+path, format="flac") | |
if gain: | |
orig=orig.apply_gain(gain) | |
return Response(generate(orig,fmt,codec),mimetype=fmt_mime[fmt]) | |
api.add_resource(AudioList, '/') | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example requests:
http://localhost:5000/audio/testfile.flac?gain=+1000&fmt=ogg&codec=libvorbis&bitrate=128k
http://localhost:5000/convert/http://example.com/testfile.wav?gain=+1000&fmt=mp3&codec=libmp3lame&bitrate=192k