Skip to content

Commit

Permalink
added option to limit bitrate
Browse files Browse the repository at this point in the history
  • Loading branch information
KoljaWindeler committed Feb 5, 2024
1 parent a6a0265 commit 699755d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion custom_components/ytube_music_player/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ async def async_create_form(hass, user_input, page=1):
data_schema[vol.Optional(CONF_LEGACY_RADIO, default=user_input[CONF_LEGACY_RADIO])] = vol.Coerce(bool) # default radio generation typ
data_schema[vol.Optional(CONF_SORT_BROWSER, default=user_input[CONF_SORT_BROWSER])] = vol.Coerce(bool) # sort browser results
data_schema[vol.Optional(CONF_INIT_EXTRA_SENSOR, default=user_input[CONF_INIT_EXTRA_SENSOR])] = vol.Coerce(bool) # default radio generation typ

data_schema[vol.Optional(CONF_TRACK_LIMIT, default=user_input[CONF_TRACK_LIMIT])] = vol.Coerce(int)
data_schema[vol.Optional(CONF_MAX_DATARATE, default=user_input[CONF_MAX_DATARATE])] = vol.Coerce(int)
data_schema[vol.Optional(CONF_BRAND_ID, default=user_input[CONF_BRAND_ID])] = str # brand id
data_schema[vol.Optional(CONF_SELECT_SPEAKERS, default=user_input[CONF_SELECT_SPEAKERS])] = str # drop down to select remote_player
data_schema[vol.Optional(CONF_SELECT_SOURCE, default=user_input[CONF_SELECT_SOURCE])] = str # drop down to select playlist / playlist-radio
Expand Down
3 changes: 3 additions & 0 deletions custom_components/ytube_music_player/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
CONF_LEGACY_RADIO = 'legacy_radio'
CONF_SORT_BROWSER = 'sort_browser'
CONF_INIT_EXTRA_SENSOR = 'extra_sensor'
CONF_MAX_DATARATE = 'max_datarate'

CONF_TRACK_LIMIT = 'track_limit'
CONF_PROXY_URL = 'proxy_url'
Expand All @@ -143,6 +144,7 @@
PROXY_FILENAME = "ytube_proxy.mp4"

DEFAULT_TRACK_LIMIT = 25
DEFAULT_MAX_DATARATE = 0
DEFAULT_LEGACY_RADIO = True
DEFAULT_SORT_BROWSER = True
DEFAULT_SHUFFLE_MODE = 1
Expand Down Expand Up @@ -329,6 +331,7 @@ def ensure_config(user_input):
out[CONF_LEGACY_RADIO] = DEFAULT_LEGACY_RADIO
out[CONF_SORT_BROWSER] = DEFAULT_SORT_BROWSER
out[CONF_INIT_EXTRA_SENSOR] = DEFAULT_INIT_EXTRA_SENSOR
out[CONF_MAX_DATARATE] = DEFAULT_MAX_DATARATE

if user_input is not None:
out.update(user_input)
Expand Down
15 changes: 14 additions & 1 deletion custom_components/ytube_music_player/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self, hass, config, name_add):
self._org_name = config.data.get(CONF_NAME, DOMAIN + name_add)
self._attr_name = self._org_name
self._init_extra_sensor = config.data.get(CONF_INIT_EXTRA_SENSOR, DEFAULT_INIT_EXTRA_SENSOR)
self._maxDatarate = config.data.get(CONF_MAX_DATARATE,DEFAULT_MAX_DATARATE)

# confgurations can be either the full entity_id or just the name
self._select_playlist = input_select.DOMAIN + "." + config.data.get(CONF_SELECT_PLAYLIST, DEFAULT_SELECT_PLAYLIST).replace(input_select.DOMAIN + ".", "")
Expand Down Expand Up @@ -123,6 +124,7 @@ def __init__(self, hass, config, name_add):
self.log_me('debug', "- like_in_name: " + str(self._like_in_name))
self.log_me('debug', "- track_limit: " + str(self._trackLimit))
self.log_me('debug', "- legacy_radio: " + str(self._legacyRadio))
self.log_me('debug', "- max_dataRate: " + str(self._maxDatarate))

self._brand_id = str(config.data.get(CONF_BRAND_ID, ""))
self._api = None
Expand Down Expand Up @@ -1438,6 +1440,17 @@ async def async_get_url(self, videoId=None, retry=60):

# try to find best audio only stream, but accept lower quality if we have to
valid_streams.sort(key=lambda x: x['bitrate'], reverse=True)
self.log_me('debug', "found "+str(len(valid_streams))+" streams")
# remove all streams with too high bitrates
if(self._maxDatarate>0):
for stream in valid_streams:
if(stream['bitrate']>self._maxDatarate):
if(len(valid_streams)>1):
valid_streams.remove(stream)
self.log_me('debug', '- removed stream with too high bitrate of '+str(stream['bitrate']))
else:
self.log_me('debug', '- preseved stream, too high quality but last available stream')

if(retry<20):
streamId = min(3,len(valid_streams))
elif(retry<30):
Expand All @@ -1447,7 +1460,7 @@ async def async_get_url(self, videoId=None, retry=60):
else:
streamId = 0

self.log_me('debug', 'Using stream '+str(streamId)+"/"+str(len(valid_streams)))
self.log_me('debug', 'Using stream '+str(streamId)+"/"+str(len(valid_streams))+", bitrate:"+str(valid_streams[streamId]['bitrate']))
# self.log_me('debug', '- using stream ' + str(streamId))
if(valid_streams[streamId].get('url') is None):
sigCipher_ch = valid_streams[streamId]['signatureCipher']
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ytube_music_player/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"debug_as_error": "Show all debug output as ERROR in the log",
"shuffle": "Turn on shuffle on startup",
"track_limit": "Limit of simultaneously loaded tracks",
"max_datarate": "Limit the maximum bit rate",
"legacy_radio": "Create radio as watchlist of random playlist track",
"sort_browser": "Sort results in the media browser",
"extra_sensor": "Create sensor that provide extra information"
Expand Down Expand Up @@ -74,6 +75,7 @@
"debug_as_error": "Show all debug output as ERROR in the log",
"shuffle": "Turn on shuffle on startup",
"track_limit": "Limit of simultaneously loaded tracks",
"max_datarate": "Limit the maximum bit rate, 0 to disable",
"legacy_radio": "Create radio as watchlist of random playlist track",
"sort_browser": "Sort results in the media browser",
"extra_sensor": "Create sensor that provide extra information"
Expand Down

0 comments on commit 699755d

Please sign in to comment.