天天看點

使用.NET Core搭建分布式音頻效果處理服務(二)建立基于FFMpeg的Web程式

使用.NET Core搭建分布式音頻效果處理服務(二)建立基于FFMpeg的Web程式

準備工作:

1:Net Core 2.1 為何要用2.1,因為在macOS 10.13上一個奇怪的問題,請看另一篇博文

介紹

2:FFmpeg 版本無所謂,最新即可,安裝教程網上很多,當然也可以使用docker進行ffmpeg的部署,下載下傳位址 http://ffmpeg.org/download.html

3:Rider 2018.1.2 在多平台上,比微軟爸爸的VS好用,在window上肯定不如VS,當然你喜歡VSCODE也可以,畢竟JET的全家桶更好吃,哈哈。

目前準備工作就這樣,後續需要添加的時候再一一告知。

好了,開始碼磚。

建立一個簡單的Net core web api應用程式

當你使用dotnet new或者使用IDE建立完成一個web項目後,該項目就已經可以運作,不過隻是一個空殼子,我們需要一些存儲全局變量和控制程式啟動的方式。在Program.cs中的Main函數修改如下

1 public static void Main(string[] args)
 2 {
 3     var config = new ConfigurationBuilder()
 4         .AddCommandLine(args)
 5         .Build();
 6 
 7     General.ConfigurationBuilder = config;
 8     General.LocalHostUrl = config["ASPNETCORE_URLS"];
 9     General.isOpenMiddleware = bool.Parse(config["OPEN_MIDDLEWARE"]);
10 
11     var host = new WebHostBuilder()
12         .UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
13         .UseUrls(config["ASPNETCORE_URLS"])
14         .UseConfiguration(config)
15         .UseKestrel()
16         .UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
17         .UseIISIntegration()
18         .UseStartup<Startup>()
19         .Build();
20 
21     host.Run();
22 }      

相信不用解釋太多,各位也明白上面每條語句的作用。筆者使用General靜态類存儲了來之外部指令的啟動參數,這種方式可以避免使用launcthSettings.json進行檔案配置,通過啟動參數直接給定運作方式和環境配置。在使用的過程中可以通過例如:--ASPNETCORE_ENVIRONMENT=Development --ASPNETCORE_URLS=http://localhost:5023 --OPEN_MIDDLEWARE=true進行啟動,前兩個參數不解釋,第三個參數是為了友善多台伺服器部署時候,是否啟動相關的自定義中間件(相關中間件後續章節介紹)。既然本節介紹FFmpeg的使用,那麼接下來我們讨論FFmpeg的使用方式。

ffmpeg是什麼

從度娘(當然你要用谷歌也行)上面抄個介紹:FFmpeg是一套可以用來記錄、轉換數字音頻、視訊,并能将其轉化為流的開源計算機程式。采用LGPL或GPL許可證。它提供了錄制、轉換以及流化音視訊的完整解決方案。它包含了非常先進的音頻/視訊編解碼庫libavcodec,為了保證高可移植性和編解碼品質,libavcodec裡很多code都是從頭開發的。

簡單的說:就是可以處理音視訊媒體的開源程式。

安裝方式這裡不做闡述,安裝完成後輸入ffmpeg會得到如下資訊。

使用.NET Core搭建分布式音頻效果處理服務(二)建立基于FFMpeg的Web程式

請忽略截圖中的問号和亂碼,不想去折騰zsh。

在http://ffmpeg.org/官網上有詳細指令的使用和介紹,這裡介紹個簡單的指令

ffmpeg -i input.mp4 output.avi      

該指令輸入一個input.mp4 檔案,輸出一個output.avi檔案,ffmpeg自動将mp4轉碼為avi檔案并輸出到目前硬碟目錄,就這麼簡單!

通過Process類進行簡單的程序通訊

當然,我們不可能通過純手動的指令行的方式去得到自己想要的檔案,這種方式對于伺服器而言也不可取,不過,Net為我們提供了Process進行與程序間通訊的簡單通路,其實最多也就是指令行的調用,編寫一個通用的處理方法,友善不同地方的調用。

1 public void DoProcessing(string param, ProcessType processType = ProcessType.Ffmpeg)
 2 {
 3     try
 4     {
 5         _process.StartInfo.FileName = GetEnvironmentalFfmpeg(GetProcessName(processType));
 6         _process.StartInfo.Arguments = param;
 7         _process.StartInfo.CreateNoWindow = true;
 8         _process.StartInfo.UseShellExecute = false;
 9         _process.StartInfo.RedirectStandardOutput = true;
10         _process.StartInfo.RedirectStandardInput = true;
11         _process.StartInfo.RedirectStandardError = true;
12 
13         _process.ErrorDataReceived += (sender, args) =>
14         {
15             if (sender is Process p && p.HasExited && p.ExitCode == 1)
16             {
17                 Console.WriteLine("have an error:" + args.Data);
18             }
19         };
20 
21         if (processType == ProcessType.Ffprobe)
22         {
23             _process.ErrorDataReceived += (sender, args) =>
24             {
25                 if (args.Data == "") return;
26                 FfprobeDataReceivedEventHandlerArgs?.Invoke(sender, args);
27             };
28         }
29 
30         _process.Start();
31         _process.BeginErrorReadLine();
32         _process.WaitForExit();
33     }
34     catch (Exception ex)
35     {
36         Console.WriteLine(ex);
37     }
38     finally
39     {
40         _process.Close();
41         _process.Dispose();
42     }
43 }      

以上實作方式都非常簡單,這裡筆者增加了一個委托FfprobeDataReceivedEventHandlerArgs函數,友善觸發輸出事件ErrorDataReceived在其他類中友善被調用,而Ffprobe能擷取到音視訊媒體的詳細資訊,後面代碼中會介紹。

GetEnvironmentalFfmpeg是筆者為了偷懶,在windows中并沒直接安裝ffmpeg程式,而是将exe程式直接綁在了項目dll中,友善該項目在其他win平台的二次調用,而免去再次安裝的繁瑣問題(linux和mac沒法偷懶,除非用docker直接拷貝鏡像檔案)

GetProcessName函數就是一個主指令的選擇方式,ffmpeg中包含三個主要指令,ffmpeg用于處理音視訊,ffplay用于播放,ffprobe用于擷取資訊,常用的主指令也就ffmpeg和ffprobe。

簡單的參數工廠

當然,我們通過一個簡單的主函數去調用ffmpeg指令是遠遠不夠的,還需要根據不同的需求封裝一下參數字元串的拼接方式,畢竟這麼多參數我可記不住,呵呵。筆者提供一個思路和模闆,有興趣的朋友的借鑒和參考一下。

