Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.ComponentModel;
- namespace Example
- {
- public class Example
- {
- static void Main()
- {
- AsyncMain().GetAwaiter().GetResult();
- }
- static async Task AsyncMain()
- {
- Console.WriteLine("Press any button to quit prematurely");
- var maintask = RunFFMPEG();
- var readtask = Task.Run(() => Console.Read());
- await Task.WhenAny(maintask, readtask);
- }
- static async Task RunFFMPEG()
- {
- await Task.Run(() =>
- {
- const int fps = 30;
- const string outfile = "video_seg_%05d.ts";
- const string dir = @"C:UsersfunnyDesktopin";
- const string pattern = "%d.bmp";
- const string path = dir + pattern;
- 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";
- const int startNum = 0;
- const int endNum = 100;
- var pinf = new ProcessStartInfo("ffmpeg", string.Format(args, fps, outfile));
- pinf.UseShellExecute = false;
- pinf.RedirectStandardInput = true;
- pinf.WorkingDirectory = dir;
- Console.WriteLine("Starting ffmpeg...");
- var proc = Process.Start(pinf);
- using (var stream = new BinaryWriter(proc.StandardInput.BaseStream))
- {
- for (var i = startNum; i < endNum; i++)
- {
- var file = string.Format(path, i.ToString("D4"));
- System.Threading.SpinWait.SpinUntil(() => File.Exists(file) && CanReadFile(file));
- Console.WriteLine("Found file: " + file);
- stream.Write(File.ReadAllBytes(file));
- }
- }
- proc.WaitForExit();
- Console.WriteLine("Closed ffmpeg.");
- });
- }
- static bool CanReadFile(string file)
- {
- //Needs to be able to read file
- FileStream fs = null;
- try
- {
- fs = File.OpenRead(file);
- return true;
- }
- catch (IOException)
- {
- return false;
- }
- finally
- {
- if (fs != null)
- fs.Close();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment