Skip to content

Commit

Permalink
Merge pull request #60 from Nubuki-all/gif-sending-fix
Browse files Browse the repository at this point in the history
Allow gifs to be resent as gifs or video
  • Loading branch information
krypton-byte authored Dec 29, 2024
2 parents d0e153e + f34cfc9 commit 293e808
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion neonize/aioze/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,8 @@ async def build_video_message(
caption: Optional[str] = None,
quoted: Optional[neonize_proto.Message] = None,
viewonce: bool = False,
gifplayback: bool = False,
is_gif:bool = False,
) -> Message:
"""
This function is used to build a video message. It uploads a video file, extracts necessary information,
Expand All @@ -905,12 +907,19 @@ async def build_video_message(
:type quoted: Optional[neonize_proto.Message], optional
:param viewonce: A flag indicating if the video message can be viewed only once, defaults to False
:type viewonce: bool, optional
:param gifplayback: Optional. Whether the video should be sent as gif. Defaults to False.
:type gifplayback: bool, optional
:param is_gif: Optional. Whether the video to be sent is a gif. Defaults to False.
:type is_gif: bool, optional
:return: A video message with the given parameters.
:rtype: Message
"""
io = BytesIO(await get_bytes_from_name_or_url_async(file))
io.seek(0)
buff = io.read()
if is_gif:
async with AFFmpeg(file) as ffmpeg:
buff = file = await ffmpeg.gif_to_mp4()
async with AFFmpeg(file) as ffmpeg:
duration = int((await ffmpeg.extract_info()).format.duration)
thumbnail = await ffmpeg.extract_thumbnail()
Expand All @@ -919,6 +928,7 @@ async def build_video_message(
videoMessage=VideoMessage(
URL=upload.url,
caption=caption,
gifPlayback=gifplayback,
seconds=duration,
directPath=upload.DirectPath,
fileEncSHA256=upload.FileEncSHA256,
Expand Down Expand Up @@ -949,6 +959,8 @@ async def send_video(
caption: Optional[str] = None,
quoted: Optional[neonize_proto.Message] = None,
viewonce: bool = False,
gifplayback: bool = False,
is_gif:bool = False,
) -> SendResponse:
"""Sends a video to the specified recipient.
Expand All @@ -962,11 +974,15 @@ async def send_video(
:type quoted: Optional[Message], optional
:param viewonce: Optional. Whether the video should be viewonce. Defaults to False.
:type viewonce: bool, optional
:param gifplayback: Optional. Whether the video should be sent as gif. Defaults to False.
:type gifplayback: bool, optional
:param is_gif: Optional. Whether the video to be sent is a gif. Defaults to False.
:type is_gif: bool, optional
:return: A function for handling the result of the video sending process.
:rtype: SendResponse
"""
return await self.send_message(
to, await self.build_video_message(file, caption, quoted, viewonce)
to, await self.build_video_message(file, caption, quoted, viewonce, gifplayback, is_gif)
)

async def build_image_message(
Expand Down
26 changes: 26 additions & 0 deletions neonize/utils/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,32 @@ async def call(self, cmd: List[str]):
)
return stdout

async def gif_to_mp4(self) -> bytes:
"""
This function convertes a gif to mp4 format.
"""
temp = tempfile.gettempdir() + "/" + time.time().__str__() + ".mp4"
await self.call(
[
"ffmpeg",
"-i",
self.filepath,
"-movflags",
"faststart",
"-pix_fmt",
"yuv420p",
"-vf",
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-crf",
"17",
temp,
]
)
with open(temp, "rb") as file:
buf = file.read()
os.remove(temp)
return buf

async def to_mp3(self) -> bytes:
temp = tempfile.gettempdir() + "/" + time.time().__str__() + ".mp3"
await self.call(
Expand Down

0 comments on commit 293e808

Please sign in to comment.