in-user
in-user

Reputation: 43

How to loop over gif file finite number of times while creating mp4 as output with ffmpeg

I am able to convert gif file to mp4 using this command:

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4

What I want to do is to loop over gif animation 3 times and convert to mp4.

I am able to do this with 2 shell commands. First one from above and then concatenate the same video 3 times.

ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/video.mp4'\n%.0s" {1..3}) -c copy videoloop.mp4

I have also tried with -ignore_loop 0 option and setting time. It does work but it is not exactly what I am trying to do since I can extend the video but can not make exactly 3 loops.

ffmpeg -ignore_loop 0 -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -t 12 videoloop.mp4

So as you can see I am already able to achieve what I want, but with 2 shell commands:

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/video.mp4'\n%.0s" {1..3}) -c copy videoloop.mp4

Is it possible to do this with only one call to ffmpeg?

I tried with -loop option for the input file. It doesn't work for gifs. I also tried with -stream_loop. It creates something corrupted.

Upvotes: 3

Views: 2907

Answers (1)

Sergio Mcfly PYK
Sergio Mcfly PYK

Reputation: 4331

I use to convert gif to mp4 in two steps when dealing with LOOPING

e.g. GIF ---> LOOP ---> MP4

loop the gif itself first. In this example, the output will be added by 2 repetitions

[final file will play once, plus 2 repetitions (loops)]

ffmpeg -stream_loop 2 -i normal.gif loop.gif

convert the LOOPED GIF to mp4

ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4

the parameters -vf was used to avoid the scale division error and to maintain the aspect ratio on fine, getting rid of the error message such as:

height not divisible by 2 (320x569)

2 frames left in the queue on closing

NOTE THAT: the width and the height was divided by 2 and then multiplied by 2 again

[ quite tricky! ] \o/

Upvotes: 4

Related Questions