星火微课系统客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using VisioForge.Shared.NAudio.CoreAudioApi;
  6. using VisioForge.Shared.NAudio.Wave;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// ffmpeg帮助类
  11. /// 创建人:赵耀
  12. /// 创建时间::2020年8月22日
  13. /// 需要安装\ffmpeg\bin\Setup Screen Capturer Recorder v0.12.10.exe
  14. /// 本地调试需要配置环境变量 将ffmpeg.exe位置配置到环境变量的path中
  15. /// </summary>
  16. public class FFMpeg
  17. {
  18. Process myProcess = null;
  19. /// <summary>
  20. /// ffmpeg输出日志文件地址 每个文件2MB左右
  21. /// </summary>
  22. string LogPath = "";
  23. /// <summary>
  24. /// 录制屏幕
  25. /// </summary>
  26. /// <param name="FilePath">文件存储路径</param>
  27. public void StartRecordingVideo(string FilePath)
  28. {
  29. //路径
  30. string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
  31. string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
  32. //文件保存路径
  33. string PathName = GetFilePathName(FilePath);
  34. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  35. Debug.WriteLine(KillProcessArray.Length.ToString());
  36. foreach (var KillProcess in KillProcessArray)
  37. {
  38. KillProcess.Kill();
  39. }
  40. myProcess = new Process();
  41. LogPath = CreateffmpegLog();
  42. string MicrophoneName = GetMicrophone();
  43. myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  44. switch (Extension.ToUpper())
  45. {
  46. case "MP4":
  47. if (string.IsNullOrWhiteSpace(MicrophoneName))
  48. {
  49. myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f avi " + PathName; //ffmpeg的参数
  50. }
  51. else
  52. {
  53. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  54. MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 " + PathName; //ffmpeg的参数
  55. }
  56. break;
  57. case "AVI":
  58. case "FLV":
  59. if (string.IsNullOrWhiteSpace(MicrophoneName))
  60. {
  61. myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
  62. }
  63. else
  64. {
  65. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  66. MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 -f " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
  67. }
  68. break;
  69. default:
  70. if (string.IsNullOrWhiteSpace(MicrophoneName))
  71. {
  72. myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f avi " + PathName; //ffmpeg的参数
  73. }
  74. else
  75. {
  76. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  77. MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 " + PathName; //ffmpeg的参数
  78. }
  79. break;
  80. }
  81. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  82. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  83. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  84. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  85. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  86. myProcess.Start();
  87. myProcess.BeginErrorReadLine();
  88. }
  89. /// <summary>
  90. /// 录制音频
  91. /// </summary>
  92. /// <param name="Mp3Path">MP3音频位置</param>
  93. public void StartRecordingAudio(string Mp3Path)
  94. {
  95. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  96. Debug.WriteLine(KillProcessArray.Length.ToString());
  97. foreach (var KillProcess in KillProcessArray)
  98. {
  99. KillProcess.Kill();
  100. }
  101. myProcess = new Process();
  102. LogPath = CreateffmpegLog();
  103. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  104. string MicrophoneName = GetMicrophone();
  105. if (string.IsNullOrWhiteSpace(MicrophoneName))
  106. {
  107. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + Mp3Path;
  108. }
  109. else
  110. {
  111. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  112. MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + Mp3Path;
  113. }
  114. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  115. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  116. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  117. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  118. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  119. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  120. myProcess.Start(); //启动线程
  121. myProcess.BeginErrorReadLine(); //开始异步读取
  122. }
  123. /// <summary>
  124. /// 暂停录制
  125. /// </summary>
  126. public void SuspendFFmpeg()
  127. {
  128. if (myProcess == null)
  129. return;
  130. myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
  131. myProcess.Close();//关闭进程
  132. myProcess.Dispose();//释放资源
  133. myProcess = null;
  134. //myProcess.Kill();
  135. }
  136. /// <summary>
  137. /// 停止录制并合成文件
  138. /// </summary>
  139. /// <param name="FilePath">文件存储路径 必须与开始时的路径完全一致</param>
  140. public void StopFFmpeg(string FilePath)
  141. {
  142. //文件完整路径
  143. if (myProcess == null)
  144. return;
  145. myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
  146. myProcess.Close();//关闭进程
  147. myProcess.Dispose();//释放资源
  148. myProcess = null;
  149. Thread t1 = new Thread(MergeVideoOrAudio);
  150. t1.Start(FilePath);
  151. //文件合成
  152. }
  153. /// <summary>
  154. /// 图片音频合成视频
  155. /// </summary>
  156. /// <param name="ImageListPath">图片列表位置 命名为1.png 2.png</param>
  157. /// <param name="Mp3Path">MP3音频位置</param>
  158. /// <param name="VideoSavePath">视频保存位置</param>
  159. /// <param name="Frequency">每秒帧率</param>
  160. /// <param name="VideoWidth">视频宽度</param>
  161. /// <param name="VideoHeight">视频高度</param>
  162. public void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight)
  163. {
  164. Thread thread = new Thread(delegate ()
  165. {
  166. //// 允许不同线程间的调用
  167. //Control.CheckForIllegalCrossThreadCalls = false;
  168. Thread.Sleep(500);
  169. string Extension = FileToolsCommon.GetIOExtension(VideoSavePath);//扩展名
  170. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  171. Debug.WriteLine(KillProcessArray.Length.ToString());
  172. foreach (var KillProcess in KillProcessArray)
  173. {
  174. KillProcess.Kill();
  175. }
  176. myProcess = new Process();
  177. LogPath = CreateffmpegLog();
  178. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  179. switch (Extension.ToUpper())
  180. {
  181. case "MP4":
  182. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  183. ImageListPath + @"%d.png -i " +
  184. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
  185. VideoSavePath;
  186. break;
  187. case "AVI":
  188. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  189. ImageListPath + @"%d.png -i " +
  190. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
  191. VideoSavePath;
  192. break;
  193. case "FLV":
  194. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  195. ImageListPath + @"%d.png -i " +
  196. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
  197. VideoSavePath;
  198. break;
  199. default:
  200. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  201. ImageListPath + @"%d.png -i " +
  202. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
  203. VideoSavePath;
  204. break;
  205. }
  206. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  207. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  208. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  209. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  210. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  211. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  212. myProcess.Start(); //启动线程
  213. myProcess.BeginErrorReadLine(); //开始异步读取
  214. myProcess.WaitForExit(); //阻塞等待进程结束
  215. myProcess.Close(); //关闭进程
  216. myProcess.Dispose(); //释放资源
  217. //生成视频后删除图片保存列表
  218. FileToolsCommon.DeleteDirectory(ImageListPath);
  219. });
  220. }
  221. /// <summary>
  222. /// 视频音频合成
  223. /// </summary>
  224. /// <param name="FilePath">存储路径</param>
  225. void MergeVideoOrAudio(object FilePathobj)
  226. {
  227. Thread.Sleep(500);
  228. //路径
  229. string FilePath = (string)FilePathobj;
  230. string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
  231. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  232. Debug.WriteLine(KillProcessArray.Length.ToString());
  233. foreach (var KillProcess in KillProcessArray)
  234. {
  235. KillProcess.Kill();
  236. }
  237. myProcess = new Process();
  238. LogPath = CreateffmpegLog();
  239. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  240. myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temp/filelist.d -c copy " + FilePath;
  241. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  242. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  243. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  244. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  245. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  246. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  247. myProcess.Start(); //启动线程
  248. myProcess.BeginErrorReadLine(); //开始异步读取
  249. myProcess.WaitForExit(); //阻塞等待进程结束
  250. myProcess.Close(); //关闭进程
  251. myProcess.Dispose(); //释放资源
  252. }
  253. /// <summary>
  254. /// 获取文件完整路径
  255. /// </summary>
  256. /// <param name="FilePath">文件路径</param>
  257. /// <returns></returns>
  258. string GetFilePathName(string FilePath)
  259. {
  260. //路径
  261. string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
  262. //Path.GetFileName(FilePath);//获取文件名
  263. string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
  264. string tempFilePath = Path + "temp/";
  265. //创建临时目录
  266. FileToolsCommon.CreateDirectory(tempFilePath);
  267. //创建记录文件
  268. if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
  269. {
  270. FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
  271. }
  272. int num = 1;
  273. string CompleteFilePath = tempFilePath + num + Extension;
  274. while (FileToolsCommon.IsExistFile(CompleteFilePath))
  275. {
  276. num++;
  277. CompleteFilePath = tempFilePath + num + Extension;
  278. }
  279. FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
  280. return CompleteFilePath;
  281. }
  282. /// <summary>
  283. /// 生成缩略图
  284. /// </summary>
  285. /// <param name="VideoPath">视频地址</param>
  286. /// <param name="ImagePath">图片地址</param>
  287. public void GenerateThumbnails(string VideoPath, string ImagePath)
  288. {
  289. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  290. Debug.WriteLine(KillProcessArray.Length.ToString());
  291. foreach (var KillProcess in KillProcessArray)
  292. {
  293. KillProcess.Kill();
  294. }
  295. myProcess = new Process();
  296. LogPath = CreateffmpegLog();
  297. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  298. //myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -s " + thubWidth + "*" + thubHeight + " -f image2 \"" + ImagePath + "\"";
  299. myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -f image2 \"" + ImagePath + "\"";
  300. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  301. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  302. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  303. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  304. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  305. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  306. myProcess.Start(); //启动线程
  307. myProcess.BeginErrorReadLine(); //开始异步读取
  308. myProcess.WaitForExit(); //阻塞等待进程结束
  309. myProcess.Close(); //关闭进程
  310. myProcess.Dispose(); //释放资源
  311. }
  312. /// <summary>
  313. /// 创建日志文件
  314. /// </summary>
  315. /// <returns></returns>
  316. string CreateffmpegLog()
  317. {
  318. string LogFileName = DateTime.Now.ToString("yyyyMMdd");
  319. string LogFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/FFMpegLog/");
  320. FileToolsCommon.CreateDirectory(LogFilePath);
  321. string LogName = LogFilePath + LogFileName + ".log";
  322. try
  323. {
  324. if (!FileToolsCommon.IsExistFile(LogName))
  325. {
  326. FileToolsCommon.CreateFile(LogName);
  327. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  328. "****************************************************************************************************\r\n");
  329. return LogName;
  330. }
  331. else
  332. {
  333. int num = 0;
  334. while (FileToolsCommon.GetFileSizeByMB(LogName) > 2)
  335. {
  336. num++;
  337. LogName = LogFilePath + LogFileName + "_" + num + ".log";
  338. FileToolsCommon.CreateFile(LogName);
  339. }
  340. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  341. "****************************************************************************************************\r\n");
  342. return LogName;
  343. }
  344. }
  345. catch (Exception)
  346. {
  347. return LogName;
  348. }
  349. }
  350. /// <summary>
  351. /// 输出结果
  352. /// </summary>
  353. /// <param name="sendProcess"></param>
  354. /// <param name="output"></param>
  355. private void Output(object sendProcess, DataReceivedEventArgs output)
  356. {
  357. if (!String.IsNullOrEmpty(output.Data))
  358. {
  359. FileToolsCommon.AppendText(LogPath, output.Data);
  360. }
  361. }
  362. //private void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  363. //{
  364. // //+= new DataReceivedEventHandler((s, message) => { Console.WriteLine(message.Data); })
  365. // using (StreamWriter fs = new StreamWriter("E:\\项目\\测试\\Wpf测试\\bin\\Debug\\ffmpeg\\log.txt", true))
  366. // {
  367. // fs.WriteLine(e.Data);
  368. // Debug.WriteLine(output.Data.ToString());
  369. // }
  370. //}
  371. /// <summary>
  372. /// 获取麦克风
  373. /// </summary>
  374. string GetMicrophone()
  375. {
  376. List<string> devs = new List<string>();
  377. MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
  378. MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
  379. for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
  380. {
  381. WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
  382. foreach (MMDevice device in deviceCollection)
  383. {
  384. try
  385. {
  386. if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
  387. {
  388. devs.Add(device.FriendlyName);
  389. break;
  390. }
  391. }
  392. catch (Exception)
  393. {
  394. }
  395. }
  396. }
  397. if (devs.Count > 0)
  398. {
  399. return devs[0];
  400. }
  401. else
  402. {
  403. return "";
  404. }
  405. }
  406. }
  407. }