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

如何在Android项目中正确导入FFmpeg以实现图片转视频

解决Android项目中MobileFFmpeg无法导入的问题

1. 补全类导入语句

你代码中直接使用FFmpeg类但未导入,MobileFFmpeg的完整类路径是com.arthenica.mobileffmpeg.FFmpeg,同时RETURN_CODE_SUCCESS常量属于ReturnCode类,需在MainActivity顶部添加:

import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.ReturnCode;

2. 确保依赖同步完成

  • 点击Android Studio右上角的Sync Project with Gradle Files按钮,等待依赖下载同步完成。
  • 若同步失败,尝试清理缓存重启:File -> Invalidate Caches... -> Invalidate and Restart

3. 验证仓库与依赖配置

你的build.gradle已添加正确依赖implementation 'com.arthenica:mobile-ffmpeg-full:4.5.LTS',且settings.gradle中dependencyResolutionManagement已包含mavenCentral()仓库,这部分配置没问题。

4. 处理Release模式混淆(可选)

如果是Release构建,需在proguard-rules.pro中添加规则避免FFmpeg类被混淆:

-keep class com.arthenica.mobileffmpeg.** { *; }
-dontwarn com.arthenica.mobileffmpeg.**

修正后的核心代码片段

import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.ReturnCode;
// ...

private void convertImagesToVideo() {
    StringBuilder imageListFileContent = new StringBuilder();
    for (Uri imageUri : selectedImages) {
        imageListFileContent.append("file '").append(imageUri.getPath()).append("'\n");
    }

    try {
        File imageListFile = new File(getCacheDir(), "image_list.txt");
        FileWriter writer = new FileWriter(imageListFile);
        writer.append(imageListFileContent.toString());
        writer.flush();
        writer.close();

        File outputFile = new File(getExternalFilesDir(null), "output_video.mp4");
        String outputFilePath = outputFile.getAbsolutePath();

        String command = "-f concat -safe 0 -i " + imageListFile.getAbsolutePath() +
                " -vf \"scale=-2:720\" -r 30 -c:v libx264 -pix_fmt yuv420p " + outputFilePath;

        int rc = FFmpeg.execute(command);

        if (rc == ReturnCode.SUCCESS) {
            Log.d("FFmpeg", "Video conversion completed successfully");
            // 后续可处理视频播放或分享逻辑
        } else {
            Log.d("FFmpeg", "Video conversion failed");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

内容的提问来源于stack exchange,提问作者Vyacheslav Parinov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 14:10:33