Маленькие правки
This commit is contained in:
27
main.py
27
main.py
@ -1,5 +1,6 @@
|
||||
from logging import getLogger
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
|
||||
import ffmpeg
|
||||
from ffmpeg.nodes import FilterableStream
|
||||
@ -11,7 +12,15 @@ PATH = Path(__file__).parent
|
||||
INPUT_DIR = PATH / 'input'
|
||||
OUTPUT_DIR = PATH / 'output'
|
||||
|
||||
OUTPUT_ARGS = {'vcodec': 'libvpx-vp9', 'pix_fmt': 'yuva420p', 'loglevel': 'error', 'y': None}
|
||||
OUTPUT_ARGS = {
|
||||
'vcodec': 'libvpx-vp9',
|
||||
'pix_fmt': 'yuva420p',
|
||||
'row-mt': 1,
|
||||
'format': 'webm',
|
||||
'loglevel': 'error',
|
||||
'an': None,
|
||||
'y': None,
|
||||
}
|
||||
|
||||
|
||||
logger = getLogger(__name__)
|
||||
@ -32,6 +41,7 @@ def compress(input_file: Path, output_file: Path, sticker_width: int, bitrate: i
|
||||
stream = ffmpeg.filter(
|
||||
stream, 'pad', sticker_width, sticker_width, '(ow-iw)/2', '(oh-ih)/2', color='0x00000000'
|
||||
)
|
||||
stream = ffmpeg.filter(stream, 'loop', 0, 1)
|
||||
|
||||
if input_file.suffix == '.gif':
|
||||
duration = float(ffmpeg.probe(input_file)['streams'][0]['duration'])
|
||||
@ -48,7 +58,11 @@ def compress(input_file: Path, output_file: Path, sticker_width: int, bitrate: i
|
||||
args['video_bitrate'] = f'{bitrate}k'
|
||||
|
||||
output = ffmpeg.output(stream, filename=file_output, **args)
|
||||
|
||||
try:
|
||||
ffmpeg.run(output)
|
||||
except Exception:
|
||||
exit(1)
|
||||
|
||||
file_size = file_output.stat().st_size
|
||||
|
||||
@ -56,12 +70,7 @@ def compress(input_file: Path, output_file: Path, sticker_width: int, bitrate: i
|
||||
if not bitrate:
|
||||
bitrate = int(int(ffmpeg.probe(output_file)['format']['bit_rate']) / 1000)
|
||||
|
||||
bitrate_offset = 100
|
||||
|
||||
if file_size / 256 * 1024 > 2:
|
||||
bitrate_offset = 100 * (file_size / (256 * 1024))
|
||||
|
||||
bitrate = int(bitrate - bitrate_offset)
|
||||
bitrate = int((256 * 1024) / (file_size / bitrate))
|
||||
|
||||
logger.info(
|
||||
f'File size is too high [{int(file_size / 1024)} KB], reducing to {bitrate}k...'
|
||||
@ -90,6 +99,8 @@ def main():
|
||||
case _:
|
||||
STICKER_WIDTH = 512
|
||||
|
||||
started_at = time()
|
||||
|
||||
for file in INPUT_DIR.iterdir():
|
||||
logger.info(f'Processing {file.name}...')
|
||||
|
||||
@ -102,6 +113,8 @@ def main():
|
||||
|
||||
compress(file_path, file_output, STICKER_WIDTH)
|
||||
|
||||
logger.info(f'Finished in {round(time() - started_at, 2)} seconds')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
configure_logging()
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "telegramstickersgenerator"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
description = "A simple tool to convert any images and/or gifs to webm extension so that they can be used to create animated sticker/emoji packs in Telegram."
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
@ -20,6 +20,7 @@ _git = "git add ."
|
||||
_lint = "pre-commit run --all-files"
|
||||
|
||||
lint = ["_git", "_lint"]
|
||||
run = "uv run main.py"
|
||||
|
||||
[tool.ruff]
|
||||
target-version = "py313"
|
||||
|
||||
Reference in New Issue
Block a user