ffmpegUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace IPADDemo.Util
  7. {
  8. public class ffmpegUtils
  9. {
  10. private static string url = System.IO.Directory.GetCurrentDirectory();
  11. private static ffmpegUtils instance;
  12. private static object _lock = new object();
  13. private static int maxNum = 10;
  14. private ffmpegUtils()
  15. {
  16. Init();
  17. }
  18. public static ffmpegUtils GetInstance()
  19. {
  20. if (instance == null)
  21. {
  22. lock (_lock)
  23. {
  24. if (instance == null)
  25. {
  26. instance = new ffmpegUtils();
  27. }
  28. }
  29. }
  30. return instance;
  31. }
  32. /// <summary>
  33. ///删除中间件pcm文件
  34. /// </summary>
  35. private static void Init()
  36. {
  37. if (!Directory.Exists(System.IO.Directory.GetCurrentDirectory() + @"\cache\"))
  38. {
  39. Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + @"\cache\");
  40. }
  41. foreach (string d in Directory.GetFileSystemEntries(System.IO.Directory.GetCurrentDirectory() + @"\cache\"))
  42. {
  43. if (File.Exists(d))
  44. {
  45. try
  46. {
  47. FileInfo fi = new FileInfo(d);
  48. if (fi.Extension.ToLower() == ".pcm")
  49. {
  50. if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
  51. fi.Attributes = FileAttributes.Normal;
  52. File.Delete(d);//删除pcm中间文件
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. Console.WriteLine(ex.Message.ToString());
  58. }
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// amr 转换 mp3 格式
  64. /// </summary>
  65. /// <param name="amrFile">amr文件所在路径</param>
  66. /// <returns></returns>
  67. public bool ConvertAmrToMp3(string amrFile, string savePath)
  68. {
  69. //if (!File.Exists(amrFile))
  70. //{
  71. // MyLog.LogW("当前目录不存在amr文件" + amrFile);
  72. // return false;
  73. //}
  74. string filePath = savePath;
  75. string fileName = amrFile.Substring(amrFile.LastIndexOf(@"\"));
  76. string finalName = fileName.Substring(1, fileName.IndexOf(".") - 1);
  77. string guid = Guid.NewGuid().ToString();
  78. string copyFile = @"silk_v3_encoder_" + guid;
  79. //string copyPath = url + @"\software\" + copyFile + ".exe";
  80. //System.IO.File.Copy(url + @"\silk_v3_decoder.exe", copyPath, true);
  81. string copyPath = url + @"\ffmpeg\silk_v3_decoder.exe";
  82. //amr解码为pcm格式
  83. string amrTopcm = "\"" + copyPath + "\" \"" + amrFile + "\" \"" + url + @"\cache\" + finalName + ".pcm\" -quiet";
  84. //pcm转码为MP3格式
  85. string pcmTomp3 = "\"" + url + "\\ffmpeg\\ffmpeg.exe\" -y -f s16le -ar 24000 -ac 1 -i \"" + url + @"\cache\" + finalName + ".pcm\"" + " \"" + filePath+ "\"";
  86. int indexNum = 1;
  87. try
  88. {
  89. bool b = FormatConvert(amrTopcm);
  90. string pcmdir = url + @"\cache\" + finalName + ".pcm";
  91. return ContinueTryConvert(amrFile, pcmdir, b, pcmTomp3, filePath, finalName, indexNum, copyFile, savePath);
  92. }
  93. catch (Exception)
  94. {
  95. return false;
  96. }
  97. }
  98. /// <summary>
  99. /// MP3 转换为 amr格式
  100. /// </summary>
  101. /// <param name="mp3File"></param>
  102. /// <returns></returns>
  103. public bool ConvertMp3ToAmr(string mp3File, string savePath)
  104. {
  105. //if (!File.Exists(mp3File))
  106. //{
  107. // return false;
  108. //}
  109. string filePath = savePath;
  110. string fileName = "";
  111. if (mp3File.LastIndexOf(@"\") != -1)
  112. {
  113. fileName = mp3File.Substring(mp3File.LastIndexOf(@"\"));
  114. }
  115. else {
  116. fileName = mp3File;
  117. }
  118. string finalName = fileName.Substring(1, fileName.IndexOf(".") - 1);
  119. string guid = Guid.NewGuid().ToString();
  120. string copyFile = @"silk_v3_encoder_" + guid;
  121. //string copyPath = url + @"\software\" + copyFile + ".exe";
  122. //System.IO.File.Copy(url + @"\silk_v3_encoder.exe", copyPath, true);
  123. string copyPath = url + @"\ffmpeg\silk_v3_encoder.exe";
  124. //mp3转码位pcm格式
  125. string mp3Topcm = "\"" + url + "\\ffmpeg\\ffmpeg.exe\" -i \"" + mp3File + "\" -f s16le -acodec pcm_s16le -ac 1 -ar 24000 \"" + url + @"\cache\" + finalName + ".pcm\"";
  126. //pcm转码为amr格式
  127. string pcmTomp3 = "\"" + copyPath + "\" \"" + url + @"\cache\" + finalName + ".pcm\"" + " \"" + filePath + "\" -tencent";
  128. int indexNum = 1;
  129. try
  130. {
  131. bool b = FormatConvert(mp3Topcm);
  132. string pcmdir = url + @"\cache\" + finalName + ".pcm";
  133. return ContinueTryConvert(mp3File, pcmdir, b, pcmTomp3, filePath, finalName, indexNum, copyFile, savePath);
  134. }
  135. catch (Exception)
  136. {
  137. return false;
  138. }
  139. }
  140. private bool ContinueTryConvert(string originFile, string pcmdir, bool b, string strCommand, string filePath, string finalName, int indexNum, string copyFile, string savePath)
  141. {
  142. if (File.Exists(pcmdir))
  143. {
  144. if (b)
  145. {
  146. if (MyUtils.IsFileInUse(pcmdir))
  147. {
  148. Thread.Sleep(100);
  149. return ContinueTryConvert(originFile, pcmdir, b, strCommand, filePath, finalName, indexNum, copyFile, savePath);
  150. }
  151. bool isSuccese = FormatConvert(strCommand);
  152. #region 删除文件
  153. Task.Factory.StartNew(() =>
  154. {
  155. Thread.Sleep(10000);
  156. try
  157. {
  158. File.Delete(pcmdir);
  159. }
  160. catch (Exception ex)
  161. {
  162. }
  163. try
  164. {
  165. string path = url + @"\software\" + copyFile + ".exe";
  166. if (File.Exists(path))
  167. {
  168. new FileInfo(path).Attributes = FileAttributes.Normal;
  169. File.Delete(path);
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. }
  175. });
  176. #endregion 删除文件
  177. indexNum = 1;
  178. return ContinueSecondTryConvert(indexNum, filePath, strCommand);
  179. }
  180. else
  181. {
  182. return false;
  183. }
  184. }
  185. else
  186. {
  187. indexNum++;
  188. if (indexNum <= maxNum)
  189. {
  190. Thread.Sleep(200);
  191. return ContinueTryConvert(originFile, pcmdir, b, strCommand, filePath, finalName, indexNum, copyFile, savePath);
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. }
  199. private bool ContinueSecondTryConvert(int indexNum, string finaldir, string strCommand)
  200. {
  201. if (!File.Exists(finaldir))
  202. {
  203. indexNum++;
  204. if (indexNum <= maxNum)
  205. {
  206. Thread.Sleep(100 * indexNum);
  207. return ContinueSecondTryConvert(indexNum, finaldir, strCommand);
  208. }
  209. else
  210. {
  211. bool isSuccese = FormatConvert(strCommand);
  212. Thread.Sleep(1000);
  213. return File.Exists(finaldir);
  214. }
  215. }
  216. else
  217. {
  218. return true;
  219. }
  220. }
  221. /// <summary>
  222. ///解码/转码-格式
  223. ///@param silk 源silk文件,需要绝对路径! 例:F:\zhuanma\vg2ub41omgipvrmur1fnssd3tq.silk
  224. ///@param pcm 目标pcm文件,需要绝对路径 例:F:\zhuanma\vg2ub41omgipvrmur1fnssd3tq.pcm
  225. ///@return
  226. /// </summary>
  227. /// <param name="cmdStr"></param>
  228. /// <returns></returns>
  229. private static bool FormatConvert(string cmdStr)
  230. {
  231. bool flag = true;
  232. try
  233. {
  234. //CmdUtils.RunCmd(cmdStr);
  235. #region 老版本
  236. using (Process p = new Process())
  237. {
  238. string strInput = cmdStr;
  239. //设置要启动的应用程序
  240. p.StartInfo.FileName = "cmd.exe";
  241. //是否使用操作系统shell启动
  242. p.StartInfo.UseShellExecute = false;
  243. // 接受来自调用程序的输入信息
  244. p.StartInfo.RedirectStandardInput = true;
  245. //输出信息
  246. p.StartInfo.RedirectStandardOutput = true;
  247. // 输出错误
  248. p.StartInfo.RedirectStandardError = true;
  249. //不显示程序窗口
  250. p.StartInfo.CreateNoWindow = true;
  251. //启动程序
  252. p.Start();
  253. //向cmd窗口发送输入信息
  254. p.StandardInput.WriteLine(strInput + "&exit");
  255. p.StandardInput.AutoFlush = true;
  256. //p.WaitForExit();
  257. //获取输出信息
  258. //string strOuput = p.StandardOutput.ReadToEnd();
  259. ////等待程序执行完退出进程
  260. //p.Kill();
  261. //Thread.Sleep(1000);
  262. }
  263. #endregion
  264. }
  265. catch (Exception)
  266. {
  267. flag = false;
  268. }
  269. return flag;
  270. }
  271. }
  272. }