C#使用FFmpeg 将视频格式转换成Gif图片示例

根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317

一、本次使用参数说明

复制代码
/*
    * 参数说明:
    * -i 源文件位置
    * -y 输出新文件,是否覆盖现有文件
    * -s 视频比例  4:3 320x240/640x480/800x600  16:9  1280x720 ,默认值 'wxh',和原视频大小相同
    * -f 等同‘-formats’,定义的可支持的文件格式‘ffmpeg-formats’,更多参考:https://ffmpeg.org/ffmpeg-formats.html
    * -vframes 数字类型,指定视频输出帧数
    * -dframes 数字类型,指定数据输出帧数,截取的前n帧
    * -frames[:stream_specifier] framecount (output,per-stream) 停止写入流之后帧数帧。
    */
复制代码

二、代码示例:

复制代码
class GifDemo1
{
    public static string ffmpegtool = @"F:\SolutionSet\ABCSolution\VideoSolution\Demo1\bin\Debug\ffmpeg.exe";
    public static string imgFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\my3.gif";
    public static string sourceFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\COOLUI.mp4";
    public void ConvertVideoToGif()
    {
        Process p = new Process();//建立外部调用进程
        //要调用外部程序的绝对路径
        p.StartInfo.FileName = ffmpegtool;
        //转化gif动画
        string strArg = "-i " + sourceFile + " -y -s 1280x720 -f gif -vframes 30 " + imgFile;
        //string strArg = "  -i " + sourceFile + " -y  -f gif -vframes 50 " + imgFile;
        // string strArg = "  -i " + sourceFile + " -y  -f gif -ss 0:20  -dframes 10 -frames 50 " + imgFile;

        p.StartInfo.Arguments = strArg;

        p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
        p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...)
        p.StartInfo.CreateNoWindow = false;//不创建进程窗口
        p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
        p.Start();//启动线程
        p.BeginErrorReadLine();//开始异步读取
        p.WaitForExit();//阻塞等待进程结束
        p.Close();//关闭进程
        p.Dispose();//释放资源
    }
    private void Output(object sendProcess, DataReceivedEventArgs output)
    {
        if (!String.IsNullOrEmpty(output.Data))
        {
            //处理方法...
            Console.WriteLine(output.Data);
        }
    }
}
复制代码

响应内容:

 

 

更多参考:

C#使用FFmpeg 将视频格式转换成MP4示例

ffmpeg ffplay ffprobe资料整理

实现拖动文件到窗体(控件)

posted @   天马3798  阅读(3744)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-01-15 Canvas简介
2014-01-15 CSS中表示cellpadding和cellspacing的方法
2014-01-15 网站项目:让一般处理文件.ashx的代码有折叠功能(#region)
点击右上角即可分享
微信分享提示