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:SolutionSetABCSolutionVideoSolutionDemo1inDebugffmpeg.exe";
    public static string imgFile = @"F:SolutionSetABCSolutionVideoSolutionVideoSolutionContentVideomy3.gif";
    public static string sourceFile = @"F:SolutionSetABCSolutionVideoSolutionVideoSolutionContentVideoCOOLUI.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资料整理

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

原文地址:https://www.cnblogs.com/tianma3798/p/6286711.html