Created
February 4, 2014 04:12
-
-
Save meandavejustice/8798044 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import subprocess | |
from flask import Flask, request, redirect, url_for, send_from_directory | |
from werkzeug import secure_filename, SharedDataMiddleware | |
from pydub import AudioSegment | |
DOWNLOAD_FOLDER = 'downloads/' | |
UPLOAD_FOLDER = 'uploads/' | |
ALLOWED_EXTENSIONS = set(['mp3', 'mp4', 'wav', 'ogg', 'flac', 'gif']) | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER | |
app.add_url_rule('/uploads/<filename>', 'uploaded_file', | |
build_only=True) | |
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { | |
'/uploads': app.config['UPLOAD_FOLDER'] | |
}) | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST': | |
file = request.files['file'] | |
if file and allowed_file(file.filename): | |
filename = secure_filename(file.filename) | |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
file.save(os.path.join(filepath)) | |
filepath2 = os.path.join(app.config['DOWNLOAD_FOLDER'], secure_filename('nu_shit2.wav')) | |
stretch_it(filepath, filepath2) | |
return redirect(url_for('uploaded_file', | |
filename=secure_filename('nu_shit2.wav'))) | |
return ''' | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>Upload new File</h1> | |
<form action="" method=post enctype=multipart/form-data> | |
<p><input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
''' | |
def convert_to_wav(file): | |
ext = os.path.splitext(file)[1] | |
if ext.toLowerCase() == 'wav': | |
return file | |
else: | |
converted_track = AudioSegment.from_file(file, ext) | |
return converted_track | |
@app.route('/downloads/<filename>') | |
def uploaded_file(filename): | |
return send_from_directory(app.config['DOWNLOAD_FOLDER'], | |
filename) | |
def stretch_it(filename, new_filename): | |
subprocess.call(["python", "./lib/paulstretch_stereo.py", filename, new_filename]) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment