|
|
|
@ -22,6 +22,7 @@ class Resolution:
|
|
|
|
def __init__(self, width: int, height: int):
|
|
|
|
def __init__(self, width: int, height: int):
|
|
|
|
self.width = width
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.height = height
|
|
|
|
|
|
|
|
self.ratio = self.width / self.height
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"Resolution(width={self.width},height={self.height})"
|
|
|
|
return f"Resolution(width={self.width},height={self.height})"
|
|
|
|
@ -29,11 +30,15 @@ class Resolution:
|
|
|
|
def __str__(self) -> str:
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.width}x{self.height}"
|
|
|
|
return f"{self.width}x{self.height}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __eq__(self, o: object) -> bool:
|
|
|
|
|
|
|
|
return self.width == o.width and self.height == o.height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Ratio:
|
|
|
|
class Ratio:
|
|
|
|
def __init__(self, x: int, y: int):
|
|
|
|
def __init__(self, x: int, y: int):
|
|
|
|
self.x = x
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
self.ratio = x / y
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"Ratio(x={self.x},y={self.y})"
|
|
|
|
return f"Ratio(x={self.x},y={self.y})"
|
|
|
|
@ -41,6 +46,8 @@ class Ratio:
|
|
|
|
def __str__(self) -> str:
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.x}x{self.y}"
|
|
|
|
return f"{self.x}x{self.y}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __eq__(self, o: object) -> bool:
|
|
|
|
|
|
|
|
return self.ratio == o.ratio
|
|
|
|
|
|
|
|
|
|
|
|
class AudioInfo:
|
|
|
|
class AudioInfo:
|
|
|
|
def __init__(self, audio_dict: Dict[str, str]):
|
|
|
|
def __init__(self, audio_dict: Dict[str, str]):
|
|
|
|
@ -102,17 +109,29 @@ class MenuInfo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediaInfo:
|
|
|
|
class MediaInfo:
|
|
|
|
def __init__(self, path: Path):
|
|
|
|
def __init__(self):
|
|
|
|
self.path = path
|
|
|
|
self.path: Path
|
|
|
|
procinfo = Popen([Config.MEDIAINFO_BINARY, path, "--Output=JSON"], stdout=PIPE)
|
|
|
|
|
|
|
|
stdout, _ = procinfo.communicate()
|
|
|
|
|
|
|
|
data = json.loads(stdout.decode('utf-8'))["media"]["track"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.general: Optional[Dict[str, Any]] = None
|
|
|
|
self.general: Optional[Dict[str, Any]] = None
|
|
|
|
self.video: Optional[Dict[str, Any]] = None
|
|
|
|
self.video: Optional[Dict[str, Any]] = None
|
|
|
|
self.audio: List[Dict[str, Any]] = []
|
|
|
|
self.audio: List[Dict[str, Any]] = []
|
|
|
|
self.menu_data: Optional[Dict[str, Any]] = None
|
|
|
|
self.menu_data: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.codec: str
|
|
|
|
|
|
|
|
self.bitrate: int
|
|
|
|
|
|
|
|
self.framerate: float
|
|
|
|
|
|
|
|
self.duration: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.resolution: Resolution
|
|
|
|
|
|
|
|
self.display_aspect_ratio: Ratio
|
|
|
|
|
|
|
|
self.keep_display_aspect: bool
|
|
|
|
|
|
|
|
self.display_width: float
|
|
|
|
|
|
|
|
self.pixel_aspect_ratio: Ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_file(self, path: Path):
|
|
|
|
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
procinfo = Popen([Config.MEDIAINFO_BINARY, path, "--Output=JSON"], stdout=PIPE)
|
|
|
|
|
|
|
|
stdout, _ = procinfo.communicate()
|
|
|
|
|
|
|
|
data = json.loads(stdout.decode('utf-8'))["media"]["track"]
|
|
|
|
for track in data:
|
|
|
|
for track in data:
|
|
|
|
if track["@type"] == "General":
|
|
|
|
if track["@type"] == "General":
|
|
|
|
self.general = track
|
|
|
|
self.general = track
|
|
|
|
@ -123,84 +142,61 @@ class MediaInfo:
|
|
|
|
elif track["@type"] == "Menu":
|
|
|
|
elif track["@type"] == "Menu":
|
|
|
|
self.menu_data = track
|
|
|
|
self.menu_data = track
|
|
|
|
|
|
|
|
|
|
|
|
# print(json.dumps(self.video, indent=2))
|
|
|
|
self.determine_information_properties()
|
|
|
|
|
|
|
|
self.determine_aspect_properties()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def determine_information_properties(self):
|
|
|
|
def codec(self) -> str:
|
|
|
|
self.codec = self.video["Format"]
|
|
|
|
return self.video["Format"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def bitrate(self) -> int:
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
bitrate = self.video["BitRate"]
|
|
|
|
bitrate = self.video["BitRate"]
|
|
|
|
except KeyError:
|
|
|
|
except KeyError:
|
|
|
|
bitrate = self.general["OverallBitRate"]
|
|
|
|
bitrate = self.general["OverallBitRate"]
|
|
|
|
|
|
|
|
self.bitrate = int(bitrate) // 1000
|
|
|
|
|
|
|
|
|
|
|
|
ibitrate = int(bitrate) // 1000
|
|
|
|
|
|
|
|
return ibitrate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def framerate(self) -> float:
|
|
|
|
|
|
|
|
keys = ["FrameRate_Maximum", "FrameRate", "FrameRate_Nominal"]
|
|
|
|
keys = ["FrameRate_Maximum", "FrameRate", "FrameRate_Nominal"]
|
|
|
|
for key in keys:
|
|
|
|
for key in keys:
|
|
|
|
if key in self.video:
|
|
|
|
if key in self.video:
|
|
|
|
return float(self.video[key])
|
|
|
|
self.framerate = float(self.video[key])
|
|
|
|
print(json.dumps(self.general, indent=2))
|
|
|
|
if not self.framerate:
|
|
|
|
print(json.dumps(self.video, indent=2))
|
|
|
|
print(json.dumps(self.general, indent=2))
|
|
|
|
raise Exception("No frame rate for video " + str(self.path))
|
|
|
|
print(json.dumps(self.video, indent=2))
|
|
|
|
|
|
|
|
raise Exception("No frame rate for video " + str(self.path))
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def duration(self) -> str:
|
|
|
|
|
|
|
|
places_to_look = [self.video, self.general]
|
|
|
|
places_to_look = [self.video, self.general]
|
|
|
|
for place in places_to_look:
|
|
|
|
for place in places_to_look:
|
|
|
|
if "Duration" in place:
|
|
|
|
if "Duration" in place:
|
|
|
|
seconds = float(place["Duration"])
|
|
|
|
seconds = float(place["Duration"])
|
|
|
|
td = str(timedelta(seconds=seconds))
|
|
|
|
self.duration = str(timedelta(seconds=seconds))
|
|
|
|
return td # f"{td:0>8}"
|
|
|
|
if not self.duration:
|
|
|
|
raise Exception("No duration for video " + str(self.path))
|
|
|
|
raise Exception("No duration for video " + str(self.path))
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def determine_aspect_properties(self):
|
|
|
|
def resolution(self) -> Resolution:
|
|
|
|
|
|
|
|
width = int(self.video["Width"])
|
|
|
|
width = int(self.video["Width"])
|
|
|
|
height = int(self.video["Height"])
|
|
|
|
height = int(self.video["Height"])
|
|
|
|
|
|
|
|
self.resolution = Resolution(width=width, height=height)
|
|
|
|
|
|
|
|
|
|
|
|
return Resolution(width=width, height=height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def display_aspect_ratio(self) -> Ratio:
|
|
|
|
|
|
|
|
ratio = [float(x) for x in str(self.video["DisplayAspectRatio"]).split(':')]
|
|
|
|
ratio = [float(x) for x in str(self.video["DisplayAspectRatio"]).split(':')]
|
|
|
|
if len(ratio) != 2:
|
|
|
|
if len(ratio) != 2:
|
|
|
|
res = self.resolution
|
|
|
|
if -0.01 <= ratio[0] - self.resolution.ratio <= 0.01:
|
|
|
|
if -0.01 <= ratio[0] - res.width / res.height <= 0.01:
|
|
|
|
frac = Fraction(self.resolution.width, self.resolution.height)
|
|
|
|
frac = Fraction(res.width, res.height)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
new_width = round(res.height * ratio[0])
|
|
|
|
new_width = round(self.resolution.height * ratio[0])
|
|
|
|
frac = Fraction(new_width, res.height)
|
|
|
|
frac = Fraction(new_width, self.resolution.height)
|
|
|
|
ratio = (frac.numerator, frac.denominator)
|
|
|
|
ratio = (frac.numerator, frac.denominator)
|
|
|
|
return Ratio(x=ratio[0], y=ratio[1])
|
|
|
|
self.display_aspect_ratio = Ratio(x=ratio[0], y=ratio[1])
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
self.keep_display_aspect = 0.95 < self.resolution.ratio / self.display_aspect_ratio.ratio < 1.05
|
|
|
|
def keep_display_aspect(self) -> bool:
|
|
|
|
|
|
|
|
ratio = self.display_aspect_ratio
|
|
|
|
|
|
|
|
resolution = self.resolution
|
|
|
|
|
|
|
|
return 0.95 < (resolution.width / resolution.height) / (ratio.x / ratio.y) < 1.05
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def display_width(self) -> float:
|
|
|
|
|
|
|
|
ratio = self.display_aspect_ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not self.keep_display_aspect:
|
|
|
|
if not self.keep_display_aspect:
|
|
|
|
display_width = self.resolution.height * ratio.x / ratio.y
|
|
|
|
self.display_width = self.resolution.height * self.display_aspect_ratio.ratio
|
|
|
|
else:
|
|
|
|
self.pixel_aspect_ratio = self.display_aspect_ratio
|
|
|
|
display_width = float(self.resolution.width)
|
|
|
|
|
|
|
|
return display_width
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def pixel_aspect_ratio(self) -> Ratio:
|
|
|
|
|
|
|
|
if self.keep_display_aspect:
|
|
|
|
|
|
|
|
return Ratio(1.0, 1.0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return Ratio(self.display_aspect_ratio.x, self.resolution.width)
|
|
|
|
self.display_width = float(self.resolution.width)
|
|
|
|
|
|
|
|
self.pixel_aspect_ratio = Ratio(
|
|
|
|
|
|
|
|
self.display_aspect_ratio.x,
|
|
|
|
|
|
|
|
self.resolution.width
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def output_bitrate(self) -> int:
|
|
|
|
def output_bitrate(self) -> int:
|
|
|
|
|