diff --git a/app/main.py b/app/main.py index 31654db..17395ea 100755 --- a/app/main.py +++ b/app/main.py @@ -27,17 +27,10 @@ convert_task = 0 # Settings language = ["ger", "eng"] subtitle_codec_blacklist = ["hdmv_pgs_subtitle", "dvd_subtitle"] + process_count = 1 - -# Test -#obj_file_test = vc.Video("/mnt/Media/21 - Spielfilme M/Star Trek/Star Trek 9 - Der Aufstand (1998) 1080p/Star Trek 9 - Der Aufstand (1998) 1080p.mkv") -#video_files = {"/mnt/Media/21 - Spielfilme M/Star Trek/Star Trek 9 - Der Aufstand (1998) 1080p/Star Trek 9 - Der Aufstand (1998) 1080p.mkv": obj_file_test} - video_files = {} -#pattern = r"(/[^:]+?\.(?:mkv|webm|avi|mp4))" -pattern = r"(?<=\.mkv\s|\.mp4\s|\.avi\s)|(?<=\.webm\s)" - progress = {} async def queue_video(): global convert_task @@ -66,14 +59,16 @@ async def get_ffprobe(select, obj): print(json_data) duration = json_data.get("format", {"duration": 999}).get("duration") - obj.duration = float(duration) if duration.isdigit() else 0.0 + if duration: + if duration.replace(".","", 1).isdigit(): + obj.duration = round(float(duration)) + else: + obj.duration = 0 return json_data except Exception as e: convert_task -= 1 await queue_video() - obj.error.append(f"ffprobe ---- {e}") - print(obj.error) async def video_convert(obj): global convert_task @@ -148,8 +143,6 @@ async def read_output(): while True: active_processes = [obj for obj in video_files.values() if obj.progress] - print(active_processes) - if not active_processes: await asyncio.sleep(10) # Kein aktives Video -> kurz warten continue @@ -162,13 +155,14 @@ async def read_output(): print(line_decoded) obj.extract_convert_data(line_decoded) - print(obj.duration) json_data = json.dumps(obj.to_dict()) await queue.put(json_data) @app.post("/") async def receive_video_file(data: dict): + pattern = r"(?<=\.mkv\s|\.mp4\s|\.avi\s)|(?<=\.webm\s)" + file_paths = data.get("files", [])[0] var_list_file = re.split(pattern, file_paths) diff --git a/app/video_class.py b/app/video_class.py index 94f2276..9662b36 100644 --- a/app/video_class.py +++ b/app/video_class.py @@ -17,7 +17,7 @@ class Video: self.speed = 0 self.finished = 0 self.progress = None - self.duration = 1 + self.duration = 0 self.loading = 0 self.error = [] @@ -55,7 +55,7 @@ class Video: self.size = self.convert_kb_mb(size[0]) if size else 0 time = re.findall(r"time=\s*(\d+:\d+:\d+)", line_decoded) - time_v = time[0] if time else "" + time_v = time[0] if time else "00:00:00" self.time = self.time_in_sec(time_v) bitrate = re.findall(r"bitrate=\s*(\d+)", line_decoded) @@ -65,20 +65,18 @@ class Video: self.speed = speed[0] if speed else 0 @staticmethod - def is_time_format(s): - return bool(re.fullmatch(r"\d{2}:\d{2}:\d{2}(\.\d{1,2})?", s)) - - def time_in_sec(self, time_str): - if self.is_time_format(time_str): - try: - t = datetime.strptime(time_str, "%H:%M:%S.%f") - return t.hour * 3600 + t.minute * 3600 + t.second + t.microsecond / 1_000_000 - except ValueError: - return None + def time_in_sec(time_str): + hs_ms_s = re.findall(r"\s*(\d+):(\d+):(\d+)", time_str) + if len(hs_ms_s[0]) >= 3: + if hs_ms_s[0][0].isdigit() and hs_ms_s[0][1].isdigit() and hs_ms_s[0][2].isdigit(): + try: + return int(hs_ms_s[0][0]) * 3600 + int(hs_ms_s[0][1]) * 3600 + int(hs_ms_s[0][2]) + except ValueError: + return 0 def calc_loading(self): - if self.duration != 0: - self.loading = round(self.time / self.duration * 100, None) + if self.duration.is_integer(): + self.loading = round(self.time / self.duration * 100) else: self.loading = 0