From df96783c97c32ec82e479209587626611a0dd3c0 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 9 Oct 2020 08:28:42 -0400 Subject: [PATCH] Add m3u8 selector --- fHDHR/tools/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/fHDHR/tools/__init__.py b/fHDHR/tools/__init__.py index 19a3459..4c0940c 100644 --- a/fHDHR/tools/__init__.py +++ b/fHDHR/tools/__init__.py @@ -3,6 +3,7 @@ import sys import ast import requests import xml.etree.ElementTree +import m3u8 UNARY_OPS = (ast.UAdd, ast.USub) BINARY_OPS = (ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Mod) @@ -14,6 +15,26 @@ def clean_exit(): os._exit(0) +def m3u8_beststream(m3u8_url): + bestStream = None + videoUrlM3u = m3u8.load(m3u8_url) + if len(videoUrlM3u.playlists) > 0: + for videoStream in videoUrlM3u.playlists: + if bestStream is None: + bestStream = videoStream + elif ((videoStream.stream_info.resolution[0] > bestStream.stream_info.resolution[0]) and + (videoStream.stream_info.resolution[1] > bestStream.stream_info.resolution[1])): + bestStream = videoStream + elif ((videoStream.stream_info.resolution[0] == bestStream.stream_info.resolution[0]) and + (videoStream.stream_info.resolution[1] == bestStream.stream_info.resolution[1]) and + (videoStream.stream_info.bandwidth > bestStream.stream_info.bandwidth)): + bestStream = videoStream + if bestStream is not None: + return bestStream.absolute_uri + else: + return m3u8_url + + def sub_el(parent, name, text=None, **kwargs): el = xml.etree.ElementTree.SubElement(parent, name, **kwargs) if text: