async def pic_gifcmd(event): # sourcery no-metrics "To convert replied image or sticker to spining round video." args = event.pattern_match.group(1) reply = await event.get_reply_message() if not reply: return await edit_delete(event, "`Reply to supported Media...`") media_type(reply) catevent = await edit_or_reply(event, "__Making round spin video wait a sec.....__") output = await _cattools.media_to_pic(event, reply, noedits=True) if output[1] is None: return await edit_delete( output[0], "__Unable to extract image from the replied message.__" ) meme_file = convert_toimage(output[1]) image = Image.open(meme_file) w, h = image.size outframes = [] try: outframes = await spin_frames(image, w, h, outframes) except Exception as e: return await edit_delete(output[0], f"**Error**\n__{str(e)}__") output = io.BytesIO() output.name = "Output.gif" outframes[0].save(output, save_all=True, append_images=outframes[1:], duration=1) output.seek(0) with open("Output.gif", "wb") as outfile: outfile.write(output.getbuffer()) final = os.path.join(Config.TEMP_DIR, "output.gif") output = await vid_to_gif("Output.gif", final) if output is None: return await edit_delete(catevent, "__Unable to make spin gif.__") media_info = MediaInfo.parse(final) aspect_ratio = 1 for track in media_info.tracks: if track.track_type == "Video": aspect_ratio = track.display_aspect_ratio height = track.height width = track.width PATH = os.path.join(Config.TEMP_DIR, "round.gif") if aspect_ratio != 1: crop_by = width if (height > width) else height await _catutils.runcmd( f'ffmpeg -i {final} -vf "crop={crop_by}:{crop_by}" {PATH}' ) else: copyfile(final, PATH) time.time() ul = io.open(PATH, "rb") uploaded = await event.client.fast_upload_file( file=ul, ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type="video/mp4", attributes=[ types.DocumentAttributeVideo( duration=0, w=1, h=1, round_message=True, supports_streaming=True, ) ], force_file=False, thumb=await event.client.upload_file(meme_file), ) sandy = await event.client.send_file( event.chat_id, media, reply_to=reply, video_note=True, supports_streaming=True, ) if not args: await _catutils.unsavegif(event, sandy) await catevent.delete() for i in [final, "Output.gif", meme_file, PATH, final]: if os.path.exists(i): os.remove(i)
async def video_catfile(event): # sourcery no-metrics "To make circular video note." reply = await event.get_reply_message() args = event.pattern_match.group(1) catid = await reply_id(event) if not reply or not reply.media: return await edit_delete(event, "`Reply to supported media`") mediatype = media_type(reply) if mediatype == "Round Video": return await edit_delete( event, "__Do you think I am a dumb person😏? The replied media is already in round format,recheck._", ) if mediatype not in ["Photo", "Audio", "Voice", "Gif", "Sticker", "Video"]: return await edit_delete(event, "```Supported Media not found...```") flag = True catevent = await edit_or_reply(event, "`Converting to round format..........`") catfile = await reply.download_media(file="./temp/") if mediatype in ["Gif", "Video", "Sticker"]: if not catfile.endswith((".webp")): if catfile.endswith((".tgs")): hmm = await make_gif(catevent, catfile) os.rename(hmm, "./temp/circle.mp4") catfile = "./temp/circle.mp4" media_info = MediaInfo.parse(catfile) aspect_ratio = 1 for track in media_info.tracks: if track.track_type == "Video": aspect_ratio = track.display_aspect_ratio height = track.height width = track.width if aspect_ratio != 1: crop_by = width if (height > width) else height await _catutils.runcmd( f'ffmpeg -i {catfile} -vf "crop={crop_by}:{crop_by}" {PATH}' ) else: copyfile(catfile, PATH) if str(catfile) != str(PATH): os.remove(catfile) try: catthumb = await reply.download_media(thumb=-1) except Exception as e: LOGS.error(f"circle - {str(e)}") elif mediatype in ["Voice", "Audio"]: catthumb = None try: catthumb = await reply.download_media(thumb=-1) except Exception: catthumb = os.path.join("./temp", "thumb.jpg") await thumb_from_audio(catfile, catthumb) if catthumb is not None and not os.path.exists(catthumb): catthumb = os.path.join("./temp", "thumb.jpg") copyfile(thumb_loc, catthumb) if ( catthumb is not None and not os.path.exists(catthumb) and os.path.exists(thumb_loc) ): flag = False catthumb = os.path.join("./temp", "thumb.jpg") copyfile(thumb_loc, catthumb) if catthumb is not None and os.path.exists(catthumb): await _catutils.runcmd( f"""ffmpeg -loop 1 -i {catthumb} -i {catfile} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -vf \"scale=\'iw-mod (iw,2)\':\'ih-mod(ih,2)\',format=yuv420p\" -shortest -movflags +faststart {PATH}""" ) os.remove(catfile) else: os.remove(catfile) return await edit_delete( catevent, "`No thumb found to make it video note`", 5 ) if ( mediatype in [ "Voice", "Audio", "Gif", "Video", "Sticker", ] and not catfile.endswith((".webp")) ): if os.path.exists(PATH): c_time = time.time() attributes, mime_type = get_attributes(PATH) ul = io.open(PATH, "rb") uploaded = await event.client.fast_upload_file( file=ul, progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, catevent, c_time, "Uploading....") ), ) ul.close() media = types.InputMediaUploadedDocument( file=uploaded, mime_type="video/mp4", attributes=[ types.DocumentAttributeVideo( duration=0, w=1, h=1, round_message=True, supports_streaming=True, ) ], force_file=False, thumb=await event.client.upload_file(catthumb) if catthumb else None, ) sandy = await event.client.send_file( event.chat_id, media, reply_to=catid, video_note=True, supports_streaming=True, ) if not args: await _catutils.unsavegif(event, sandy) os.remove(PATH) if flag: os.remove(catthumb) await catevent.delete() return data = reply.photo or reply.media.document img = io.BytesIO() await event.client.download_file(data, img) im = Image.open(img) w, h = im.size img = Image.new("RGBA", (w, h), (0, 0, 0, 0)) img.paste(im, (0, 0)) m = min(w, h) img = img.crop(((w - m) // 2, (h - m) // 2, (w + m) // 2, (h + m) // 2)) w, h = img.size mask = Image.new("L", (w, h), 0) draw = ImageDraw.Draw(mask) draw.ellipse((10, 10, w - 10, h - 10), fill=255) mask = mask.filter(ImageFilter.GaussianBlur(2)) img = ImageOps.fit(img, (w, h)) img.putalpha(mask) im = io.BytesIO() im.name = "cat.webp" img.save(im) im.seek(0) await event.client.send_file(event.chat_id, im, reply_to=catid) await catevent.delete()