C# Winfrom使用ffmpeg轉換視頻格式

  //視頻轉碼
        private void VideoConverter()
        {
            filelen = 0;
            ListView lv = listView1;

            for (int i = 0; i < lv.Items.Count; i++)
            {
                if (lv.Items[i].SubItems[3].Text != "已完成")
                {
                    Process p = new Process();

                    p.StartInfo.FileName = path + "ffmpeg";

                    //p.StartInfo.FileName = path + "ffmpeg.exe";

                    p.StartInfo.UseShellExecute = false;
                    string srcFileName = "";
                    string destFileName = "";
                    string newFileName = "";
                    string mbgs = "." + comboBox2.SelectedItem.ToString();

                    srcFileName = lv.Items[i].SubItems[1].Text;
                    newFileName = lv.Items[i].SubItems[0].Text.Split('.')[0];

                    destFileName = "\"" + label3.Text + "\\" + newFileName + DateTime.Now.ToString("yyyyMMddhhmmss");

                    p.StartInfo.Arguments = "-i " + srcFileName + " -y  -vcodec h264 -b 500000 " + destFileName + mbgs + "\"";    //執行參數

                    p.StartInfo.UseShellExecute = false;  ////不使用系統外殼程序啓動進程
                    p.StartInfo.CreateNoWindow = true;  //不顯示dos程序窗口

                    p.StartInfo.RedirectStandardInput = true;

                    p.StartInfo.RedirectStandardOutput = true;

                    p.StartInfo.RedirectStandardError = true;//把外部程序錯誤輸出寫到StandardError流中

                    p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);

                    p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

                    p.StartInfo.UseShellExecute = false;

                    p.Start();

                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                    p.BeginErrorReadLine();//開始異步讀取

                    p.WaitForExit();//阻塞等待進程結束

                    p.Close();//關閉進程

                    p.Dispose();//釋放資源
                    setState(i);//更新文件狀態
                    filelen += 1;//已轉換文件數量
                }
            }

        }



下面是壓縮前後的文件大小及畫質對比


壓縮前

壓縮後



源碼下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章