1. 程式人生 > >ffmpeg下載安裝和簡單應用(C#音頻格式轉換)

ffmpeg下載安裝和簡單應用(C#音頻格式轉換)

lan 音頻 sss sleep 自定義庫 blog version 就是 可執行文件

ffmpeg下載安裝和簡單應用

先介紹一下ffmpeg:FFmpeg是一個自由軟件,可以運行音頻和視頻多種格式的錄影、轉換、流功能,包含了libavcodec —這是一個用於多個項目中音頻和視頻的解碼器庫,以及libavformat——一個音頻與視頻格式轉換庫。
名字由來:"FFmpeg"這個單詞中的"FF"指的是"Fast Forward"[2]。有些新手寫信給"FFmpeg"的項目負責人,詢問FF是不是代表“Fast Free”或者“Fast Fourier”等意思,"FFmpeg"的項目負責人回信說“Just for the record, the original meaning of "FF" in FFmpeg is "Fast Forward"...”

一、ffmpeg下載

先到http://ffmpeg.org/下載ffmpeg安裝文件

技術分享圖片

技術分享圖片

技術分享圖片

接著中間部分黑體字 Latest Zeranoe FFmpeg Build Version下面有系統標註,32位還是64位,並且都有三種版本,簡單介紹一下,都是我理解的想法,不太懂英文,大家見諒。
Static Versions是集成版,就是全都封裝在一個exe可執行文件裏了。
Shared Versions是共享版,這個是總的執行程序和一些lib庫文件在一個文件夾裏,應該是為了可以自定義庫吧,我猜的。
Dev Versions是開發版,裏面完全是腳本,看樣子像Linux下的,這個真不懂。

大家按自己的系統位數建議選擇Static Versions集成版,只需要一個文件ok了,幹凈方便。

二、ffmpeg安裝

a、解壓下載完的ffmpeg-20150407-git-c4b2017-win64-shared

解壓後如圖,(doc文件夾就是關於文檔,licenses是聲明,這個有個開源軟件協議,了解詳情請百度,presets文件夾裏貌似是一些編碼的默認設置吧,我猜的,不懂,想了解還是百度吧,度娘真是萬能的)

技術分享圖片

技術分享圖片

b、將ffmpeg.exe的路徑配置到環境變量裏的Path裏

三、ffmpeg驗證

Alt+r,輸入cmd,在dos命令行輸入 ffmpeg

出現下列提示,即表示ffmpeg安裝成功

技術分享圖片

四、ffmpeg簡單應用

目前我是用來把錄制好的視頻轉換成圖片

ffmpeg.exe -i 路徑\待轉換的文件名.mp4 -r 30 -s 640*480 轉換後保存的路徑\文件夾名/%d.jpg

ffmpeg.exe -i C:\Users\Administrator\Desktop\video\20150407_174405.mp4 -r 30 -s 640x480 C:\Users\Administrator\Desktop\video/%d.jpg

-i 是選擇被執行文件

-r 30 是轉換後視頻的幀率,就是每秒的幀數

-s 640*480 是轉換後視屏的分辨率

C#代碼實現

首先,得下載個“ffmpeg.exe” 插件,然後把它放到你的項目中,如下圖:
程序中會調用該文件,以助於轉換音頻格式!

        /// <summary>
        /// 音頻格式轉換
        /// </summary>
        /// <param name="pathBefore">轉換前文件路徑/文件名</param>
        /// <param name="pathLater">轉換後文件路徑/文件名</param>
        /// <returns></returns>
        public string ConvertToMp3(string pathBefore, string pathLater)
        {
            //string pathBefore = MapPathFile("~/Attachment/AlertMessage/") + "20180309194843";
            //string pathLater = MapPathFile("~/Attachment/AlertMessage/") + "20180309194855.mp3";

            string c = MapPathFile("/Resources/ffmpeg/") + @"ffmpeg.exe -i " + pathBefore + " " + pathLater;
            string str = RunCmd(c);
            return str;
        }

        /// <summary>
        /// 執行Cmd命令
        /// </summary>
        private string RunCmd(string c)
        {
            try
            {
                ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
                info.RedirectStandardOutput = false;
                info.UseShellExecute = false;
                Process p = Process.Start(info);
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                p.StandardInput.WriteLine(c);
                p.StandardInput.AutoFlush = true;
                Thread.Sleep(1000);
                p.StandardInput.WriteLine("exit");
                p.WaitForExit();
                string outStr = p.StandardOutput.ReadToEnd();
                p.Close();
                return outStr;
            }
            catch (Exception ex)
            {
                return "error" + ex.Message;
            }
        }

ffmpeg下載安裝和簡單應用(C#音頻格式轉換)