File size: 2,772 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import time

from pyrogram.types import Message
from PIL import Image
from Hellbot.core import Config

from .tools import runcmd


async def convert_to_gif(file: str, is_video: bool = False) -> str:
    resultFileName = f"gif_{round(time.time())}.mp4"

    if is_video:
        cmd = f"ffmpeg -i '{file}' -c copy '{resultFileName}'"
    else:
        cmd = f"lottie_convert.py '{file}' '{resultFileName}'"

    await runcmd(cmd)

    return resultFileName


async def tgs_to_png(file: str) -> str:
    resultFileName = f"png_{round(time.time())}.png"

    cmd = f"lottie_convert.py '{file}' '{resultFileName}'"

    await runcmd(cmd)

    return resultFileName


async def image_to_sticker(file: str, max_size: tuple = (512, 512)) -> tuple[bool, str]:
    try:
        with Image.open(file) as img:
            original_width, original_height = img.size

            new_width = min(original_width, max_size[0])
            new_height = min(original_height, max_size[1])

            if original_width > max_size[0] or original_height > max_size[1]:
                img = img.resize((new_width, new_height), Image.LANCZOS)

            file_name = f"sticker_{int(time.time())}.png"
            img.save(file_name, "PNG")

        return True, file_name

    except Exception as e:
        return False, str(e)


async def video_to_png(

    file: str, duration: float, output: str = None

) -> tuple[str, bool]:
    resultFileName = output or f"{os.path.basename(file)}.png"
    cut_at = duration // 2

    cmd = f"ffmpeg -ss {cut_at} -i '{file}' -vframes 1 '{resultFileName}'"

    _, err, _, _ = await runcmd(cmd)
    if err:
        return err, False

    return resultFileName, True


async def video_to_sticker(file: Message) -> tuple[str, bool]:
    try:
        if file.animation:
            width, height = file.animation.width, file.animation.height
        elif file.video:
            width, height = file.video.width, file.video.height
        else:
            return "Unsupported media type.", False

        file_path = await file.download(Config.TEMP_DIR)
        output_path = os.path.join(Config.TEMP_DIR, "videoSticker.webm")

        if height > width:
            scale_params = f"scale=-1:512"
        else:
            scale_params = f"scale=512:-1"

        cmd = (
            f"ffmpeg -i {file_path} "
            f"-vf fps=30,{scale_params} -t 3 -c:v libvpx-vp9 -b:v 256k -an -pix_fmt yuv420p -auto-alt-ref 0 -loop 0 "
            f"-f webm {output_path}"
        )

        await runcmd(cmd)
        os.remove(file_path)

        return output_path, True

    except Exception as e:
        return f"Error during conversion: {e}", False