1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Introducing client prefs

Allow setting default transcoding values for specific clients
This commit is contained in:
spl0k 2013-12-07 18:46:30 +01:00
parent 82992047dd
commit 4cd1aace76
2 changed files with 33 additions and 17 deletions

View File

@ -7,7 +7,7 @@ import subprocess
import config, scanner import config, scanner
from web import app 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 from api import get_entity
def prepare_transcoding_cmdline(base_cmdline, input_file, input_format, output_format, output_bitrate): def prepare_transcoding_cmdline(base_cmdline, input_file, input_format, output_format, output_bitrate):
@ -24,33 +24,40 @@ def stream_media():
if not status: if not status:
return res 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: if format:
format = format.lower() format = format.lower()
do_transcoding = False
src_suffix = res.suffix() src_suffix = res.suffix()
dst_suffix = res.suffix() dst_suffix = res.suffix()
dst_bitrate = res.bitrate dst_bitrate = res.bitrate
dst_mimetype = res.content_type dst_mimetype = res.content_type
if format != 'raw': # That's from API 1.9.0 but whatever if client:
if maxBitRate: prefs = ClientPrefs.query.get((request.user.id, client))
try: if not prefs:
maxBitRate = int(maxBitRate) prefs = ClientPrefs(user_id = request.user.id, client_name = client)
except: session.add(prefs)
return request.error_formatter(0, 'Invalid bitrate value')
if dst_bitrate > maxBitRate and maxBitRate != 0: if prefs.format:
do_transcoding = True dst_suffix = prefs.format
dst_bitrate = maxBitRate if prefs.bitrate and prefs.bitrate < dst_bitrate:
dst_bitrate = prefs.bitrate
if format and format != src_suffix: if maxBitRate:
do_transcoding = True try:
dst_suffix = format maxBitRate = int(maxBitRate)
dst_mimetype = scanner.get_mime(dst_suffix) 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)) transcoder = config.get('transcoding', 'transcoder_{}_{}'.format(src_suffix, dst_suffix))
decoder = config.get('transcoding', 'decoder_' + src_suffix) or config.get('transcoding', 'decoder') decoder = config.get('transcoding', 'decoder_' + src_suffix) or config.get('transcoding', 'decoder')
encoder = config.get('transcoding', 'encoder_' + dst_suffix) or config.get('transcoding', 'encoder') encoder = config.get('transcoding', 'encoder_' + dst_suffix) or config.get('transcoding', 'encoder')
@ -78,6 +85,7 @@ def stream_media():
proc.terminate() proc.terminate()
proc.wait() 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) response = Response(transcode(), mimetype = dst_mimetype)
else: else:
response = send_file(res.path, mimetype = dst_mimetype) response = send_file(res.path, mimetype = dst_mimetype)

8
db.py
View File

@ -97,6 +97,14 @@ class User(Base):
'shareRole': False '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): class Folder(Base):
__tablename__ = 'folder' __tablename__ = 'folder'