基于FFmpegKit的Flutter多图转带音频视频幻灯片开发问题
Using FFmpegKit to Create Videos from Images (with Audio & Animations)
1. Basic: Single Image + MP3 to Video
First, let's get the simple case working. Here's the FFmpeg command you can run via FFmpegKit:
ffmpeg -loop 1 -i input.jpg -i audio.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 192k -shortest output.mp4
Breakdown of key parameters:
-loop 1: Loops the input image until the audio ends-i input.jpg: Your source image file-i audio.mp3: Your source audio file-c:v libx264: Uses the H.264 codec (standard for MP4 compatibility)-tune stillimage: Optimizes encoding for static images (reduces file size without quality loss)-c:a aac: Uses the AAC audio codec (widely supported)-b:a 192k: Sets audio bitrate to 192kbps (adjust based on quality needs)-shortest: Ends the video when the audio finishesoutput.mp4: The final video file
2. Multiple Images with Animations & Transitions
For multiple images with animations (fades, slides, etc.), use FFmpeg's filter complex. Below is an example with 3 images, each displayed for 5 seconds, with crossfade transitions, plus background audio:
ffmpeg -i img1.jpg -i img2.jpg -i img3.jpg -i audio.mp3 \ -filter_complex " [0:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v0]; [1:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; [2:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; [v0][v1]xfade=transition=fade:duration=1:offset=4[v01]; [v01][v2]xfade=transition=fade:duration=1:offset=9[v]; " \ -map "[v]" -map 3:a -c:v libx264 -c:a aac -b:a 192k -shortest output.mp4
Key explanations:
- Each image gets
fadefilters:fade=t=in:st=0:d=1adds a 1-second fade-in,fade=t=out:st=4:d=1adds a 1-second fade-out starting at 4 seconds (total 5 seconds per image) xfadecreates crossfade transitions:transition=fadeuses a fade effect,duration=1makes transitions last 1 second,offset=4starts the transition when the first image's fade-out begins-map "[v]"uses the filtered video stream,-map 3:auses the 4th input (audio) stream
Common Animation Variations
- Slide transitions: Replace
xfade=transition=fadewithxfade=transition=slideleft(orslideright,slideup,slidedown) - Zoom effect: Add
zoompan=z='min(zoom+0.001,1.5)':d=125to an image filter (adjust zoom speed and max zoom level as needed) - Pan effect: Use
pan=w=1920:h=1080:x='if(gte(t,2),100+(t-2)*50,0)':y=0to pan across a larger image (tweak dimensions and speed values)
Troubleshooting Tips
- Ensure all images have the same resolution, or add a
scalefilter to standardize: e.g.,[0:v]scale=1920:1080,fade=... - If audio is out of sync, double-check image durations and transition timings, or rely on
-shortestto match audio length - For mobile FFmpegKit (Android/iOS), use the full package variant to access all codecs and filters
内容的提问来源于stack exchange,提问作者Ajeet Kumar Sah
相关产品推荐
相关产品推荐

