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
02 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.mp4The extra options
-fflags +shortest -max_interleave_delta 100Mare likely needed in this case. See My ffmpeg output always add extra 30s of silence at the end.Make sure the output frame rate isn't lower than 6 or so or players won't like it. You can see output frame rate in the console output log. Otherwise add the fps filter, such as
fps=10.
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.mp42 - Looping the output video for X time (X == audio duration/GIF duration in sec)
-y -stream_loop xTime -i gifToVideo.mp4 -c copy videoNoSound.mp43 - Merging the videoNoSound.mp4 with and audio file :
-y -i videoNoSound.mp4 -i audio.mp3 -c:v copy -c:a aac longVideo.mp44 - 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