diff --git a/api/media.py b/api/media.py index 2c2d43d..3877b41 100755 --- a/api/media.py +++ b/api/media.py @@ -7,7 +7,7 @@ import subprocess import config, scanner from web import app -from db import Track, Folder, User, now, session +from db import Track, Folder, User, ClientPrefs, now, session from api import get_entity def prepare_transcoding_cmdline(base_cmdline, input_file, input_format, output_format, output_bitrate): @@ -24,33 +24,40 @@ def stream_media(): if not status: return res - maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ]) + maxBitRate, format, timeOffset, size, estimateContentLength, client = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength', 'c' ]) if format: format = format.lower() - do_transcoding = False src_suffix = res.suffix() dst_suffix = res.suffix() dst_bitrate = res.bitrate dst_mimetype = res.content_type - if format != 'raw': # That's from API 1.9.0 but whatever - if maxBitRate: - try: - maxBitRate = int(maxBitRate) - except: - return request.error_formatter(0, 'Invalid bitrate value') + if client: + prefs = ClientPrefs.query.get((request.user.id, client)) + if not prefs: + prefs = ClientPrefs(user_id = request.user.id, client_name = client) + session.add(prefs) - if dst_bitrate > maxBitRate and maxBitRate != 0: - do_transcoding = True - dst_bitrate = maxBitRate + if prefs.format: + dst_suffix = prefs.format + if prefs.bitrate and prefs.bitrate < dst_bitrate: + dst_bitrate = prefs.bitrate - if format and format != src_suffix: - do_transcoding = True - dst_suffix = format - dst_mimetype = scanner.get_mime(dst_suffix) + if maxBitRate: + try: + maxBitRate = int(maxBitRate) + except: + return request.error_formatter(0, 'Invalid bitrate value') - if do_transcoding: + if dst_bitrate > maxBitRate and maxBitRate != 0: + dst_bitrate = maxBitRate + + if format and format != 'raw' and format != src_suffix: + dst_suffix = format + dst_mimetype = scanner.get_mime(dst_suffix) + + if format != 'raw' and (dst_suffix != src_suffix or dst_bitrate != res.bitrate): transcoder = config.get('transcoding', 'transcoder_{}_{}'.format(src_suffix, dst_suffix)) decoder = config.get('transcoding', 'decoder_' + src_suffix) or config.get('transcoding', 'decoder') encoder = config.get('transcoding', 'encoder_' + dst_suffix) or config.get('transcoding', 'encoder') @@ -78,6 +85,7 @@ def stream_media(): proc.terminate() proc.wait() + app.logger.info('Transcoding track {0.id} for user {1.id}. Source: {2} at {0.bitrate}kbps. Dest: {3} at {4}kbps'.format(res, request.user, src_suffix, dst_suffix, dst_bitrate)) response = Response(transcode(), mimetype = dst_mimetype) else: response = send_file(res.path, mimetype = dst_mimetype) diff --git a/db.py b/db.py index 4c932e3..13bd479 100755 --- a/db.py +++ b/db.py @@ -97,6 +97,14 @@ class User(Base): 'shareRole': False } +class ClientPrefs(Base): + __tablename__ = 'client_prefs' + + user_id = Column(UUID, ForeignKey('user.id'), primary_key = True) + client_name = Column(String(32), nullable = False, primary_key = True) + format = Column(String(8), nullable = True) + bitrate = Column(Integer, nullable = True) + class Folder(Base): __tablename__ = 'folder'