|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321 |
- using AForge.Video.DirectShow;
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
-
- using VisioForge.Shared.NAudio.CoreAudioApi;
- using VisioForge.Shared.NAudio.Wave;
-
- namespace Common.system
- {
-
-
-
-
-
-
-
- public class FFMpeg
- {
- #region 变量
- public Process myProcess = null;
-
-
-
-
- public bool OutputVideoLog = FileToolsCommon.GetConfigValue("OutputVideoLog") != "0";
-
-
-
- private string LogPath = "";
-
-
-
-
- private bool IsRecordSpeaker = true;
-
-
-
-
- private bool IsRecordMicrophone = true;
- #endregion
-
-
-
-
-
- public bool StartRecordingVideo(string FilePath, Size size, out string ErrMessage)
- {
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
- ErrMessage = null;
-
- string Path = FileToolsCommon.GetDirectoryName(FilePath);
- string Extension = FileToolsCommon.GetIOExtension(FilePath);
-
- string PathName = GetRSFilePathName(FilePath);
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- string MicrophoneName = null;
- #region 检测麦克风扬声器
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.CreateDirectory(AudioPath);
- string audioSpeakerPath = AudioPath + "adoS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";
- string audioMicrophonePath = AudioPath + "adoM" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";
- try
- {
- FileToolsCommon.DeleteFile(audioSpeakerPath);
- FileToolsCommon.DeleteFile(audioMicrophonePath);
- }
- catch (Exception)
- {
-
- }
- if (StartRecordSpeakerAudio(audioSpeakerPath))
- {
- IsRecordSpeaker = true;
- }
- else
- {
-
- IsRecordSpeaker = false;
- }
- StopRecordAudio(2);
- Thread.Sleep(100);
- if (StartRecordAudio(audioMicrophonePath))
- {
- IsRecordMicrophone = true;
- }
- else
- {
-
- IsRecordMicrophone = false;
- }
- StopRecordAudio(1);
- #endregion
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- string SpeakerStr = "";
- string MicrophoneStr = "";
- if (IsRecordSpeaker)
- {
- SpeakerStr = "-f dshow -i audio=\"virtual-audio-capturer\" ";
- }
- if (IsRecordMicrophone)
- {
- MicrophoneName = GetMicrophoneName();
- if (!string.IsNullOrWhiteSpace(MicrophoneName))
- {
- MicrophoneStr = "-f dshow -i audio=\"" + MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 ";
- }
- }
- switch (Extension.ToUpper())
- {
- case ".MP4":
- myProcess.StartInfo.Arguments = SpeakerStr + MicrophoneStr + " -f gdigrab -framerate 6 -offset_x 0 -offset_y 0 -video_size " + size.Width + "x" + size.Height + " -i desktop -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -acodec aac " + PathName;
-
-
-
-
-
-
-
-
-
-
- break;
- case ".AVI":
- case ".FLV":
- myProcess.StartInfo.Arguments = SpeakerStr + MicrophoneStr + " -f gdigrab -framerate 6 -offset_x 0 -offset_y 0 -video_size " + size.Width + "x" + size.Height + " -i desktop -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -acodec aac -f " + Extension.ToLower().Replace(".", "") + " " + PathName;
-
-
-
-
-
-
-
-
-
- break;
- default:
- myProcess.StartInfo.Arguments = SpeakerStr + MicrophoneStr + " -f gdigrab -framerate 6 -offset_x 0 -offset_y 0 -video_size " + size.Width + "x" + size.Height + " -i desktop -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -acodec aac " + PathName;
- break;
- }
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【录屏】:" + myProcess.StartInfo.Arguments);
- }
- FileToolsCommon.AppendText(LogPath, "【录屏】:" + myProcess.StartInfo.Arguments + "\r\n");
- myProcess.StartInfo.UseShellExecute = false;
- myProcess.StartInfo.RedirectStandardError = true;
- myProcess.StartInfo.CreateNoWindow = true;
- myProcess.StartInfo.RedirectStandardInput = true;
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- try
- {
- myProcess.Start();
- myProcess.BeginErrorReadLine();
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("访问"))
- {
- ErrMessage = "访问被拒绝,请关闭杀毒软件或新任此软件后重试!";
- }
- else
- {
- ErrMessage = ex.Message + " 请关闭杀毒软件或信任此软件后重试!";
- }
- LogHelper.WriteErrLog("【录音】(StartRecordingAudio)" + ErrMessage, ex);
- myProcess.Dispose();
- myProcess = null;
- return false;
- }
- return true;
- }
-
-
-
-
-
- public bool StartRecordingAudio(string Mp3Path, out string ErrMessage)
- {
- ErrMessage = null;
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
-
- string Path = FileToolsCommon.GetDirectoryName(Mp3Path);
-
- string PathName = GetFilePathName(Mp3Path);
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- string MicrophoneName = null;
- #region 检测麦克风扬声器
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.CreateDirectory(AudioPath);
- string audioSpeakerPath = AudioPath + "adoS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";
- string audioMicrophonePath = AudioPath + "adoM" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";
- try
- {
- FileToolsCommon.DeleteFile(audioSpeakerPath);
- FileToolsCommon.DeleteFile(audioMicrophonePath);
- }
- catch (Exception)
- {
-
- }
- if (StartRecordSpeakerAudio(audioSpeakerPath))
- {
- IsRecordSpeaker = true;
- }
- else
- {
-
- IsRecordSpeaker = false;
- }
- StopRecordAudio(2);
- Thread.Sleep(100);
- if (StartRecordAudio(audioMicrophonePath))
- {
- IsRecordMicrophone = true;
- MicrophoneName = GetMicrophoneName();
- }
- else
- {
-
- IsRecordMicrophone = false;
- }
- StopRecordAudio(1);
- if (!IsRecordSpeaker && !IsRecordMicrophone)
- {
- ErrMessage = "无法录制声音,请关闭杀毒软件,确保声卡和麦克风处于可用状态!";
- return false;
- }
- #endregion
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- if (IsRecordSpeaker && IsRecordMicrophone)
- {
- 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 " + PathName;
- }
- else if (IsRecordSpeaker)
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + PathName;
- }
- else if (IsRecordMicrophone)
- {
-
- if (!string.IsNullOrWhiteSpace(MicrophoneName))
- {
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"" +
- MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + PathName;
- }
- }
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【录音】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【录音】:" + myProcess.StartInfo.Arguments + "\r\n");
- myProcess.StartInfo.UseShellExecute = false;
- myProcess.StartInfo.RedirectStandardError = true;
- myProcess.StartInfo.CreateNoWindow = true;
- myProcess.StartInfo.RedirectStandardInput = true;
-
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- try
- {
- myProcess.Start();
- myProcess.BeginErrorReadLine();
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("访问"))
- {
- ErrMessage = "无法录制声音,访问被拒绝,请关闭杀毒软件或信任此软件后重试!";
- }
- else
- {
- ErrMessage = ex.Message + " 请关闭杀毒软件或信任此软件后重试!";
- }
- LogHelper.WriteErrLog("【录音】(StartRecordingAudio)" + ErrMessage, ex);
- myProcess.Dispose();
- myProcess = null;
- return false;
- }
- return true;
- }
-
-
-
-
- public bool SuspendFFmpeg()
- {
- if (myProcess != null)
- {
- myProcess.StandardInput.WriteLine("q");
- myProcess.Close();
- myProcess.Dispose();
- }
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
- return true;
-
- }
-
-
-
-
-
- public bool StopFFmpeg(string FilePath)
- {
-
- if (myProcess != null)
- {
- myProcess.StandardInput.WriteLine("q");
- myProcess.Close();
- myProcess.Dispose();
- }
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
- Thread t1 = new Thread(MergeVideoOrAudio);
- t1.Start(FilePath);
- return true;
-
- }
-
-
-
-
-
-
-
-
-
-
- public bool SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight, out string ErrMessage)
- {
-
-
-
-
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
- ErrMessage = null;
- string Extension = FileToolsCommon.GetIOExtension(VideoSavePath);
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
-
- foreach (Process KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- try
- {
-
- LogPath = CreateffmpegLog();
- string mp3Str = "";
- #region 判断音频是否存在
- if (!string.IsNullOrWhiteSpace(Mp3Path))
- {
- if (FileToolsCommon.IsExistFile(Mp3Path))
- {
- mp3Str = "-i " + Mp3Path;
- }
- }
- #endregion
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- switch (Extension.ToUpper())
- {
- case ".MP4":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png " +
- mp3Str + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
- VideoSavePath;
- break;
- case ".AVI":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png " +
- mp3Str + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
- VideoSavePath;
- break;
- case ".FLV":
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png " +
- mp3Str + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
- VideoSavePath;
- break;
- default:
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
- ImageListPath + @"%d.png " +
- mp3Str + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
- VideoSavePath;
- break;
- }
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【图片音频合成视频】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【图片音频合成视频】:" + myProcess.StartInfo.Arguments + "\r\n");
- myProcess.StartInfo.UseShellExecute = false;
- myProcess.StartInfo.RedirectStandardError = true;
- myProcess.StartInfo.CreateNoWindow = true;
- myProcess.StartInfo.RedirectStandardInput = true;
-
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- try
- {
- myProcess.Start();
- myProcess.BeginErrorReadLine();
- myProcess.WaitForExit();
- myProcess.Close();
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("访问"))
- {
- ErrMessage = "访问被拒绝,请关闭杀毒软件或新任此软件后重试!";
- }
- else
- {
- ErrMessage = ex.Message + " 请关闭杀毒软件或信任此软件后重试!";
- }
- LogHelper.WriteErrLog("【录音】(StartRecordingAudio)" + ErrMessage, ex);
- myProcess.Dispose();
- myProcess = null;
- return false;
- }
- myProcess.Dispose();
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
-
-
-
-
- Thread.Sleep(100);
- FileToolsCommon.DeleteDirectory(FileToolsCommon.GetDirectoryName(VideoSavePath) + "temp");
- return true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【视频合成】(SynthesisVideo)视频生成失败," + ex.Message, ex);
- ErrMessage = ex.Message;
- return false;
- }
-
-
-
-
-
- }
-
-
-
-
-
- public void MergeVideoOrAudio(object FilePathobj)
- {
-
-
-
-
-
-
-
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
-
- string FilePath = (string)FilePathobj;
- string Path = FileToolsCommon.GetDirectoryName(FilePath);
-
- string Extension = FileToolsCommon.GetIOExtension(FilePath);
-
-
-
-
-
- #region 判断文件是否存在
- if (Extension.ToUpper() == ".MP3")
- {
- if (!FileToolsCommon.IsExistFile(Path + "temp/filelist.d"))
- {
- return;
- }
- }
- else
- {
- if (!FileToolsCommon.IsExistFile(Path + "temprs/filelist.d"))
- {
- return;
- }
- }
- #endregion
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- if (Extension.ToUpper() == ".MP3")
- {
- myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temp/filelist.d -c copy " + FilePath;
- }
- else
- {
-
- myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temprs/filelist.d -c copy " + FilePath;
- }
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【音视频合成】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【音视频合成】:" + myProcess.StartInfo.Arguments + "\r\n");
- 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();
- myProcess.WaitForExit();
- myProcess.Close();
- myProcess.Dispose();
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
- if (Extension.ToUpper() == ".MP3")
- {
- FileToolsCommon.DeleteDirectory(Path + "temp/");
- }
- else
- {
- FileToolsCommon.DeleteDirectory(Path + "temprs/");
- }
- }
-
-
-
-
-
-
- private string GetFilePathName(string FilePath)
- {
-
- string Path = FileToolsCommon.GetDirectoryName(FilePath);
-
- string Extension = FileToolsCommon.GetIOExtension(FilePath);
- string tempFilePath = Path + "temp/";
-
- FileToolsCommon.CreateDirectory(tempFilePath);
-
- if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
- {
- FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
- }
- int num = 1;
- string CompleteFilePath = tempFilePath + num + Extension;
- while (FileToolsCommon.IsExistFile(CompleteFilePath))
- {
- num++;
- CompleteFilePath = tempFilePath + num + Extension;
- }
- FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
- return CompleteFilePath;
- }
-
-
-
-
-
-
- private string GetRSFilePathName(string FilePath)
- {
-
- string Path = FileToolsCommon.GetDirectoryName(FilePath);
-
- string Extension = FileToolsCommon.GetIOExtension(FilePath);
- string tempFilePath = Path + "temprs/";
-
- FileToolsCommon.CreateDirectory(tempFilePath);
-
- if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
- {
- FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
- }
- int num = 1;
- string CompleteFilePath = tempFilePath + num + Extension;
- while (FileToolsCommon.IsExistFile(CompleteFilePath))
- {
- num++;
- CompleteFilePath = tempFilePath + num + Extension;
- }
- FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
- return CompleteFilePath;
- }
-
-
-
-
-
-
-
- public bool GenerateThumbnails(string VideoPath, string ImagePath, int Width = 0, int Height = 0)
- {
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- string WHStr = "";
- if (Width > 0)
- {
- WHStr = " -s " + Width + "x" + Height;
- }
- try
- {
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
-
- myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2" + WHStr + " -f image2 \"" + ImagePath + "\"";
-
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【生成缩略图】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【生成缩略图】:" + myProcess.StartInfo.Arguments + "\r\n");
- 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();
- myProcess.WaitForExit();
- myProcess.Close();
- myProcess.Dispose();
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
- return true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【生成缩略图】(GenerateThumbnails)缩略图生成失败," + ex.Message, ex);
- return false;
- }
- }
-
-
-
-
-
-
-
-
- public bool VideoTranscode(string VideoPathName, string VideoSavePathName, int Width, int Height)
- {
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- try
- {
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- myProcess.StartInfo.Arguments = "-i " + VideoPathName + " -acodec copy -vcodec libx264 -s " + Width + "*" + Height + " " + VideoSavePathName;
-
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【视频转码】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【视频转码】:" + myProcess.StartInfo.Arguments + "\r\n");
- 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();
- myProcess.WaitForExit();
- myProcess.Close();
- myProcess.Dispose();
- #region 进程是否已经释放
- bool IsRunning = true;
- while (IsRunning)
- {
- IsRunning = false;
- Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
- foreach (Process KillProcess in ProcessArray)
- {
- IsRunning = true;
- Thread.Sleep(100);
- }
- }
- #endregion
- myProcess = null;
- Thread.Sleep(200);
- FileToolsCommon.DeleteFile(VideoPathName);
- return true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【视频转码】(VideoTranscode)视频转码失败," + ex.Message, ex);
- return false;
- }
- }
-
-
-
-
-
- private 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;
- }
- }
-
-
-
-
-
-
- private void Output(object sendProcess, DataReceivedEventArgs output)
- {
- if (!string.IsNullOrEmpty(output.Data))
- {
- FileToolsCommon.AppendText(LogPath, "【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "】:");
- FileToolsCommon.AppendText(LogPath, output.Data + "\r\n");
- }
- }
-
-
- #region 麦克风声卡检测
- #region 获取麦克风
- private bool IsMicrophone = false;
-
-
-
-
- private string MicrophoneNameInfo = "";
-
-
-
-
- public string GetMicrophoneName()
- {
- return GetAudioInDevices();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
-
- public void GetMToFFmpeg()
- {
-
- new Thread(new ThreadStart(new Action(() =>
- {
- try
- {
-
- while (myProcess != null)
- {
- Thread.Sleep(100);
- }
- myProcess = new Process();
- LogPath = CreateffmpegLog();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");
- myProcess.StartInfo.Arguments = " -list_devices true -f dshow -i dummy";
- if (OutputVideoLog)
- {
- LogHelper.WriteInfoLog("【获取设备信息】:" + myProcess.StartInfo.Arguments);
- }
-
- FileToolsCommon.AppendText(LogPath, "【获取设备信息】:" + myProcess.StartInfo.Arguments + "\r\n");
- myProcess.StartInfo.UseShellExecute = false;
- myProcess.StartInfo.RedirectStandardError = true;
- myProcess.StartInfo.CreateNoWindow = true;
- myProcess.StartInfo.RedirectStandardInput = true;
-
- myProcess.ErrorDataReceived += GetDevInfoData;
- myProcess.Start();
- myProcess.BeginErrorReadLine();
- myProcess.WaitForExit();
- myProcess.Close();
- myProcess.Dispose();
- myProcess = null;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【获取设备信息】(GetMToFFmpeg)信息获取失败:" + ex.Message, ex);
- }
-
- }))).Start();
- }
-
-
-
-
-
- private void GetDevInfoData(object sender, DataReceivedEventArgs e)
- {
- if (!string.IsNullOrEmpty(e.Data))
- {
- if (IsMicrophone)
- {
- try
- {
- MicrophoneNameInfo = e.Data;
- MicrophoneNameInfo = MicrophoneNameInfo.Substring(MicrophoneNameInfo.IndexOf("]") + 1);
- MicrophoneNameInfo = MicrophoneNameInfo.Replace("\"", "");
- MicrophoneNameInfo = MicrophoneNameInfo.Trim();
- #region 测试
-
-
-
-
-
- #endregion
- IsMicrophone = false;
- FileToolsCommon.AppendText(LogPath, e.Data);
-
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【获取设备信息】(GetDevInfoData)信息获取失败:" + ex.Message, ex);
- }
- }
- else
- {
- if (e.Data.Contains("DirectShow audio devices"))
- {
-
- IsMicrophone = true;
- }
- FileToolsCommon.AppendText(LogPath, e.Data);
- FileToolsCommon.AppendText(LogPath, "\r\n");
- }
- }
- }
-
-
-
-
-
- public static string GetAudioInDevices()
- {
- List<string> devicesList = new List<string>();
- try
- {
- FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);
- foreach (FilterInfo device in videoDevices)
- {
- devicesList.Add(device.Name);
- }
- }
- catch (ApplicationException)
- {
-
- }
-
- if (devicesList.Count > 1)
- {
- return devicesList[1];
- }
- else
- {
- return "";
- }
- }
-
-
-
-
- public static List<string> GetAudioInDevicesList()
- {
- List<string> devicesList = new List<string>();
- try
- {
- FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);
- foreach (FilterInfo device in videoDevices)
- {
- devicesList.Add(device.Name);
- }
- }
- catch (ApplicationException)
- {
-
- }
- return devicesList;
- }
-
-
-
-
- public static string GetInDeviceName()
- {
- List<WaveInCapabilities> devices = new List<WaveInCapabilities>();
- int waveInDevices = WaveIn.DeviceCount;
- for (int i = 0; i < waveInDevices; i++)
- {
- devices.Add(WaveIn.GetCapabilities(i));
- }
- List<string> devs = new List<string>();
- devs = devices.Select(item => item.ProductName).ToList();
-
- if (devs.Count > 0)
- {
- byte[] buffer = Encoding.UTF8.GetBytes(devs[0]);
- string MicrophoneName = Encoding.UTF8.GetString(buffer);
- return MicrophoneName;
- }
- else
- {
- return "";
- }
- }
-
-
-
-
- public static string GetInDeviceFull()
- {
- List<string> devices = 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))
- {
- devices.Add(device.FriendlyName);
- break;
- }
- }
- catch (Exception)
- {
- continue;
- }
- }
- }
- if (devices.Count > 0)
- {
- byte[] buffer = Encoding.UTF8.GetBytes(devices[0]);
- string MicrophoneName = Encoding.UTF8.GetString(buffer);
- return MicrophoneName;
- }
- else
- {
- return "";
- }
- }
-
-
-
-
- public List<string> GetInDeviceListFull()
- {
- List<string> devices = 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))
- {
- devices.Add(device.FriendlyName);
- break;
- }
- }
- catch (Exception)
- {
- continue;
- }
- }
- }
- return devices;
- }
-
-
-
- private 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 "";
- }
- }
- #endregion
-
- #region 检测是否可用
-
-
-
- private WaveInEvent waveIn = null;
-
-
-
- public bool StartRecordAudio(string audioFile)
- {
- try
- {
- waveIn = new WaveInEvent();
-
- WaveFileWriter writer = new WaveFileWriter(audioFile, waveIn.WaveFormat);
-
- waveIn.DataAvailable += (s, a) =>
- {
- writer.Write(a.Buffer, 0, a.BytesRecorded);
- };
-
-
- waveIn.RecordingStopped += (s, a) =>
- {
- writer.Dispose();
- writer = null;
- waveIn.Dispose();
- waveIn = null;
- try
- {
- FileToolsCommon.DeleteFile(audioFile);
- }
- catch (Exception)
- {
-
- }
- };
- waveIn.StartRecording();
- return true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【麦克风】麦克风不可用:" + ex.Message, ex);
- return false;
- }
- }
-
-
-
-
-
- public void StopRecordAudio(int type)
- {
- try
- {
- if (type == 1)
- {
- if (waveIn != null)
- {
- try
- {
- waveIn.StopRecording();
- }
- catch (Exception)
- {
- waveIn = null;
- }
- }
- }
- else if (type == 2)
- {
- if (capture != null)
- {
- try
- {
- capture.StopRecording();
- }
- catch (Exception)
- {
- capture = null;
- }
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【麦克风】麦克风不可用:" + ex.Message, ex);
- }
- }
-
-
-
-
-
- private WasapiLoopbackCapture capture = null;
-
-
-
-
- public bool StartRecordSpeakerAudio(string audioFile)
- {
- try
- {
- capture = new WasapiLoopbackCapture();
-
- WaveFileWriter writer = new WaveFileWriter(audioFile, capture.WaveFormat);
- capture.DataAvailable += (s, a) =>
- {
- writer.Write(a.Buffer, 0, a.BytesRecorded);
- };
-
- capture.RecordingStopped += (s, a) =>
- {
- writer.Dispose();
- writer = null;
- capture.Dispose();
- capture = null;
- try
- {
- FileToolsCommon.DeleteFile(audioFile);
- }
- catch (Exception)
- {
-
- }
- };
- capture.StartRecording();
- return true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【麦克风】麦克风不可用:" + ex.Message, ex);
- return false;
- }
- }
- #endregion
- #endregion
- }
- }
|