Skip to content

Commit

Permalink
feat: v0.1.1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jessielw committed Sep 1, 2023
1 parent 73abd72 commit 935ebf9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
24 changes: 17 additions & 7 deletions deezy/audio_encoders/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import shutil
import os
from pathlib import Path
from typing import Union
from deezy.exceptions import (
AutoChannelDetectionError,
ChannelMixError,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion deezy/audio_encoders/dee/dd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion deezy/audio_encoders/dee/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions deezy/track_info/audio_track_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class AudioTrackInfo:
auto_name = None
fps = None
recommended_free_space = None
duration = None
sample_rate = None
bit_depth = None
Expand Down
31 changes: 31 additions & 0 deletions deezy/track_info/mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion deezy/utils/_version.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 935ebf9

Please sign in to comment.