19

I am trying to find some way for converting GIF to mp4 using Python or library. I didn't found any solution for it. I found a library for generating gifs from videos but not the other way round.

Can anyone please give me some information on how to do it.

1

3 Answers 3

29

Try MoviePy:

import moviepy.editor as mp

clip = mp.VideoFileClip("mygif.gif")
clip.write_videofile("myvideo.mp4")

If you don't have MoviePY installed then first install it:

pip install MoviePy
3
  • 1
    When I use it , it is giving me error : TypeError: must be real number, not NoneType
    – Hamza
    Commented Aug 26, 2021 at 7:29
  • It's important to also have the library "ffmpeg" installed in the system... in Alpine linux it is RUN apk add ffmpeg Commented Mar 10, 2022 at 7:46
  • This works fine. But the transparency of the gif images is lost after the mp4 is created
    – rags
    Commented Jul 22, 2023 at 13:55
4

There are many ways to do this. Relatively simple way is to use ffmpeg. There are many python bindings. ffmpy is one of them. Please check here for the documentation. Basic example:

Installation:

pip install ffmpy

Usage:

>>> import ffmpy
>>> ff = ffmpy.FFmpeg(
...     inputs={'input.gif': None},
...     outputs={'output.mp4': None}
... )
>>> ff.run()

Again, there are many other ways to do this. Please find the related references here:

  1. https://unix.stackexchange.com/questions/40638/how-to-do-i-convert-an-animated-gif-to-an-mp4-or-mv4-on-the-command-line
  2. https://sonnguyen.ws/convert-gif-to-mp4-ubuntu/
  3. How to Convert animated .gif into .webm format in Python?
2
  • This didn't work :( Commented Mar 10, 2022 at 7:33
  • This works fine. But the transparency of the gif images is lost after the mp4 is created
    – rags
    Commented Jul 22, 2023 at 13:55
-2
from moviepy.editor import *

clip = (VideoFileClip("VIDEO.mp4")
        .subclip((1,22.65),(1,23.2))
        .resize(0.3))
clip.write_gif("nAME_OF_gif_FILE.gif")

You can download Video with this command if you have Youtube-dl installed:

1
  • 1
    The OP is asking for gif -> mp4
    – DrustZ
    Commented Feb 15, 2021 at 0:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.