-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils refactor, extensions, logging, tweaks
- Loading branch information
1 parent
710a930
commit 69a9ee5
Showing
9 changed files
with
214 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
ffmpeg-python==0.2.0 | ||
termcolor==1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import logging | ||
|
||
|
||
def configure_logger(log_level): | ||
numeric_level = getattr(logging, log_level.upper(), None) | ||
if not isinstance(numeric_level, int): | ||
raise ValueError(f"Invalid log level: {log_level}") | ||
logging.basicConfig( | ||
level=numeric_level, | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
handlers=[logging.StreamHandler()], | ||
) | ||
|
||
|
||
logger = logging.getLogger("space_saver") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import math | ||
import os | ||
|
||
from pathlib import Path | ||
|
||
from utils.logger import logger | ||
|
||
|
||
def convert_size(size_bytes): | ||
if size_bytes == 0: | ||
return "0B" | ||
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | ||
i = int(math.floor(math.log(size_bytes, 1024))) | ||
p = math.pow(1024, i) | ||
s = round(size_bytes / p, 2) | ||
return f"{s} {size_name[i]}" | ||
|
||
|
||
def print_file_sizes(original_size, new_size): | ||
size_reduction = ((original_size - new_size) / original_size) * 100 | ||
logger.info( | ||
f"Original file size: {convert_size(original_size)}\n" | ||
f"New mp4 file size: {convert_size(new_size)}\n" | ||
f"Size reduction: {int(size_reduction)}%" | ||
) | ||
|
||
|
||
def check_file_type(file, extensions): | ||
file_extension = Path(file).suffix.lower()[1:] | ||
if file_extension not in extensions: | ||
logger.warning( | ||
f'Skipping file "{Path(file).name}", it doesn\'t have a valid extension' | ||
) | ||
return False | ||
return True | ||
|
||
|
||
def after_conversion_clean_up( | ||
input_file_path, original_size, output_file_path, start_time, end_time | ||
): | ||
if os.path.isfile(input_file_path): | ||
os.remove(input_file_path) | ||
new_size = os.path.getsize(output_file_path) | ||
os.chmod(output_file_path, 0o755) | ||
space_saved = original_size - new_size | ||
print_file_sizes(original_size, new_size) | ||
hours, rem = divmod(end_time - start_time, 3600) | ||
minutes, seconds = divmod(rem, 60) | ||
formatted_time = "{:0>2}:{:0>2}:{:05.2f}".format( | ||
int(hours), int(minutes), seconds | ||
) | ||
logger.info(f"Time elapsed: {formatted_time}") | ||
return space_saved |
Oops, something went wrong.