Skip to content

Commit

Permalink
Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
laggykiller committed Mar 27, 2024
1 parent 8e597a9 commit 592b1ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
13 changes: 6 additions & 7 deletions src/sticker_convert/converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
import os
from decimal import ROUND_HALF_UP, Decimal
from fractions import Fraction
from io import BytesIO
from math import ceil, floor
Expand All @@ -15,7 +14,7 @@
from sticker_convert.job_option import CompOption
from sticker_convert.utils.callback import Callback, CallbackReturn, CbQueueItemType
from sticker_convert.utils.files.cache_store import CacheStore
from sticker_convert.utils.media.codec_info import CodecInfo
from sticker_convert.utils.media.codec_info import CodecInfo, rounding
from sticker_convert.utils.media.format_verify import FormatVerify

if TYPE_CHECKING:
Expand Down Expand Up @@ -44,10 +43,6 @@
)


def rounding(value: float) -> Decimal:
return Decimal(value).quantize(0, ROUND_HALF_UP)


def get_step_value(
max_step: Optional[int],
min_step: Optional[int],
Expand Down Expand Up @@ -114,7 +109,11 @@ def yuva_to_rgba(frame: "VideoFrame") -> "np.ndarray[Any, Any]":
yuv_array[:, :, 1:].clip(16, 240).astype(yuv_array.dtype) - 128 # type: ignore
)

rgb_array = np.matmul(yuv_array, YUV_RGB_MATRIX.T).clip(0, 255).astype("uint8")
rgb_array = (
cast("np.ndarray[Any, Any]", np.matmul(yuv_array, YUV_RGB_MATRIX.T))
.clip(0, 255)
.astype("uint8")
)

return np.concatenate((rgb_array, a), axis=2)

Expand Down
22 changes: 8 additions & 14 deletions src/sticker_convert/utils/media/codec_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,19 @@ def get_five_dec_place(value: float) -> str:
return str(value).split(".")[1][:5].ljust(5, "0")


def is_fraction_of_x(value: float, x: int) -> bool:
valid_ends = (get_five_dec_place(i / x) for i in range(1, x))
value_dec = get_five_dec_place(value)
return any(i for i in valid_ends if value_dec == i)


def is_recurring_dec_int(value: float) -> bool:
# 0.99999... == 1
return True if get_five_dec_place(value) == "99999" else False
def likely_int(value: float) -> bool:
if isinstance(value, int):
return True
return True if get_five_dec_place(value) in ("99999", "00000") else False


def durations_gcd(*durations: Union[int, float]) -> Union[float, Fraction]:
if any(i for i in durations if isinstance(i, float)):
if all(i for i in durations if isinstance(i, int) or is_recurring_dec_int(i)):
if all(i for i in durations if likely_int(i)):
return Fraction(gcd(*(int(rounding(i)) for i in durations)), 1)
for x in (3, 6, 7, 9):
if all(
i for i in durations if isinstance(i, int) or is_fraction_of_x(i, x)
):
# Test for denominators that can produce recurring decimal
for x in (3, 6, 7, 9, 11, 13):
if all(likely_int(i * x) for i in durations):
return Fraction(gcd(*(int(rounding(i * x)) for i in durations)), x)
else:
return min(durations)
Expand Down

0 comments on commit 592b1ed

Please sign in to comment.