File Statistik eingebaut, Log Rotation

This commit is contained in:
Eduard Wisch 2025-04-07 21:33:51 +02:00
parent 65e87ce997
commit 3c18dd7e18
5 changed files with 72 additions and 10 deletions

View file

@ -2,6 +2,7 @@ import logging
import asyncio import asyncio
import time import time
from app.class_file_convert_read_out import Process from app.class_file_convert_read_out import Process
from app.class_media_file_stat import Stat
class Convert: class Convert:
def __init__(self, websocket, cfg, obj_path): def __init__(self, websocket, cfg, obj_path):
@ -40,6 +41,7 @@ class Convert:
"""Startet die Videokonvertierung asynchron.""" """Startet die Videokonvertierung asynchron."""
obj_process = Process(self.obj_websocket) obj_process = Process(self.obj_websocket)
obj_stat = Stat(obj)
obj.convert_start = time.time() obj.convert_start = time.time()
command = self.convert_cmd(obj) command = self.convert_cmd(obj)
@ -79,6 +81,7 @@ class Convert:
self.active_process.discard(obj) self.active_process.discard(obj)
self.active_tasks.discard(obj) self.active_tasks.discard(obj)
self.obj_path.save_paths() self.obj_path.save_paths()
obj_stat.save_stat()
obj.convert_end = time.time() obj.convert_end = time.time()

View file

@ -31,8 +31,7 @@ class Process:
self.process_line_extract(obj, line_decoded) self.process_line_extract(obj, line_decoded)
# in json umwandeln await self.obj_websocket.send_websocket(self.to_dict())
#await self.obj_websocket.send_websocket(self.to_dict())
if self.line_empty > 30: if self.line_empty > 30:
break break

View file

@ -82,6 +82,36 @@ class Media:
def to_dict(): def to_dict():
return "Fertig mit der Welt" return "Fertig mit der Welt"
def to_dict_stat(self):
return {self.id: {
# source
"source_file": self.source_file,
"source_duration": self.source_duration,
"source_size": self.source_size,
"source_frame_rate": self.source_frame_rate,
"source_frames_total": self.source_frames_total,
"source_time": self.source_time,
# target
"target_file": self.target_file,
"target_size": self.target_size,
# process
"status": self.status,
"process_start": self.process_start,
"process_end": self.process_end,
"process_time": self.process_time,
"process_size": self.process_size,
"process_frames": self.process_frames,
"process_time_remaining": self.process_time_remaining,
# statistic
"stat_fps": self.stat_fps,
"stat_bitrate": self.stat_bitrate,
"stat_quantizer": self.stat_quantizer,
"stat_speed": self.stat_speed
}}
@staticmethod @staticmethod
def frame_rate(video_streams): def frame_rate(video_streams):
var_frame_rate = video_streams[0].get("r_frame_rate", "0/0").split("/") var_frame_rate = video_streams[0].get("r_frame_rate", "0/0").split("/")

View file

@ -0,0 +1,26 @@
import yaml
import os
class Stat:
def __init__(self, obj):
self.obj = obj
def save_stat(self):
pfad = "app/cfg/statistic.yaml"
# Bestehende Daten laden
if os.path.exists(pfad):
with open(pfad, "r", encoding="utf8") as file:
daten = yaml.safe_load(file) or {}
else:
daten = {}
# Videosammlung initialisieren, falls nötig
daten.setdefault("videos", {})
# Neuen Eintrag hinzufügen
daten["videos"].update(self.obj.to_dict_stat())
# Datei mit aktualisierten Daten speichern
with open(pfad, "w", encoding="utf8") as file:
yaml.dump(daten, file, default_flow_style=False, indent=4, allow_unicode=True)

View file

@ -1,9 +1,9 @@
import asyncio
import websockets import websockets
import json import json
import logging import logging
from websockets import InvalidUpgrade, ConnectionClosed, ConnectionClosedError
from app.class_settings import Settings from app.class_settings import Settings
from app.class_file_path import Path from app.class_file_path import Path
from app.class_file_convert import Convert from app.class_file_convert import Convert
@ -12,6 +12,7 @@ var_convert_active = False
class Server: class Server:
def __init__(self): def __init__(self):
self.websocket_send = 0
self.websocket = None self.websocket = None
obj_settings = Settings() obj_settings = Settings()
obj_settings.set_logging() obj_settings.set_logging()
@ -22,9 +23,14 @@ class Server:
async def send_websocket(self, message): async def send_websocket(self, message):
try: try:
await self.websocket.send(message) await self.websocket.send(json.dumps(message))
except websockets.exceptions.ConnectionClosed: except websockets.exceptions.ConnectionClosed:
logging.warning("No websocket client connected!") if self.websocket_send == 0:
logging.warning("No websocket client connected!")
self.websocket_send += 1
if self.websocket_send == 100:
self.websocket_send = 0
async def handle_client(self, websocket): async def handle_client(self, websocket):
self.websocket = websocket self.websocket = websocket
@ -51,11 +57,9 @@ class Server:
#response = f"Server antwortet: {message.upper()}" #response = f"Server antwortet: {message.upper()}"
#await websocket.send(response) #await websocket.send(response)
except websockets.exceptions.ConnectionClosedError: except (ConnectionClosedError, InvalidUpgrade):
pass pass
except websockets.exceptions.InvalidUpgrade: except ConnectionClosed:
pass
except websockets.exceptions.ConnectionClosed:
print("Server sagt: Client getrennt") print("Server sagt: Client getrennt")
@staticmethod @staticmethod