-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert_pyrlottie.py
78 lines (65 loc) · 2.12 KB
/
convert_pyrlottie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Conversion functionality for animated stickers.
implements the conversion functionality for the pyrlottie backend. exposing a
public function called `convertAnimated`, which is used to perform the conversion.
"""
from __future__ import annotations
import asyncio
from pathlib import Path
import pyrlottie
def convertAnimated(
swd: Path,
_threads: int = 4,
fps: int = 20,
scale: float = 1,
_formats: set[str] | None = None,
) -> int:
"""Convert animated stickers, over a number of threads, at a given framerate, scale and to a
set of formats.
:param Path swd: The sticker working directory (e.g., downloads/packName).
:param int _threads: This is ignored for the pyrlottie backend
:param int fps: framerate of the converted sticker, affecting optimization and
quality (default: 20)
:param float scale: Scale factor for up/downscaling images, affecting optimization and
quality (default: 1).
:param set[str] | None _formats: This is ignored for the pyrlottie backend
:return int: Number of stickers successfully converted.
"""
converted = 0
(swd / "webp").mkdir(parents=True, exist_ok=True)
(swd / "gif").mkdir(parents=True, exist_ok=True)
# here we are going to assume that the raw image is 60 fps as pyrlottie does not have a way
# of setting the fps
frameSkip = 0
if fps < 45: # bisector of 30 and 60
frameSkip = 1 # ~30fps
if fps < 25: # bisector of 20 and 30
frameSkip = 2 # ~20fps
if fps < 18: # bisector of 15 and 20
frameSkip = 3 # ~15fps
if fps < 13: # 12fps and below
frameSkip = 4 # ~12fps
def doConvMultLottie(filemaps: list[pyrlottie.FileMap], frameSkip: int, scale: float) -> int:
return (
len(
asyncio.get_event_loop().run_until_complete(
pyrlottie.convMultLottie(filemaps, frameSkip=frameSkip, scale=scale)
)
)
// 2
)
converted += doConvMultLottie(
filemaps=[
pyrlottie.FileMap(
pyrlottie.LottieFile(stckr.absolute().as_posix()),
{
stckr.absolute().as_posix().replace("tgs", "gif"),
stckr.absolute().as_posix().replace("tgs", "webp"),
},
)
for stckr in swd.glob("**/*.tgs")
],
frameSkip=frameSkip,
scale=scale,
)
return converted