FFmpeg库

FFmpeg库

将FFmpeg库添加到Minecraft中

FFmpegLib 是一个面向 Minecraft NeoForge 1.21.1 的技术类库模组,将 FFmpeg 多媒体处理能力直接集成到游戏中。该模组不会为玩家添加任何游戏内容,纯粹作为其他模组的 API,用于在后台线程中处理动态媒体而不会拖慢游戏主循环。

如有任何错误或疑问,请在评论区或通过私信告知我。

功能特性:

— 可执行文件管理:自动追踪并管理位于 config/ffmpeglib/ 目录下的 ffmpeg.exe 文件
— 异步编排器:提供 Java API(FFmpegOrchestrator),使用 CompletableFuture 执行繁重的视频/音频处理任务
— 帧提取:将视频文件(如 mp4)按指定帧率转换为 PNG 帧序列,用于自定义 GUI 或游戏内渲染
— 崩溃隔离:所有解码任务在独立进程中运行,避免因媒体文件损坏导致 Minecraft 崩溃。


API 方法

视频帧提取

将视频转换为以 frame_%04d.png 命名的顺序图像,存放于目标文件夹

— 方法签名

public static CompletableFuture<Void> extractFrames(File ffmpegExe, File videoFile, File targetDir, int fps)

— 参数

ffmpegExe:已验证的 FFmpeg 可执行文件路径

videoFile:要处理的源视频文件

targetDir:输出 PNG 帧的目标文件夹

fps:目标帧率(每秒提取的帧数)


来自 Lost in Fog 的示例(近似)

@SubscribeEvent
public static void onClientLogin(net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent.LoggingIn event) {
    if (!ffmpegExe.exists() || !ffmpegExe.isFile()) {
        Minecraft.getInstance().execute(() -> {
            Minecraft.getInstance().setScreen(new FfmpegMissingScreen(null));
        });
        return;
    }
CompletableFuture.runAsync(() -&gt; {
    try {
        for (int d = 1; d &lt;= 5; d++) {
            File targetDir = new File(cadriDir, "video" + d);
            File videoFile = new File(videoDir, "anal" + d + ".mp4");
            
            if (!videoFile.exists()) extractVideoResource(d, videoFile);
            
            if (!targetDir.exists()) targetDir.mkdirs();
            File[] existingFrames = targetDir.listFiles();
            
            if (existingFrames == null || existingFrames.length == 0) {
                // 此处调用
                FFmpegOrchestrator.extractFrames(ffmpegExe, videoFile, targetDir, 25).join();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
});

}

我并非编程专家,也不是制作模组库的专业人士,因此这个库的规模较小。我已尽力描述所有用到的方法,该模组主要作为 Lost in Fog 的补充,而非独立的模组库。