You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Core FFmpeg Command for the Conversion

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 players
  • output.mp4: The path and filename for your finished video
Implementing This in Android

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())
);
Quick Tips for Better Results
  • Speed up conversion: If the process is too slow on your device, add the -preset fast parameter 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 libx264 and yuv420p should cover almost all cases.

内容的提问来源于stack exchange,提问作者Mehmet Kurtgöz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:18:04