Guest User

Untitled

a guest
Dec 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Diagnostics;
  6. using System.ComponentModel;
  7. namespace Example
  8. {
  9. public class Example
  10. {
  11.  
  12. static void Main()
  13. {
  14.  
  15. AsyncMain().GetAwaiter().GetResult();
  16. }
  17.  
  18. static async Task AsyncMain()
  19. {
  20. Console.WriteLine("Press any button to quit prematurely");
  21. var maintask = RunFFMPEG();
  22. var readtask = Task.Run(() => Console.Read());
  23. await Task.WhenAny(maintask, readtask);
  24. }
  25.  
  26.  
  27.  
  28. static async Task RunFFMPEG()
  29. {
  30. await Task.Run(() =>
  31. {
  32. const int fps = 30;
  33. const string outfile = "video_seg_%05d.ts";
  34. const string dir = @"C:UsersfunnyDesktopin";
  35. const string pattern = "%d.bmp";
  36. const string path = dir + pattern;
  37. const string args = "-y -re -f image2pipe -framerate 2 -i - -c:v libx264 -r {0} -s 1920x1200 -b:v 256000 -flags +global_header -map 0 -f segment -segment_time 10 -segment_list_size 0 -segment_list list.m3u8 -segment_format mpegts";
  38. const int startNum = 0;
  39. const int endNum = 100;
  40.  
  41. var pinf = new ProcessStartInfo("ffmpeg", string.Format(args, fps, outfile));
  42. pinf.UseShellExecute = false;
  43. pinf.RedirectStandardInput = true;
  44. pinf.WorkingDirectory = dir;
  45.  
  46. Console.WriteLine("Starting ffmpeg...");
  47. var proc = Process.Start(pinf);
  48. using (var stream = new BinaryWriter(proc.StandardInput.BaseStream))
  49. {
  50. for (var i = startNum; i < endNum; i++)
  51. {
  52. var file = string.Format(path, i.ToString("D4"));
  53. System.Threading.SpinWait.SpinUntil(() => File.Exists(file) && CanReadFile(file));
  54. Console.WriteLine("Found file: " + file);
  55. stream.Write(File.ReadAllBytes(file));
  56. }
  57. }
  58. proc.WaitForExit();
  59. Console.WriteLine("Closed ffmpeg.");
  60. });
  61. }
  62.  
  63.  
  64. static bool CanReadFile(string file)
  65. {
  66. //Needs to be able to read file
  67. FileStream fs = null;
  68. try
  69. {
  70. fs = File.OpenRead(file);
  71. return true;
  72. }
  73. catch (IOException)
  74. {
  75. return false;
  76. }
  77. finally
  78. {
  79. if (fs != null)
  80. fs.Close();
  81. }
  82. }
  83. }
  84. }
Add Comment
Please, Sign In to add comment