123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using VisioForge.Shared.NAudio.CoreAudioApi;
- using VisioForge.Shared.NAudio.Wave;
-
- namespace Common.system
- {
- /// <summary>
- /// ffmpeg帮助类
- /// 需要安装\ffmpeg\bin\Setup Screen Capturer Recorder v0.12.10.exe
- /// 本地调试需要配置环境变量 将ffmpeg.exe位置配置到环境变量的path中
- /// </summary>
- public class FFMpeg
- {
- Process myProcess = null;
- /// <summary>
- /// ffmpeg输出日志文件地址 每个文件2MB左右
- /// </summary>
- string LogPath = "";
- /// <summary>
- /// 录制屏幕
- /// </summary>
- /// <param name="PathName">文件存储路径</param>
- /// <param name="VideoType">视频类型</param>
- public void StartRecordingVideo(string PathName, string VideoType)
- {
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- Debug.WriteLine(KillProcessArray.Length.ToString());
- foreach (var KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- string MicrophoneName = GetMicrophone();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
- switch (VideoType.ToUpper())
- {
- case "MP4":
- if (string.IsNullOrWhiteSpace(MicrophoneName))
- {
- 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的参数
- }
- else
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
- 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的参数
- }
- break;
- case "AVI":
- case "FLV":
- if (string.IsNullOrWhiteSpace(MicrophoneName))
- {
- 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 "+ VideoType.ToLower() + " " + PathName; //ffmpeg的参数
- }
- else
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
- 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 " + VideoType.ToLower() + " " + PathName; //ffmpeg的参数
- }
- break;
- default:
- if (string.IsNullOrWhiteSpace(MicrophoneName))
- {
- 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的参数
- }
- else
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
- 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的参数
- }
- break;
- }
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- myProcess.Start();
- myProcess.BeginErrorReadLine();
- }
-
- /// <summary>
- /// 录制音频
- /// </summary>
- /// <param name="Mp3Path">MP3音频位置</param>
- private void StartRecordingAudio(string Mp3Path)
- {
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- Debug.WriteLine(KillProcessArray.Length.ToString());
- foreach (var KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
- string MicrophoneName = GetMicrophone();
- if (string.IsNullOrWhiteSpace(MicrophoneName))
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + Mp3Path;
- }
- else
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
- MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + Mp3Path;
- }
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- myProcess.Start(); //启动线程
- myProcess.BeginErrorReadLine(); //开始异步读取
- }
-
- /// <summary>
- /// 停止录制
- /// </summary>
- public void StopFFmpeg()
- {
- //Thread.Sleep(3000);
- if (myProcess == null)
- return;
- myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
- myProcess.Close();//关闭进程
- myProcess.Dispose();//释放资源
- myProcess = null;
- //myProcess.Kill();
- }
-
- /// <summary>
- /// 合成视频
- /// </summary>
- /// <param name="ImageListPath">图片列表位置 命名为1.png 2.png</param>
- /// <param name="Mp3Path">MP3音频位置</param>
- /// <param name="VideoSavePath">视频保存位置</param>
- /// <param name="Frequency">每秒帧率</param>
- /// <param name="VideoWidth">视频宽度</param>
- /// <param name="VideoHeight">视频高度</param>
- /// <param name="VideoType">视频类型 MP4 FLV AVI</param>
- private void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight, string VideoType)
- {
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- Debug.WriteLine(KillProcessArray.Length.ToString());
- foreach (var KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- LogPath=CreateffmpegLog();
- this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
- switch (VideoType.ToUpper())
- {
- case "MP4":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png -i " +
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
- VideoSavePath;
- break;
- case "AVI":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png -i " +
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
- VideoSavePath;
- break;
- case "FLV":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png -i " +
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
- VideoSavePath;
- break;
- default:
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png -i " +
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
- VideoSavePath;
- break;
- }
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- myProcess.Start(); //启动线程
- myProcess.BeginErrorReadLine(); //开始异步读取
- myProcess.WaitForExit(); //阻塞等待进程结束
- myProcess.Close(); //关闭进程
- myProcess.Dispose(); //释放资源
- //生成视频后删除图片保存列表
- FileToolsCommon.DeleteDirectory(ImageListPath);
-
- }
-
- /// <summary>
- /// 生成缩略图
- /// </summary>
- /// <param name="VideoPath">视频地址</param>
- /// <param name="ImagePath">图片地址</param>
- void GenerateThumbnails(string VideoPath,string ImagePath)
- {
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- Debug.WriteLine(KillProcessArray.Length.ToString());
- foreach (var KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
- //myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -s " + thubWidth + "*" + thubHeight + " -f image2 \"" + ImagePath + "\"";
- myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -f image2 \"" + ImagePath + "\"";
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- myProcess.Start(); //启动线程
- myProcess.BeginErrorReadLine(); //开始异步读取
- myProcess.WaitForExit(); //阻塞等待进程结束
- myProcess.Close(); //关闭进程
- myProcess.Dispose(); //释放资源
- }
-
- /// <summary>
- /// 创建日志文件
- /// </summary>
- /// <returns></returns>
- string CreateffmpegLog()
- {
- string LogFileName = DateTime.Now.ToString("yyyyMMdd");
- string LogFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/FFMpegLog/");
- FileToolsCommon.CreateDirectory(LogFilePath);
- string LogName = LogFilePath + LogFileName + ".log";
- try
- {
- if (!FileToolsCommon.IsExistFile(LogName))
- {
- FileToolsCommon.CreateFile(LogName);
- FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
- "****************************************************************************************************\r\n");
- return LogName;
- }
- else
- {
- int num = 0;
- while (FileToolsCommon.GetFileSizeByMB(LogName) > 2)
- {
- num++;
- LogName = LogFilePath + LogFileName + "_" + num + ".log";
- FileToolsCommon.CreateFile(LogName);
- }
- FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
- "****************************************************************************************************\r\n");
- return LogName;
- }
- }
- catch (Exception)
- {
- return LogName;
- }
- }
-
- /// <summary>
- /// 输出结果
- /// </summary>
- /// <param name="sendProcess"></param>
- /// <param name="output"></param>
- private void Output(object sendProcess, DataReceivedEventArgs output)
- {
- if (!String.IsNullOrEmpty(output.Data))
- {
- FileToolsCommon.AppendText(LogPath, output.Data);
- }
- }
-
- //private void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
- //{
- // //+= new DataReceivedEventHandler((s, message) => { Console.WriteLine(message.Data); })
- // using (StreamWriter fs = new StreamWriter("E:\\项目\\测试\\Wpf测试\\bin\\Debug\\ffmpeg\\log.txt", true))
- // {
- // fs.WriteLine(e.Data);
- // Debug.WriteLine(output.Data.ToString());
- // }
- //}
-
- /// <summary>
- /// 获取麦克风
- /// </summary>
- string GetMicrophone()
- {
- List<string> devs = new List<string>();
-
- MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
- MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
- for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
- {
- WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
- foreach (MMDevice device in deviceCollection)
- {
- try
- {
-
- if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
- {
- devs.Add(device.FriendlyName);
- break;
- }
- }
- catch (Exception)
- {
-
- }
- }
- }
- if (devs.Count > 0)
- {
- return devs[0];
- }
- else
- {
- return "";
- }
- }
- }
- }
|