public static void Main(string[] args) { MyProcess p = new MyProcess(); p.StartInfo.FileName = "notepad.exe"; p.EnableRaisingEvents = true; p.Exited += new EventHandler(myProcess_HasExited); p.Start(); p.WaitForInputIdle(); p.Stop(); }
public void Start() { if (IsRunning) { throw new Exception("Kcptun running"); } if (_server == null) { throw new Exception("No Server"); } try { Configuration config = controller.ConfigController.GetCurrentConfiguration(); string filename = GetKCPTunPath(); Console.WriteLine($"kcptun client: {filename}"); MyProcess p = new MyProcess(_server); p.StartInfo.FileName = filename; p.StartInfo.Arguments = BuildArguments(config.snmp, _server, localaddr, remoteaddr); p.StartInfo.WorkingDirectory = Utils.GetTempPath(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.ErrorDataReceived += OnProcessErrorDataReceived; p.OutputDataReceived += OnProcessOutputDataReceived; p.Exited += OnProcessExited; p.EnableRaisingEvents = true; p.Start(); _process = p; p.BeginOutputReadLine(); p.BeginErrorReadLine(); Console.WriteLine("kcptun client started - " + p.server.FriendlyName()); if (Started != null) { Started.Invoke(this, new EventArgs()); } } catch (Exception e) { Logging.LogUsefulException(e); } }
public void InitializeProcess(object sender, EventArgs args) { var processStartInfo = new ProcessStartInfo { UseShellExecute = false, ErrorDialog = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true } var myProcess = new Process(processStartInfo); Task.Run(() => { MyProcess.Start(); MyProcess.WaitForExit(); }); _streamWriter = MyProcess.StandardInput; _streamReader = MyProcess.StandardOutput; }
private static void Main() { // can store this to file or db var processId = Guid.NewGuid().ToString(); var myProcess = new MyProcess { StartInfo = { FileName = @"C:\HelloWorld.exe" }, ProcessId = processId }; _processes = new Dictionary <string, MyProcess> { { processId, myProcess } }; myProcess.Start(); Thread.Sleep(5000); // read id from file or db or another var pr = _processes[processId]; pr.Kill(); Console.ReadKey(); }
public static bool Mp3ToFlac(String Input, String Output, FFMpegMediaMetadataFlac ConversionParameters, bool OverrideIfExist, FFmpegConvertStatusChanged OnStatusChanged = null, FFmpegConvertProgressChanged OnProgressChanged = null, bool Async = true) { bool ret = true; Thread t = new Thread(() => { if (!_Loaded) { ret = false; return; } if (ConversionParameters == null || ConversionParameters.SamplingRate == SamplingRateInfo.nul || ConversionParameters.SamplingRate == 0 || ConversionParameters.Bit == BitInfo.nul) { ret = false; return; } if (CheckValidInput(Input) && CheckValidOutput(Output)) { if (File.Exists(Output)) { if (OverrideIfExist) File.Delete(Output); else { ret = false; return; } } if (OnStatusChanged != null) { OnStatusChanged(FFmpegStatus.Running, Input, Output); } MyProcess p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:1? -c copy " + JpgNameTemp+" -y"); p.UseShellExecute = false; p.RedirectStandardOutput = false; p.RedirectStandardError = false; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:0 -c:a:0 flac -map_metadata 0 -id3v2_version 3 -ar " + ConversionParameters.SamplingRate.ToStringReplace("_", "") + " \"" + Output + "\""); if (OnProgressChanged != null) { bool AspettaLaDurata = false; bool AspettaProgress = false; long TotalMilliSec = 0; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("Input")) { AspettaLaDurata = true; AspettaProgress = false; } else if (AspettaLaDurata && line.StartsWith(" Duration:")) { line = line.RemoveLeft(" Duration: "); line = line.SplitAndGetFirst(','); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { AspettaLaDurata = false; AspettaProgress = true; TotalMilliSec = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; } else { TotalMilliSec = -1; AspettaLaDurata = false; AspettaProgress = false; } } else if (AspettaProgress && line.Contains("No such file or directory")) { OnProgressChanged(-1, Input, Output, FFmpegError.DestFolderNotFound); } else if (AspettaProgress && (line.StartsWith("frame") || line.StartsWith("size"))) { line = line.Substring(line.IndexOf("time=") + 5, 11); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { long current = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; OnProgressChanged((int)((double)current / TotalMilliSec * 100), Input, Output); } } }; } p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; SystemService.CreateFolderSecure(SystemService.GetParent(Output)); p.Async = false; p.Start(); if (SystemService.FileExist(JpgNameTemp)) { p = new MyProcess(_PathMetaflac, "--import-picture-from=\"" + JpgNameTemp + "\" \"" + Output+"\""); p.UseShellExecute = false; p.RedirectStandardOutput = false; p.RedirectStandardError = false; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); } if (OnStatusChanged != null) { OnStatusChanged(FFmpegStatus.Stop, Input, Output); } } else { ret = false; return; } ret = true; }); t.Start(); if (!Async) t.Join(); return ret; /* ffmpeg.exe -i "07. Magic Box - Scream My Name (Radio Edit).mp3" -map 0:1? -c copy OUT.jpg ffmpeg.exe -i "07. Magic Box - Scream My Name (Radio Edit).mp3" -map 0:0 -c:a: 0 flac -map_metadata 0 -id3v2_version 3 out.flac metaflac --import-picture-from="OUT.jpg" out.flac */ }
/// <summary> /// Ottiene i Metadata da un file Media in modalità sincrona /// </summary> /// <param name="Input">Path del file Media</param> /// <returns></returns> public static FFmpegMetadata GetMetadata(String Input) { if (!_Loaded) return null; if (CheckValidInput(Input)) { MyProcess p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\""); FFmpegMetadata temp = new FFmpegMetadata(); p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith(" LANGUAGE : ")) { temp.Language = line.RemoveLeft(" LANGUAGE : ").Trim(); } else if (line.StartsWith(" YEAR : ")) { temp.Year = line.RemoveLeft(" YEAR : ").Trim(); } else if (line.StartsWith(" TITLE : ")) { temp.Title = line.RemoveLeft(" TITLE : ").Trim(); } else if (line.StartsWith(" title : ")) { temp.Title = line.RemoveLeft(" title : ").Trim(); } else if (line.StartsWith(" ARTIST : ")) { temp.Artist = line.RemoveLeft(" ARTIST : ").Trim(); } else if (line.StartsWith(" artist :")) { temp.Artist = line.RemoveLeft(" artist :").Trim(); } else if (line.StartsWith(" ALBUM : ")) { temp.Album = line.RemoveLeft(" ALBUM : ").Trim(); } else if (line.StartsWith(" album :")) { temp.Album = line.RemoveLeft(" album :").Trim(); } else if (line.StartsWith(" DATE : ")) { temp.Date = line.RemoveLeft(" DATE : ").Trim(); } else if (line.StartsWith(" date : ")) { temp.Date = line.RemoveLeft(" date : ").Trim(); } else if (line.StartsWith(" GENRE : ")) { temp.Genre = line.RemoveLeft(" GENRE : ").Trim(); } else if (line.StartsWith(" genre :")) { temp.Genre = line.RemoveLeft(" genre :").Trim(); } else if (line.StartsWith(" COMMENT : ")) { temp.Comment = line.RemoveLeft(" COMMENT : ").Trim(); } else if (line.StartsWith(" track : ")) { temp.track = line.RemoveLeft(" track : ").Trim(); } else if (line.StartsWith(" ENSEMBLE : ")) { temp.Ensemble = line.RemoveLeft(" ENSEMBLE : ").Trim(); } else if (line.StartsWith(" Duration:")) { String[] sss = line.Split(','); if (sss.Length == 3) { temp.MediaMetadata.Duration = sss[0].RemoveLeft(" Duration: ").Trim(); temp.MediaMetadata.start = sss[1].RemoveLeft(" start: ").Trim(); temp.MediaMetadata.bitrate = sss[2].RemoveLeft(" bitrate: ").Trim(); } } else if (line.StartsWith(" Stream")) { String[] sss = line.Split(':'); if (sss.Length == 4 && sss[2].Contains("Audio")) { sss = sss[3].Split(','); if(sss.Length>1) { if(sss[0].Contains("flac")) { temp.MediaMetadata = new FFMpegMediaMetadataFlac(temp.MediaMetadata); try { (temp.MediaMetadata as FFMpegMediaMetadataFlac).SamplingRate= (SamplingRateInfo) Enum.Parse(typeof(SamplingRateInfo), "_" + sss[1].RemoveRight("Hz", " ").Trim()); if (sss[3].Contains("(")) { string bittemp = sss[3].Substring("(", ")").RemoveRight("bit", " ").Trim(); (temp.MediaMetadata as FFMpegMediaMetadataFlac).Bit = (BitInfo)Enum.Parse(typeof(BitInfo), "_" + bittemp); } } catch (Exception ex){ StreamWriter sw = File.AppendText("tetttmp.txt"); sw.WriteLine(Input + " " + ex.Message ); sw.Close(); } } else if (sss[0].Contains("mp3")) { temp.MediaMetadata = new FFMpegMediaMetadataMp3(temp.MediaMetadata); try { (temp.MediaMetadata as FFMpegMediaMetadataMp3).SamplingRate = (SamplingRateInfo)Enum.Parse(typeof(SamplingRateInfo), "_" + sss[1].RemoveRight("Hz", " ").Trim()); string bitrate = sss[4].RemoveRight("kb/s", " ").Trim(); int.TryParse(bitrate, out (temp.MediaMetadata as FFMpegMediaMetadataMp3).BitRateMp3); } catch (Exception ex) { //MessageBox.Show(ex.Message); StreamWriter sw = File.AppendText("tetttmp.txt"); sw.WriteLine(Input + " " + ex.Message); sw.Close(); } } else if (sss[0].Contains("pcm")) { temp.MediaMetadata = new FFMpegMediaMetadataWav(temp.MediaMetadata); try { (temp.MediaMetadata as FFMpegMediaMetadataWav).SamplingRate = (SamplingRateInfo)Enum.Parse(typeof(SamplingRateInfo), "_" + sss[1].RemoveRight("Hz", " ").Trim()); } catch (Exception ex) { StreamWriter sw = File.AppendText("tetttmp.txt"); sw.WriteLine(Input + " " + ex.Message); sw.Close(); } } } } } }; p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); return temp; } else return null; }
public static bool FlacToMp3(String Input,String Output,FFMpegMediaMetadataMp3 ConversionParameters, bool OverrideIfExist,FFmpegConvertStatusChanged OnStatusChanged=null, FFmpegConvertProgressChanged OnProgressChanged=null, bool Async = true ) { if (!_Loaded) return false; if (ConversionParameters == null || ConversionParameters.SamplingRate == SamplingRateInfo.nul || ConversionParameters.SamplingRate == 0 || ConversionParameters.BitRateMp3 == 0) return false; if (CheckValidInput(Input) && CheckValidOutput(Output)) { if (File.Exists(Output)) { if (OverrideIfExist) File.Delete(Output); else return true; } MyProcess p = new MyProcess(_PathFFmpeg, "-i \"" + Input + "\" -map 0:0 -map 0:1? -c:a:0 libmp3lame -ab "+ ConversionParameters.BitRateMp3 + "k -ar "+ConversionParameters.SamplingRate.ToStringReplace("_","")+" -map_metadata 0 -id3v2_version 3 -c:v copy \"" + Output + "\""); if (OnStatusChanged != null) { p.OnStatusChanged += (ProcessStatus s) => { if (s == ProcessStatus.Running) OnStatusChanged(FFmpegStatus.Running, Input, Output); else if (s == ProcessStatus.Stop) OnStatusChanged(FFmpegStatus.Stop, Input, Output); }; } if (OnProgressChanged != null) { bool AspettaLaDurata = false; bool AspettaProgress = false; long TotalMilliSec = 0; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("Input")) { AspettaLaDurata = true; AspettaProgress = false; } else if (AspettaLaDurata && line.StartsWith(" Duration:")) { line = line.RemoveLeft(" Duration: "); line = line.SplitAndGetFirst(','); string[] ss=line.Split(':', '.'); if (ss.Length == 4) { AspettaLaDurata = false; AspettaProgress = true; TotalMilliSec = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; } else { TotalMilliSec = -1; AspettaLaDurata = false; AspettaProgress = false; } } else if(AspettaProgress && line.Contains("No such file or directory")) { OnProgressChanged(-1, Input, Output,FFmpegError.DestFolderNotFound); } else if (AspettaProgress && ( line.StartsWith("frame") || line.StartsWith("size") )) { line = line.Substring(line.IndexOf("time=") + 5, 11); string[] ss = line.Split(':', '.'); if (ss.Length == 4) { long current = ss[3].ParseInt() * 10 + ss[2].ParseInt() * 1000 + ss[1].ParseInt() * 60000 + ss[0].ParseInt() * 3600000; OnProgressChanged((int)((double)current / TotalMilliSec * 100), Input, Output); } } }; } p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; SystemService.CreateFolderSecure(SystemService.GetParent(Output)); p.Async = Async; p.Start(); } else return false; return true; }
public static bool CheckValidMetaflac(string pathMetaflac) { if (!SystemService.FileExist(pathMetaflac)) return false; MyProcess p = new MyProcess(pathMetaflac); bool valid = false; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("metaflac - Command-line FLAC metadata editor version")) { valid = true; } }; p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); return valid; }
public static bool CheckValidFFmpeg(String Path) { if (!SystemService.FileExist(Path)) return false; MyProcess p = new MyProcess(Path); bool valid = false; p.OnNewLine += (string line) => { if (line == null) return; else if (line.StartsWith("ffmpeg version")) { valid = true; } }; p.UseShellExecute = false; p.RedirectStandardOutput = true; p.RedirectStandardError = true; p.CreateNoWindow = true; p.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Async = false; p.Start(); return valid; }