What command would I use to convert an mp4 or mv4 video file to an animated gif, and vice versa. That is, convert a animated gif to a mp4 or mv4.
Here's what worked for me:
ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
movflags – This option optimizes the structure of the MP4 file so the browser can load it as quickly as possible.
pix_fmt – MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers.
vf – MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case.
In my case, using
ffmpeg directly did the trick and provided the best result:
$ ffmpeg -f gif -i infile.gif outfile.mp4
Another way to convert GIF animation to video:
ffmpeg -i your_gif.gif -c:v libvpx -crf 12 -b:v 500K output.mp4
-crf values can go from 4 to 63. Lower values mean better quality. -b:v is the maximum allowed bitrate. Higher means better quality.
If you want to make the output in "n loops", look at this solution in one shot
So, let's convert a normal.gif to loop.mp4
for 2 loops movie example:
ffmpeg -stream_loop 2 -i normal.gif loop.gif -y;ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4 -y
for 5 loops movie example:
ffmpeg -stream_loop 5 -i normal.gif loop.gif -y;ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4 -y
BE AWARE: there is a -y [for the overwrite]
MacOs version is as following:
$ ffmpeg -i input.avi -pix_fmt yuv420p output.mp4