FFMPEG : create a looping video with specific time

I'm trying to convert a GIF file into video and merge the output video with an audio file, so the final result will be a video + audio.

public static String makeVideo(String inputGifPath, String outputPath, int width, int height) { return "-f gif -y -i " + inputGifPath + " -vf crop=" + width + ":" + height + ":0:40,scale=1280:720,setsar=1 " + outputPath;
}

The code above convert the input GIF into video file keeping the same duration of the GIF.

I wonder if there's any way to convert the GIF into video with a specific time for example, I want the video output to loop for 3min:20s

0

2 Answers

Use the -stream_loop input option:

ffmpeg -stream_loop -1 -i input.gif -i audio.mp3 -vf crop=" + width + ":" + height + ":0:40,scale=1280:720,setsar=1,format=yuv420p -shortest -fflags +shortest -max_interleave_delta 100M -movflags +faststart output.mp4
1

The @llogan answer worked for me, but for some reason its take long time to finish the processing, I ended this using the following :

1 - Converting GIF to video (with the same duration of the GIF file)

-f gif -y -i input.gif -vf crop=540:402:0:40,scale=1280:720,setsar=1 gifToVideo.mp4

2 - Looping the output video for X time (X == audio duration/GIF duration in sec)

-y -stream_loop xTime -i gifToVideo.mp4 -c copy videoNoSound.mp4

3 - Merging the videoNoSound.mp4 with and audio file :

-y -i videoNoSound.mp4 -i audio.mp3 -c:v copy -c:a aac longVideo.mp4

4 - Cutting the longVideo.mp4 to match the audio duration :

-y -i longVideo.mp4 -ss 00:00:00 -t audioDuration -async 1 -c copy finalVideo.mp4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like