1 /// <summary>
  2 /// FFMPEG參數構造工廠
  3 /// </summary>
  4 public class AudioParamFactory
  5 {
  6     private static string GetRandFileName()
  7     {
  8         return GetDictory() + "temp/" + GetRandomString(6, true, true, true, false, "") + ".mp3";
  9     }
 10 
 11     private static string GetDictory()
 12     {
 13         return AppDomain.CurrentDomain.BaseDirectory;
 14     }
 15 
 16     /// <summary>
 17     /// 調整音量大小
 18     /// </summary>
 19     /// <param name="inputFilePath"></param>
 20     /// <param name="volumeSize"></param>
 21     /// <returns></returns>
 22     public AudioParamConstructor AdjustVolume(string inputFilePath, int volumeSize = 100)
 23     {
 24         var outputFile = GetRandFileName();
 25         return new AudioParamConstructor
 26         {
 27             Paramter = $"-i {inputFilePath} " +
 28                        $"-vol {volumeSize} {outputFile} -y",
 29             NewFileName = outputFile
 30         };
 31     }
 32 
 33     /// <summary>
 34     /// 合并兩個音頻檔案
 35     /// </summary>
 36     /// <param name="inputFile1"></param>
 37     /// <param name="inputFile2"></param>
 38     /// <returns></returns>
 39     public AudioParamConstructor MergeTwoAudio(string inputFile1, string inputFile2)
 40     {
 41         var outputFile = GetRandFileName();
 42         return new AudioParamConstructor
 43         {
 44             Paramter = $"-i {inputFile1} " +
 45                        $"-i {inputFile2} " +
 46                        "-filter_complex amix=inputs=2:duration=first:dropout_transition=10 -y " +
 47                        $"{outputFile}",
 48             NewFileName = outputFile
 49         };
 50     }
 51 
 52     /// <summary>
 53     /// 拆分音頻檔案
 54     /// </summary>
 55     /// <param name="inputFile1"></param>
 56     /// <param name="startTime"></param>
 57     /// <param name="durtionTime"></param>
 58     /// <returns></returns>
 59     public AudioParamConstructor InterceptAudio(string inputFile1, TimeSpan startTime, TimeSpan durtionTime)
 60     {
 61         var outputFile = GetRandFileName();
 62         return new AudioParamConstructor
 63         {
 64             Paramter = $"-i {inputFile1} -vn -acodec copy -ss " +
 65                        $"{startTime.Hours:00}:{startTime.Minutes:00}:{startTime.Seconds:00}.{startTime.Milliseconds:000} -t " +
 66                        $"{durtionTime.Hours:00}:{durtionTime.Minutes:00}:{durtionTime.Seconds:00}.{durtionTime.Milliseconds:000} " +
 67                        $"{outputFile}",
 68             NewFileName = outputFile
 69         };
 70     }
 71 
 72     /// <summary>
 73     /// 拼接多個音頻檔案
 74     /// </summary>
 75     /// <param name="inputList"></param>
 76     /// <returns></returns>
 77     public AudioParamConstructor SplicingAudio(IEnumerable<string> inputList)
 78     {
 79         var splic = inputList.Aggregate("", (current, input) => current + input + "|");
 80         splic = splic.Remove(splic.Length - 1, 1);
 81         splic = $"\"concat:{splic}\"";
 82         var outputFile = GetRandFileName();
 83         return new AudioParamConstructor
 84         {
 85             Paramter = $"-i {splic} -acodec copy {outputFile}",
 86             NewFileName = outputFile
 87         };
 88     }
 89 
 90     /// <summary>
 91     /// 擷取音頻檔案資訊
 92     /// </summary>
 93     /// <param name="inputFile"></param>
 94     /// <returns></returns>
 95     public string GetFileInfo(string inputFile)
 96     {
 97         return $"-i {inputFile} -print_format json -v 0 -show_format";
 98     }
 99 
100     /// <summary>
101     /// 鍵入效果
102     /// </summary>
103     /// <param name="inputFile"></param>
104     /// <returns></returns>
105     public AudioParamConstructor FadeIn(string inputFile, int startSecends)
106     {
107         var outputFile = GetRandFileName();
108         return new AudioParamConstructor
109         {
110             Paramter = $"-i {inputFile} -filter_complex afade=t=in:ss={startSecends}:d=2 {outputFile}",
111             NewFileName = outputFile
112         };
113     }
114 
115     /// <summary>
116     /// 漸出效果
117     /// </summary>
118     /// <param name="inputFile"></param>
119     /// <returns></returns>
120     public AudioParamConstructor FadeOut(string inputFile, int startSecends)
121     {
122         var outputFile = GetRandFileName();
123         return new AudioParamConstructor
124         {
125             Paramter = $"-i {inputFile} -filter_complex afade=t=out:st={startSecends}:d=2 {outputFile}",
126             NewFileName = outputFile
127         };
128     }
129 
130     ///<summary>
131     ///生成随機字元串
132     ///</summary>
133     ///<param name="length">目标字元串的長度</param>
134     ///<param name="useNum">是否包含數字,1=包含,預設為包含</param>
135     ///<param name="useLow">是否包含小寫字母,1=包含,預設為包含</param>
136     ///<param name="useUpp">是否包含大寫字母,1=包含,預設為包含</param>
137     ///<param name="useSpe">是否包含特殊字元,1=包含,預設為不包含</param>
138     ///<param name="custom">要包含的自定義字元,直接輸入要包含的字元清單</param>
139     ///<returns>指定長度的随機字元串</returns>
140     public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe,
141         string custom)
142     {
143         byte[] b = new byte[4];
144         new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
145         Random r = new Random(BitConverter.ToInt32(b, 0));
146         string s = null, str = custom;
147         if (useNum == true)
148         {
149             str += "0123456789";
150         }
151 
152         if (useLow == true)
153         {
154             str += "abcdefghijklmnopqrstuvwxyz";
155         }
156 
157         if (useUpp == true)
158         {
159             str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
160         }
161 
162         if (useSpe == true)
163         {
164             str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
165         }
166 
167         for (int i = 0; i < length; i++)
168         {
169             s += str.Substring(r.Next(0, str.Length - 1), 1);
170         }
171 
172         return s;
173     }
174 }      

至此,我們在Net Core Web App上面已經建立了一個基于ffmpeg的Web程式,目前運作沒有任何效果的,下一節我們将這個Web程式進行小部分的完善,并開始處理音頻檔案的第一個問題。

感謝閱讀!