From 4385cc98d1af499be4eeb5ec1269203928d749ff Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Fri, 27 Nov 2015 19:38:10 +0000 Subject: Serve transcoded track --- mach2.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mach2.py b/mach2.py index 31f9466..ffa1c1e 100644 --- a/mach2.py +++ b/mach2.py @@ -1,7 +1,11 @@ import configparser import json import mimetypes +import os import sqlite3 +import subprocess +import tempfile +import threading from flask import Flask, Response, current_app, g, redirect, render_template from flask import request, url_for @@ -144,17 +148,23 @@ def track(track_id): if chunk: yield chunk else: + os.remove(filename) break local_track = Track(track_id) + fd, temp_filename = tempfile.mkstemp() + + subprocess.call(["ffmpeg", "-y", "-i", local_track.filename, "-acodec", + "libopus", "-b:a", "64000", "-f", "opus", temp_filename]) + mime_string = "application/octet-stream" - mime = mimetypes.guess_type(local_track.filename) + mime = mimetypes.guess_type(temp_filename) if mime[0]: mime_string = mime[0] - resp = Response(stream_file(local_track.filename), mimetype=mime_string) + resp = Response(stream_file(temp_filename), mimetype=mime_string) if mime[1]: resp.headers["Content-Encoding"] = mime[1] -- cgit v1.2.3