From 935ebf96dfd9a9bcf9f158740f019f4d9e19d108 Mon Sep 17 00:00:00 2001 From: jlw4049 Date: Fri, 1 Sep 2023 19:05:44 -0400 Subject: [PATCH] feat: v0.1.1 feat: significantly enhanced temporary storage detection needs. Will not longer only calculate it based off of file input size. Now detects needed space based on video/audio tracks in the input file and falls back to input size if cannot be detected --- deezy/audio_encoders/base.py | 24 ++++++++++++++------- deezy/audio_encoders/dee/dd.py | 6 +++++- deezy/audio_encoders/dee/ddp.py | 6 +++++- deezy/track_info/audio_track_info.py | 1 + deezy/track_info/mediainfo.py | 31 ++++++++++++++++++++++++++++ deezy/utils/_version.py | 2 +- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/deezy/audio_encoders/base.py b/deezy/audio_encoders/base.py index 8cc6d85..7fef3dc 100644 --- a/deezy/audio_encoders/base.py +++ b/deezy/audio_encoders/base.py @@ -1,6 +1,7 @@ import shutil import os from pathlib import Path +from typing import Union from deezy.exceptions import ( AutoChannelDetectionError, ChannelMixError, @@ -31,7 +32,11 @@ def _check_input_file(input_file: Path): return input_file.exists() @staticmethod - def _check_disk_space(input_file_path: Path, drive_path: Path): + def _check_disk_space( + input_file_path: Path, + drive_path: Path, + recommended_free_space: Union[None, int], + ): """ Check for free space at the temporary directory, rounding to the nearest whole number. If there isn't at least 110% of the size of the input file as free space in the temporary directory, @@ -40,21 +45,26 @@ def _check_disk_space(input_file_path: Path, drive_path: Path): Args: input_file_path (Path): Path to the input file. drive_path (Path): Path to the temporary directory where intermediate files will be stored. + recommended_free_space (None or int): None or calculated free space in bytes. """ - # Get the size of the input file in bytes - input_file_size = os.path.getsize(input_file_path) + # Calculate the required space (110% of the input file size if no recommendation) in bytes + if recommended_free_space: + required_space_bytes = recommended_free_space + else: + # Get the size of the input file in bytes + input_file_size = os.path.getsize(input_file_path) + + required_space_bytes = int(input_file_size * 1.1) # Get free space in bytes in the temporary directory free_space_bytes = shutil.disk_usage(drive_path).free - # Calculate the required space (110% of the input file size) in bytes - required_space_bytes = int(input_file_size * 1.1) - # Check if the required space is available if free_space_bytes < required_space_bytes: raise NotEnoughSpaceError( - "Insufficient storage in the temporary directory to complete the process." + "Insufficient storage in the temporary directory to complete the process. " + f"Calculated required storage (bytes): {required_space_bytes}" ) @staticmethod diff --git a/deezy/audio_encoders/dee/dd.py b/deezy/audio_encoders/dee/dd.py index 231bbe3..c7a9996 100644 --- a/deezy/audio_encoders/dee/dd.py +++ b/deezy/audio_encoders/dee/dd.py @@ -77,7 +77,11 @@ def encode(self, payload: object): temp_dir = self._get_temp_dir(file_input, payload.temp_dir) # check disk space - self._check_disk_space(input_file_path=file_input, drive_path=temp_dir) + self._check_disk_space( + input_file_path=file_input, + drive_path=temp_dir, + recommended_free_space=audio_track_info.recommended_free_space, + ) # temp filename temp_filename = Path(tempfile.NamedTemporaryFile(delete=False).name).name diff --git a/deezy/audio_encoders/dee/ddp.py b/deezy/audio_encoders/dee/ddp.py index e2fc794..b0066d1 100644 --- a/deezy/audio_encoders/dee/ddp.py +++ b/deezy/audio_encoders/dee/ddp.py @@ -77,7 +77,11 @@ def encode(self, payload: object): temp_dir = self._get_temp_dir(file_input, payload.temp_dir) # check disk space - self._check_disk_space(input_file_path=file_input, drive_path=temp_dir) + self._check_disk_space( + input_file_path=file_input, + drive_path=temp_dir, + recommended_free_space=audio_track_info.recommended_free_space, + ) # temp filename temp_filename = Path(tempfile.NamedTemporaryFile(delete=False).name).name diff --git a/deezy/track_info/audio_track_info.py b/deezy/track_info/audio_track_info.py index 6eead01..e93e895 100644 --- a/deezy/track_info/audio_track_info.py +++ b/deezy/track_info/audio_track_info.py @@ -1,6 +1,7 @@ class AudioTrackInfo: auto_name = None fps = None + recommended_free_space = None duration = None sample_rate = None bit_depth = None diff --git a/deezy/track_info/mediainfo.py b/deezy/track_info/mediainfo.py index db12dd6..ecd403e 100644 --- a/deezy/track_info/mediainfo.py +++ b/deezy/track_info/mediainfo.py @@ -119,6 +119,10 @@ def get_track_by_id(self, file_input: Path, track_index: int): # update AudioTrackInfo with needed values audio_info.fps = self._get_fps(mi_object) + audio_info.audio_only = False + audio_info.recommended_free_space = self._recommended_free_space( + mi_object, track_index + ) audio_info.duration = self._get_duration(mi_object, track_index) audio_info.sample_rate = mi_object.audio_tracks[track_index].sampling_rate audio_info.bit_depth = mi_object.audio_tracks[track_index].bit_depth @@ -181,6 +185,33 @@ def _get_fps(mi_object): return mi_track.frame_rate return None + @staticmethod + def _recommended_free_space(mi_object, track_index: int): + """ + Determine the recommended temporary file size needed for processing. + + Args: + mi_object (MediaInfo): A MediaInfo object. + + Returns: + size (int or None): Recommended size in bytes. + """ + selected_audio_track_size = mi_object.audio_tracks[track_index].stream_size + if selected_audio_track_size: + try: + return int(selected_audio_track_size) + except ValueError: + general_track = mi_object.general_tracks[0] + video_streams = general_track.count_of_video_streams + audio_streams = general_track.count_of_audio_streams + + if video_streams and audio_streams: + return int(int(general_track.stream_size) * 0.12) + else: + return int(int(general_track.stream_size) * 1.1) + else: + return None + @staticmethod def _get_duration(mi_object, track_index): """ diff --git a/deezy/utils/_version.py b/deezy/utils/_version.py index 5a83b1f..cdd1f5e 100644 --- a/deezy/utils/_version.py +++ b/deezy/utils/_version.py @@ -1,5 +1,5 @@ # edit this file to control app name and version program_name = "DeeZy" -__version__ = "0.1.0" +__version__ = "0.1.1" developed_by = "jlw4049 and eSTeeM"