c#使用 FFMPEG 視頻格式轉換


  偶然收到一份需求,需要將wmv的視頻文件,轉換成MP4格式,以方便公眾號、自媒體等推送后,在線點擊播放。

實現思路:下載了格式工廠,提供的方法可滿足業務需求,可無損將wmv文件進行轉換。出於業務化的考慮,進行了一點點嘗試。如果MP4文件的碼流格式無要求,那么,你可以按我下面的步驟來實現。

外部應用:

             說明:mplayer.exe 主要提供桌面端應用視頻的播放功能;

        mencoder.exe是Mplayer自帶的編碼工具(Mplayer是Linux下的播放器,開源,支持幾乎所有視頻格式的播放,有windows和Mac版本)

        ffmpeg.exe  FLV視頻轉換器,可以輕易地實現FLV向其它格式avi、asf、 mpeg的轉換或者將其它格式轉換為flv ,我主要也是用它來實現轉換。 

 

不多說,show my code 

1.創建一個ProcessStartInfo,去調用ffmpeg.exe

private static void Dowork(FileInfo fileinfo)
        {            
            logger.InfoFormat("start deal file {0}",fileinfo.Name);
            var ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extention", "ffmpeg.exe");
            var parameters = BuildCommon(fileinfo);
            var oInfo = new ProcessStartInfo(ffmpegPath,parameters);
            oInfo.UseShellExecute = false;
            oInfo.CreateNoWindow = true;
            oInfo.RedirectStandardOutput = true;
            oInfo.RedirectStandardError = true;
            oInfo.RedirectStandardInput = true;

            try
            {
                //調用ffmpeg開始處理命令
                var proc = Process.Start(oInfo);
                proc.StartInfo = oInfo;
                proc.ErrorDataReceived+=new DataReceivedEventHandler(OnReceive);
                
                proc.OutputDataReceived += new DataReceivedEventHandler(OnReceive);

                proc.Start();
                proc.BeginErrorReadLine();
                proc.WaitForExit();
                //轉換成string

                //關閉處理程序
                proc.Close();
                proc.Dispose();
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            finally
            {
            }
            logger.InfoFormat("finish deal file {0}", fileinfo.Name);

        }

2.獲取原視頻 相關參數,包括寬高、fps、音頻碼率、音頻采樣率等等信息

private static VideoFile GetMovieInfo(string file)
        {
            var ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extention", "ffmpeg.exe");
            var  enc=new VideoEncoder.Encoder();
            enc.FFmpegPath = ffmpegPath;
            VideoFile videoFile = new VideoFile(file);
            enc.GetVideoInfo(videoFile);
            return videoFile;
        }

3.構建轉換參數 參數定義源碼中有詳細解釋

private static string BuildCommon(FileInfo file)
        {
            var parameters = "-i  {1} -c:v libx264 -strict -2 {0} {2}";
            var videoinfo=GetMovieInfo(file.FullName);
            var commond = "";
            if (!string.IsNullOrEmpty(_scale))
            {
                commond += string.Format(" -s {0}", _scale);
            }
            else
            {
                commond += string.Format(" -s {0}x{1}", videoinfo.Width,videoinfo.Height);
            }
            if (!string.IsNullOrEmpty(_fps))
            {
                commond += string.Format(" -r {0}", _fps);
            }
            else
            {
                commond += string.Format(" -r {0}", videoinfo.Fps);
            }
            if (!string.IsNullOrEmpty(_datarate))
            {
                commond += string.Format(" -b {0}", _datarate);
            }
            else
            {
                commond += " -qscale 4";
            }
            if (!string.IsNullOrEmpty(_bitrate))
            {
                commond += string.Format(" -ab {0}", _bitrate);
            }
            else
            {
                commond += string.Format(" -ab {0}k", videoinfo.AudioRate);
            }
            if (!string.IsNullOrEmpty(_freq))
            {
                commond += string.Format(" -ar {0}", _freq);
            }
            else
            {
                commond += string.Format(" -ar {0}k", videoinfo.AudioFreq);
            }

            return string.Format(parameters, commond, file.FullName, file.FullName.Replace(".wmv", ".mp4"));
        }

 

總結:FFMPEG.EXE非常強大,市場是主流的轉碼工具,視頻合成、剪切工具都是基於它為底層進行的二次開發,可根據自己的實際需要,來進行研究。

源代碼

 


免責聲明!

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



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