FFmpegService.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace Vinno.IUS.Common.Media.FFmpeg
  5. {
  6. public static class FFmpegService
  7. {
  8. private static string _ffmpegPath = "ffmpeg";
  9. static FFmpegService()
  10. {
  11. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages");
  12. var toolPath = "";
  13. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  14. {
  15. toolPath = Path.Combine(path, "ffmpeg.exe");
  16. }
  17. else if (Environment.OSVersion.Platform == PlatformID.Unix)
  18. {
  19. toolPath = Path.Combine(path, "ffmpeg");
  20. }
  21. _ffmpegPath = toolPath;
  22. }
  23. public static void StartFFmpeg(out Process process, string arguments, DataReceivedEventHandler errorDataReceived)
  24. {
  25. process = new Process
  26. {
  27. StartInfo =
  28. {
  29. FileName = _ffmpegPath,
  30. Arguments = arguments,
  31. UseShellExecute = false,
  32. Verb = "runas",
  33. CreateNoWindow = true,
  34. RedirectStandardError = true,
  35. RedirectStandardInput = true
  36. },
  37. EnableRaisingEvents = true
  38. };
  39. if (errorDataReceived != null)
  40. {
  41. process.ErrorDataReceived += errorDataReceived;
  42. }
  43. process.Start();
  44. process.BeginErrorReadLine();
  45. //process.ErrorDataReceived -= errorDataReceived;
  46. }
  47. }
  48. }