[Python] The final size of generated gif is much more than expected

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I wrote a code for a bot which download a file, convert it to gif ( by moviepy ) and then it will send the final gif to the client. The problem is e.g the input is 5 mb and the result is 220 mb ! What should I do to compress it as low as possible?

    This is my piece of code which downloads and make the gif :



    @bot.on_message(at_state("VIDEO") & video)

    async def download_video(client, message):

    downloading = await message.reply("⏳ *در حال دانلود فایل شما ...*")

    file_size = message.video.size

    max_size = 10 * 1024 * 1024

    if file_size > max_size:

    await downloading.edit_text("حجم فایل ارسالی بیشتر از 10 مگابایت است ")

    else:

    response = await client.download(message.video.id)

    mime_type = message.video.mime_type.split("/")[-1]

    file_format = mime_type.split(";")[0]

    id = message.author.id

    with open(f"video-{id}.{file_format}", "wb") as file:

    file.write(response)



    await downloading.edit_text(f"""⌛️ *دانلود فایل شما با موفقیت به پایان رسید.

    ️ شروع ساخت گیف ...*""")



    # Context-Manager

    with VideoFileClip(f"video-{id}.mp4") as clip:

    resized_video = CompositeVideoClip([clip])

    resized_video.write_gif(f"video-{id}.gif", program="ffmpeg", fps=25)



    id = message.author.id

    await downloading.edit_text(f"""️ *گیف شما با موفقیت ساخته شد.

    شروع ارسال گیف نهایی برای شما ...*""")

    await bot.send_animation(id, f"video-{id}.gif", 60, 100, 100, "️ *گیف شما ساخته شد و نتیجه نهایی ارسال گردید.*")

    await downloading.edit_text("️ *گیف شما با موفقیت ساخته شد.*")

    await message.reply(f"""*جهت ادامه کار با ربات ،

    یکی از گزینه های زیر رو انتخاب کن *""",

    InlineKeyboard(

    [(" ویدیو به گیف ️","1")],

    [(" عکس به گیف ️","2")],

    ))



    # Deleting

    time.sleep(10)

    os.remove(f"video-{id}.mp4")

    os.remove(f"video-{id}. gif")

    Continue reading...

Compartilhe esta Página