Android平台下FFmpeg将图片转30秒演示视频的实现求助
Hey there! Let's get that image turned into a 30-second video using FFmpeg on Android. I'll break this down step by step so it's easy to follow, even if you're new to FFmpeg.
First, let's start with the basic FFmpeg command that does exactly what you need. This command will loop your image for 30 seconds and encode it into a widely supported MP4 video:
ffmpeg -loop 1 -i input.jpg -t 30 -c:v libx264 -pix_fmt yuv420p output.mp4
Here's what each part does:
-loop 1: Tells FFmpeg to loop the input image continuously (so it doesn't just show for a split second)-i input.jpg: Specifies the path to your input image file-t 30: Sets the total duration of the output video to 30 seconds-c:v libx264: Uses the H.264 video codec, which works seamlessly on almost all Android devices-pix_fmt yuv420p: Sets the pixel format to YUV420P—critical for ensuring compatibility with all Android media playersoutput.mp4: The path and filename for your finished video
Compiling FFmpeg from scratch for Android is a huge hassle, so we'll use a pre-built library called FFmpegKit to simplify things.
Step 1: Add the Dependency
Open your module-level build.gradle file and add this dependency to the dependencies block:
dependencies { implementation 'com.arthenica:ffmpegkit-full:5.1.0' }
Sync your project to download the library.
Step 2: Handle Permissions
You'll need permissions to read the input image and write the output video. Add these to your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" /> <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" android:minSdkVersion="33" />
Don't forget to request these permissions dynamically in your activity/fragment before running the FFmpeg command—Android won't let you access files without user approval.
Step 3: Run the Command in Code
Use FFmpegKit's async execution to avoid blocking the main thread (which would cause your app to freeze):
// Replace these paths with your actual input image and desired output video paths String inputPath = "/storage/emulated/0/Pictures/your_image.jpg"; String outputPath = "/storage/emulated/0/Movies/30sec_video.mp4"; FFmpegKit.executeAsync( "-loop 1 -i " + inputPath + " -t 30 -c:v libx264 -pix_fmt yuv420p " + outputPath, session -> { if (ReturnCode.isSuccess(session.getReturnCode())) { // Success! Do something like show a toast or navigate to the video Log.d("FFmpeg", "Video created successfully!"); } else if (ReturnCode.isCancel(session.getReturnCode())) { Log.d("FFmpeg", "Conversion was cancelled"); } else { // Handle errors—check the stack trace for details Log.e("FFmpeg", "Conversion failed: " + session.getFailStackTrace()); } }, log -> Log.d("FFmpeg", "Log: " + log.getMessage()), errorLog -> Log.e("FFmpeg", "Error Log: " + errorLog.getMessage()) );
- Speed up conversion: If the process is too slow on your device, add the
-preset fastparameter to trade a tiny bit of quality for faster encoding:ffmpeg -loop 1 -i input.jpg -t 30 -c:v libx264 -preset fast -pix_fmt yuv420p output.mp4 - Check file paths: Double-check that your input image path is correct—invalid paths are the most common cause of failed conversions.
- Test on multiple devices: Some older Android devices might have issues with certain codecs, but using
libx264andyuv420pshould cover almost all cases.
内容的提问来源于stack exchange,提问作者Mehmet Kurtgöz

