C#使用FFmpeg 將視頻格式轉換成MP4示例

1、經常使用視頻格式分辨率html

  • 640x480p
  • 720p格式,分辨率爲1280×720p / 60Hz,行頻爲45kHz
  • 1080p格式,分辨率爲1920×1080逐行掃描,專業格式

2、FFmpeg部分參數說明:異步

//參數說明
/*
    * -i filename(input) 源文件目錄
    * -y 輸出新文件,是否強制覆蓋已有文件
    * -c 指定編碼器 
    * -fs limit_size(outinput) 設置文件大小的限制,以字節表示的。沒有進一步的字節塊被寫入後,超過極限。輸出文件的大小略大於所請求的文件大小。
    * -s 視頻比例  4:3 320x240/640x480/800x600  16:9  1280x720 ,默認值 'wxh',和原視頻大小相同
    * -vframes number(output) 將視頻幀的數量設置爲輸出。別名:-frames:v
    * -dframes number (output) 將數據幀的數量設置爲輸出.別名:-frames:d
    * -frames[:stream_specifier] framecount (output,per-stream) 中止寫入流以後幀數幀。
    * -bsf[:stream_specifier] bitstream_filters (output,per-stream)  指定輸出文件流格式,
例如輸出h264編碼的MP4文件:ffmpeg -i h264.mp4 -c:v copy -bsf:v h264_mp4toannexb -an out.h264 * -r 29.97 楨速率(能夠改,確認非標準楨率會致使音畫不一樣步,因此只能設定爲15或者29.97) *
*/

3、使用實例代碼:ide

    public class Demo2
    {
        public static string ffmpegtool = @"F:\SolutionSet\ABCSolution\VideoSolution\Demo1\bin\Debug\ffmpeg.exe";
        //public static string ffmpegtool = @"F:\ABCSolution\ffmpeg-20160808-ce2217b-win64-static\bin\ffplay.exe";
        public static string playFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\my3.mp4";
        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 static string sourceFile = @"F:\ABCSolution\VideoSolution\VideoSolution\Content\Video\theme.mp4";
        public void ConvertVideo()
        {
            Process p = new Process();//創建外部調用線程
            p.StartInfo.FileName = ffmpegtool;//要調用外部程序的絕對路徑
                                              //參數(這裏就是FFMPEG的參數了)
                                              //p.StartInfo.Arguments = @"-i "+sourceFile+ " -ab 56  -b a -ar 44100 -b 500 -r 29.97 -s 1280x720 -y " + playFile+"";

            // p.StartInfo.Arguments = "-y -i \""+sourceFile+"\" -b v   -s 800x600 -r 29.97 -b 1500 -acodec aac -ac 2 -ar 24000 -ab 128 -vol 200 -f psp  \""+playFile+"\" ";


            //string strArg = "-i " + sourceFile + " -y -s 640x480 " + playFile + " ";
            string strArg = "-i " + sourceFile + " -y -s 1280x720 " + playFile + " ";

            //獲取圖片
            //截取圖片jpg
            //string strArg = "-i " + sourceFile + " -y -f image2 -t 1 " + imgFile;
            //string strArg = "-i " + sourceFile + " -y -s 1280x720 -f image2 -t 1 " + imgFile;

            //視頻截取
            //string strArg = "  -i " + sourceFile + " -y   -ss 0:20  -frames 100  " + playFile;

            //轉化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;

            //顯示基本信息
            //string strArg = "-i " + sourceFile + " -n OUTPUT";

            //播放視頻 
            //string strArg = "-stats -i " + sourceFile + " ";

            p.StartInfo.Arguments = strArg;

            p.StartInfo.UseShellExecute = false;//不使用操做系統外殼程序啓動線程(必定爲FALSE,詳細的請看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序錯誤輸出寫到StandardError流中(這個必定要注意,FFMPEG的全部輸出信息,都爲錯誤輸出流,用StandardOutput是捕獲不到任何消息的...這是我耗費了2個多月得出來的經驗...mencoder就是用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);

                ////去獲取時長
                //string partitio1 = @"Duration: \d{2}:\d{2}:\d{2}.\d{2}";
                //if (RegexHelper.IsMatch(partitio1, output.Data))
                //{
                //    string partition = @"(?<=Duration: )\d{2}:\d{2}:\d{2}.\d{2}";
                //    string timespan = RegexHelper.Matchs(output.Data, partition).FirstOrDefault();
                //    TimeSpan span;
                //    if (TimeSpan.TryParse(timespan, out span))
                //    {
                //        Console.WriteLine(span.TotalMilliseconds);
                //    }
                //}

                ////獲取時刻
                //string partitio2 = @"time=\d{2}:\d{2}:\d{2}.\d{2}";
                //if (RegexHelper.IsMatch(partitio2, output.Data))
                //{
                //    string partition = @"(?<=time=)\d{2}:\d{2}:\d{2}.\d{2}";

                //    string timespan = RegexHelper.Matchs(output.Data, partition).FirstOrDefault();
                //    TimeSpan span;
                //    if (TimeSpan.TryParse(timespan, out span))
                //    {
                //        Console.WriteLine(span.TotalMilliseconds);
                //    }
                //}
            }
        }
    }

 

更多參考:post

ffmpeg ffplay ffprobe資料整理動畫

HTML5媒體播放說明編碼

相關文章
相關標籤/搜索