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

Unity(C#)开发中如何将GIF转换为MP4视频并保存到磁盘

方案1:Unity中调用moviepy实现GIF转MP4
  • 提前在Windows 10设备安装Python运行环境,通过pip安装moviepy及依赖的ffmpeg组件,确认本地命令行可正常运行GIF转MP4的moviepy脚本。
  • 不要在Unity内直接嵌入Python解释器执行代码,直接通过C#启动系统进程调用本地Python环境运行转换脚本,避开跨语言引用的兼容问题。
  • 转换逻辑单独写成独立Python脚本存放在项目StreamingAssets目录,C#侧动态传入待转换GIF路径、输出MP4路径作为脚本执行参数,不要硬编码文件路径。
  • C#侧调用示例代码:
using System.Diagnostics;
using System.IO;
using UnityEngine;

public class GifConverter
{
    public void ConvertWithMoviePy(string gifInputAbsPath, string mp4OutputAbsPath)
    {
        string pyScriptPath = Path.Combine(Application.streamingAssetsPath, "gif_convert.py");
        ProcessStartInfo processInfo = new ProcessStartInfo
        {
            // 若Python未配置系统环境变量,此处替换为本地python.exe的完整绝对路径
            FileName = "python",
            Arguments = $"\"{pyScriptPath}\" \"{gifInputAbsPath}\" \"{mp4OutputAbsPath}\"",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        };

        using Process convertProcess = Process.Start(processInfo);
        // 读取执行日志可判断转换进度、报错信息
        string runtimeLog = convertProcess.StandardOutput.ReadToEnd();
        string runtimeError = convertProcess.StandardError.ReadToEnd();
        convertProcess.WaitForExit();
    }
}
  • 对应gif_convert.py脚本内容:
import sys
import moviepy.editor as mp

if __name__ == "__main__":
    input_gif_path = sys.argv[1]
    output_mp4_path = sys.argv[2]
    clip = mp.VideoFileClip(input_gif_path)
    clip.write_videofile(output_mp4_path)
  • 项目打包时,需要将Python运行环境、ffmpeg依赖、转换脚本随安装包一同分发,避免目标设备缺失运行环境导致转换失败。
方案2:纯C#实现GIF转MP4(无Python依赖)
  • 放弃在Unity中使用System.Drawing下的Bitmap类,Unity默认Mono/.NET运行时对System.Drawing兼容度极低,安装System.Drawing.Common NuGet包、手动引用系统System.Drawing程序集均无法解决兼容问题,无需反复尝试这两类修复方案。
  • GIF帧提取不要使用WinForm适配的GDI+接口,直接使用Unity原生Texture2D配合跨平台GIF解析库读取帧数据,同时记录每帧的显示时长。
  • MP4编码可直接使用Unity 2022.2及以上版本内置的UnityEngine.Video.VideoEncoder接口,或引入FFmpeg.AutoGen绑定库完成编码,全程不依赖WinForm专属组件。
  • 最简落地路径为随项目打包ffmpeg.exe,通过C#启动进程调用ffmpeg命令完成转换,无需自行实现帧提取、编码逻辑,无System.Drawing相关兼容问题,示例代码如下:
using System.Diagnostics;
using System.IO;
using UnityEngine;

public class GifConverter
{
    public void ConvertWithFFmpeg(string gifInputAbsPath, string mp4OutputAbsPath)
    {
        // ffmpeg.exe存放于StreamingAssets目录下
        string ffmpegExePath = Path.Combine(Application.streamingAssetsPath, "ffmpeg.exe");
        ProcessStartInfo processInfo = new ProcessStartInfo
        {
            FileName = ffmpegExePath,
            Arguments = $"-f gif -i \"{gifInputAbsPath}\" -c:v libx264 -pix_fmt yuv420p \"{mp4OutputAbsPath}\" -y",
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using Process convertProcess = Process.Start(processInfo);
        convertProcess.WaitForExit();
    }
}
  • 该方案无需额外安装Python环境,适配Windows 10平台下所有Unity发布场景,转换稳定性高于自行实现的编码逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 05:21:24