Skip to content

Commit

Permalink
Refactor variant handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lossless1024 committed Sep 11, 2023
1 parent 36ed399 commit bf8a5fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
36 changes: 21 additions & 15 deletions streamonitor/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ def getPlaylistVariants(self, url):
m3u8_doc = result.content.decode("utf-8")
variant_m3u8 = m3u8.loads(m3u8_doc)
for playlist in variant_m3u8.playlists:
resolution = playlist.stream_info.resolution if type(playlist.stream_info.resolution) is tuple else (0, 0)
sources.append(( playlist.uri, resolution ))
stream_info = playlist.stream_info
resolution = stream_info.resolution if type(stream_info.resolution) is tuple else (0, 0)
sources.append({
'url': playlist.uri,
'resolution': resolution,
'frame_rate': stream_info.frame_rate,
'bandwidth': stream_info.bandwidth
})

if not variant_m3u8.is_variant and len(sources) >= 1:
self.logger.warn("Not variant playlist, can't select resolution")
Expand All @@ -200,32 +206,29 @@ def getWantedResolutionPlaylist(self, url):
self.logger.error("No available sources")
return None

sources2 = []
for source in sources:
width, height = source[1]
width, height = source['resolution']
if width < height:
source += (width - WANTED_RESOLUTION,)
source['resolution_diff'] = width - WANTED_RESOLUTION
else:
source += (height - WANTED_RESOLUTION,)
sources2.append(source)
sources = sources2
source['resolution_diff'] = height - WANTED_RESOLUTION

sources.sort(key=lambda a: abs(a[2]))
sources.sort(key=lambda a: abs(a['resolution_diff']))
selected_source = None

if WANTED_RESOLUTION_PREFERENCE == 'exact':
if sources[0][2] == 0:
if sources[0]['resolution_diff'] == 0:
selected_source = sources[0]
elif WANTED_RESOLUTION_PREFERENCE == 'closest' or len(sources) == 1:
selected_source = sources[0]
elif WANTED_RESOLUTION_PREFERENCE == 'exact_or_least_higher':
for source in sources:
if source[2] >= 0:
if source['resolution_diff'] >= 0:
selected_source = source
break
elif WANTED_RESOLUTION_PREFERENCE == 'exact_or_highest_lower':
for source in sources:
if source[2] <= 0:
if source['resolution_diff'] <= 0:
selected_source = source
break
else:
Expand All @@ -236,9 +239,12 @@ def getWantedResolutionPlaylist(self, url):
self.logger.error("Couldn't select a resolution")
return None

if selected_source[1][1] != 0:
self.logger.info(f'Selected {selected_source[1][0]}x{selected_source[1][1]} resolution')
selected_source_url = selected_source[0]
if selected_source['resolution'][1] != 0:
frame_rate = ''
if selected_source['frame_rate'] is not None and selected_source['frame_rate'] != 0:
frame_rate = f" {selected_source['frame_rate']}fps"
self.logger.info(f"Selected {selected_source['resolution'][0]}x{selected_source['resolution'][1]}{frame_rate} resolution")
selected_source_url = selected_source['url']
if selected_source_url.startswith("https://"):
return selected_source_url
else:
Expand Down
9 changes: 7 additions & 2 deletions streamonitor/sites/myfreecams.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class MyFreeCams(Bot):
def __init__(self, username):
super().__init__(username)
self.attrs = {}
self.videoUrl = None

def getVideoUrl(self, refresh=False):
if not refresh:
return self.videoUrl

def getVideoUrl(self):
if 'data-cam-preview-model-id-value' not in self.attrs:
return None

Expand Down Expand Up @@ -41,7 +45,8 @@ def getStatus(self):
params = doc.find(class_='campreview-link')
if params:
self.attrs = params.attrs
if self.getVideoUrl():
self.videoUrl = self.getVideoUrl(refresh=True)
if self.videoUrl:
return Bot.Status.PUBLIC
else:
return Bot.Status.PRIVATE
Expand Down

0 comments on commit bf8a5fd

Please sign in to comment.