python.video_converter_v3/app/class_media_file.py

175 lines
No EOL
6 KiB
Python

import os
import math
class Media:
_id_counter = 0
def __init__(self, path, streams_video, streams_audio, streams_subtitle, streams_format):
# misc
self.id = Media._id_counter
# source
self.source_file: str = path
self.source_file_name: str = os.path.basename(self.source_file)
self.source_duration: int = self.time_in_sec(streams_video[0]["tags"].get("DURATION" or "duration", "00:00:00"))
self.source_size: int = 0
self.source_frame_rate: int = self.frame_rate(streams_video)
self.source_frames_total: int = self.source_frame_rate * self.source_duration
self.source_time: int = 0
# target
self.target_file: str = f"{path.rsplit(".", 1)[0]}.webm"
self.target_file_name: str = os.path.basename(self.target_file)
self.target_size: int = 0
# process
self.status = None
self.process_start: int = 0
self.process_end: int = 0
self.process_time: int = 0
self.process_size: int = 0
self.process_frames: int = 0
self.process_time_remaining: int = 0
# statistic
self.stat_fps: list = [0, 0]
self.stat_bitrate: list = [0, 0]
self.stat_quantizer: list = [0, 0]
self.stat_speed: list = [0, 0]
# raw
self.streams_video = streams_video
self.streams_audio = streams_audio
self.streams_subtitle = streams_subtitle
self.streams_format = streams_format
# --------------------------------------------------------------------------------------------------------------
Media._id_counter += 1
def __str__(self):
def stream_output(stream_list):
count = 1
string = ""
for video_stream in stream_list:
string += f"{video_stream.get("codec_type").capitalize()} {count}" if video_stream.get(
"codec_type") else "Format"
for key, value in video_stream.items():
string += f" -- {key}: {value}"
string += "\n"
count += 1
return string
# Ausgabe
output_string = f"\n{self.source_file}\n"
output_string += "------------------------------------\n"
output_string += stream_output(self.streams_format)
output_string += "------------------------------------\n"
output_string += stream_output(self.streams_video)
output_string += "------------------------------------\n"
output_string += stream_output(self.streams_audio)
output_string += "------------------------------------\n"
output_string += stream_output(self.streams_subtitle)
output_string += "------------------------------------\n"
output_string += f"{self.target_file}\n"
output_string += "------------------------------------\n"
output_string += f"{self.id} -- {self.status}"
output_string += "\n************************************\n"
return output_string
@staticmethod
def to_dict():
return "Fertig mit der Welt"
def to_dict_stat(self):
return {self.id: {
# source
"source_file_name": self.source_file_name,
"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_name": self.target_file_name,
"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
def frame_rate(video_streams):
var_frame_rate = video_streams[0].get("r_frame_rate", "0/0").split("/")
if int(var_frame_rate[1]) > 0:
int_frame_rate = round(int(var_frame_rate[0]) / int(var_frame_rate[1]))
else:
int_frame_rate = 0
return int_frame_rate
def time_remaining(self):
if self.stat_fps[0] > 0:
var_time_remaining = (self.source_frames_total - self.process_frames) / (self.stat_fps[0] / self.stat_fps[1])
self.process_time_remaining = round(var_time_remaining)
elif self.stat_fps[0] == 0:
self.process_time_remaining = 0
return self.process_time_remaining
@staticmethod
def format_time(seconds):
days = round(seconds // (24 * 3600))
seconds %= (24 * 3600)
if days:
d = (f"{days} Tag"
f"")
else:
d = ""
hours = round(seconds // 3600)
seconds %= 3600
if hours:
h = f"{hours} Stunden"
else:
h = ""
minutes = math.ceil(seconds // 60)
seconds %= 60
if minutes:
m = f"{minutes} Minuten"
else:
m = ""
return f"{d} {h} {m}"
# Data convert
@staticmethod
def time_in_sec(time_str: str) -> int:
parts = time_str.split(":")
if len(parts) == 1: # Falls nur Sekunden mit Nachkommastellen vorliegen
return int(float(parts[0])) # Erst in float, dann in int umwandeln
if len(parts) == 3: # Normales HH:MM:SS-Format
h, m, s = map(float, parts) # In float umwandeln, falls Nachkommastellen im Sekundenwert sind
return int(h * 3600 + m * 60 + s) # Alles in Sekunden umrechnen
raise ValueError(f"Ungültiges Zeitformat: {time_str}")