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資料整理

實現拖動文件到窗體(控件)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM