public string FileDetails() { FFMPEG ffmpeg = new FFMPEG(FFMPEG.ProgramToRun.FFPROBE); var info = ffmpeg.GetMediaInfo(HostingEnvironment.MapPath("~/Media/vid7.mp4")); return("aa"); }
private async Task WriteToQueue() { await Task.Delay(0); var ms = new MemoryStream(); PCMQueue = new ConcurrentQueue <Stream>(); var ffout = FFMPEG.StandardOutput.BaseStream; int read; byte[] buffer = new byte[3840]; while ((read = ffout.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); ms.Position = 0; PCMQueue.Enqueue(ms); //ms.Dispose(); ms = new MemoryStream(); //Console.WriteLine(PCMQueue.Count); } FFMPEG.StandardOutput.Close(); Percentage = 100; FFMPEG.Dispose(); CacheStatus = CacheStatus.Cached; }
public void ConvertTask(string command) { FFMPEG ffmpeg = new FFMPEG(); var result = ffmpeg.RunCommand(command); Debug.WriteLine(result); }
public async Task AddFFMPEG(FFMPEG f) { if (!FFMPEGQue.Instance.Ques.ContainsKey(f.RoomId)) { await FFMPEGQue.Instance.AddQue(f.RoomId); } FFMPEGQue.Instance.AddData(f); }
private static void ConvertFileToWav(string key, FFMPEG ffmpeg) { if (File.Exists(TempAudioFile)) { File.Delete(TempAudioFile); } ffmpeg.RunCommand($"-i \"{key}\" -f wav -acodec pcm_s16le -ac 1 -sample_fmt s16 -ar 16000 \"{TempAudioFile}\""); }
/// <summary> /// Speech cutter using the Windows speech recognition to spot and cut sentences. /// </summary> /// <param name="ffmpegExePath">Path of the ffmpeg.exe.</param> /// <param name="languague">Sets the languague of the speech recognition.</param> public SpeechCutterClient(string languague, string ffmpegExePath = "ffmpeg.exe") { _ffmpegClient = new FFMPEG(ffmpegExePath); _speechRecognitionEngine = new SpeechRecognitionEngine(new CultureInfo(languague)); _speechRecognitionEngine.RecognizeCompleted += _speechRecognitionEngine_RecognizeCompleted; _speechRecognitionEngine.SpeechRecognized += _speechRecognitionEngine_SpeechRecognized; _recognizedSentences = new List <RecognizedSentence>(); _eventLock = new ManualResetEvent(false); }
public static void VideoInfo() { string output = FFMPEG.Execute(m_inputFilePath); Console.WriteLine("Duration = " + InfoProcessor.GetDuration(output)); Console.WriteLine("Audio Rate = " + InfoProcessor.GetAudioBitRate(output)); Console.WriteLine("Audio Format = " + InfoProcessor.GetAudioFormat(output)); Console.WriteLine("Video Format = " + InfoProcessor.GetVideoFormat(output)); Console.WriteLine("Video Dimensions = " + InfoProcessor.GetVideoDimensions(output)); }
public ConvertController(Configuration config, ProcessMessageHandler webSocket) { this.FfmpegExecutablePath = config.FfmpegExecutablePath; this.MP4BoxExecutablePath = config.MP4BoxExecutablePath; this.tempFolder = config.TempFolder; this.convertedFolder = config.ConvertedFolder; this.compresssedFolder = config.CompresssedFolder; this.ffmpeg = new FFMPEG(FfmpegExecutablePath); this.ffmpeg.ErrorReceived += OnErrorReceived; this.ffmpeg.ConvertionDone += OnConvertionDone; this.webSocket = webSocket; }
public void AddAudio(String path, String provider) { TrainingAudioFile file = new TrainingAudioFile(); file.RawContent = File.ReadAllBytes(path); file.Provider = provider; FFMPEGFileInfo info = FFMPEG.GetFileInfo(path); file.AudioFormat = info.Format; file.AudioSamplingRate = info.SamplingRate; file.AudioBitrateInKB = (int)info.BitRate; AudioFiles.Add(file); }
public void GetYoutubeAudioFile(InputField link) { ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; string source = Directory.GetCurrentDirectory(); try { YouTube youtube = YouTube.Default; FFMPEG ffmpeg = new FFMPEG(Path.Combine(Application.dataPath, @"StreamingAssets\youtube-dl\ffmpeg.exe")); YouTubeVideo vid = youtube.GetVideo(link.text); string path = source + @"\" + RemoveIllegalPathCharacters(vid.Title.Replace(" - YouTube", "")) + vid.FileExtension; if (File.Exists(path)) { try { File.Delete(path); } catch (Exception ex) { Debug.LogError(ex); } } File.WriteAllBytes(path, vid.GetBytes()); string wavPath = path.Replace(vid.FileExtension, ".wav"); string result = ffmpeg.RunCommand("-i \"" + path + "\" -acodec pcm_u8 -ar 22050 \"" + wavPath + "\""); if (File.Exists(path)) { try { File.Delete(path); } catch (Exception ex) { Debug.LogError(ex); } } videoTitleText.text = Path.GetFileNameWithoutExtension(wavPath); audioPath = wavPath; } catch (Exception ex) { Debug.LogError(ex); videoTitleText.text = "<color=red>" + ex.ToString() + "</color>"; link.text = ""; } }
//public async void OnErrorReceived(object sender, DataReceivedEventArgs d) //{ // Console.WriteLine(d.Data); // await webSocket.SendMessageToAllAsync(d.Data); //} //public async void OnConvertionDone(object sender, EventArgs e) //{ // Console.WriteLine("Done!"); //} internal MediaConverter(string ffmpegExecutablePath, string mp4boxExecutablePath) { // Working folders //this.tempFolder = tempFolder; //this.convertedFolder = convertedFolder; //this.compresssedFolder = compressedFolder; // Contruct FFMPEG this.ffmpeg = new FFMPEG(ffmpegExecutablePath); this.ffmpeg.ErrorReceived += ErrorReceived; this.ffmpeg.ConvertionDone += ConvertionDone; //Construct MP4BOX this.mp4box = new MP4Box(mp4boxExecutablePath); this.mp4box.ErrorReceived += ErrorReceived; this.mp4box.ConvertionDone += ConvertionDone; }
private Image getThumb(string file, out string info) { if (filetype == FileType.Image) { using (Image orig = Image.FromFile(file)) { Image thumb = Util.GetReducedImage(imgw, imgh, orig); info = string.Format("{0}. {1} * {2}", Resource1.res, orig.Width, orig.Height); return(thumb); } } else { info = "s"; return(Image.FromFile(FFMPEG.GetThumbnail(file))); } }
private static void ResizeVideo() { FFMPEGParameters parameters = new FFMPEGParameters() { InputFilePath = m_inputFilePath, OutputFilePath = m_outputFilePath, VideoCodec = "libx264", AudioCodec = "libvo_aacenc", Format = "flv", BufferSize = 50000, Size = new Size(320, 240), MaximumRate = 400, Overwrite = true, OutputOptions = "-ar 22050 -ab 128k -ac 1", }; string output = FFMPEG.Execute(parameters); }
/// <summary> /// Get exercise thumnail if avaialble otherwise create aand get path /// </summary> /// <param name="exerciseVideoPath"></param> /// <param name="thumnailName"></param> /// <returns></returns> public static string GetSaveExerciseThumbnail(string exercisePath, string thumnailName) { StringBuilder traceLog = null; string exercisethumnailNamePath = string.Empty; try { traceLog = new StringBuilder(); traceLog.AppendLine("Start: GetSaveExerciseThumbnail"); if (!string.IsNullOrEmpty(exercisePath) && !string.IsNullOrEmpty(thumnailName)) { string root = HttpContext.Current.Server.MapPath("~/videos/exerciseThumbnails"); thumnailName = thumnailName.Replace(" ", string.Empty); string thumnailNamePath = thumnailName + Message.JpgImageExtension; if (!string.IsNullOrEmpty(thumnailNamePath) && !File.Exists(root + ConstantHelper.constDoubleBackSlash + thumnailNamePath)) { FFMPEG OBJfFMPEG = new FFMPEG(); OBJfFMPEG.SaveThumbnail(exercisePath, root + ConstantHelper.constDoubleBackSlash + thumnailNamePath); exercisethumnailNamePath = CommonUtility.VirtualPath + Message.ExerciseThumbnailDirectory + thumnailNamePath; } else { exercisethumnailNamePath = CommonUtility.VirtualPath + Message.ExerciseThumbnailDirectory + thumnailNamePath; } } return(exercisethumnailNamePath); } catch { return(exercisethumnailNamePath); } finally { traceLog.AppendLine("End: GetSaveExerciseThumbnail --- " + DateTime.Now.ToLongDateString()); LogManager.LogManagerInstance.WriteTraceLog(traceLog); } }
static public MediaInfo GetMediaInfo(string path) { FFMPEG ffmpeg = new FFMPEG(FFMPEG.ProgramToRun.FFPROBE); return(ffmpeg.GetMediaInfo(path)); }
static public void ConvertVideo(string mediaName, int mediaId) { var db = new ApplicationDbContext(); var dbRecord = db.MediaFiles.Find(mediaId); var baseDir = HostingEnvironment.MapPath("~/MediaData/Videos/" + mediaId); var mediaDir = Path.Combine(baseDir, mediaName); var mediaInfo = GetMediaInfo(mediaDir); //todo error when mediinfo doesnt have video if (!mediaInfo.Video.HasVideo) { //todo: implement this in the controller throw new Exception("Video file is not supported"); } VideoQuality videoQuality = ServerTools.VideoParams.ClassifyVideo(mediaInfo); MediaInfo videoParams = ServerTools.VideoParams.GetVideoParams(videoQuality); dbRecord.VideoQuality = videoQuality; var outputVidSd = Path.Combine(baseDir, "sd.mp4"); var outputVidHd = Path.Combine(baseDir, "hd.mp4"); var outputAudio = Path.Combine(baseDir, "audio.mp4"); var outputMobile = Path.Combine(baseDir, "mobile.mp4"); var outputMobileHd = Path.Combine(baseDir, "mobileHd.mp4"); var outputThumbnail = Path.Combine(baseDir, "thumbnail.jpg"); var outputPasslogFile = Path.Combine(baseDir, "passlog"); var segmentsDir = Path.Combine(baseDir, "segments"); if (!Directory.Exists(segmentsDir)) { Directory.CreateDirectory(segmentsDir); } var hcontext = HttpContext.Current; var task = Task.Factory.StartNew(() => { HttpContext.Current = hcontext; FFMPEG ffmpeg = new FFMPEG(); //What part of total progress is the current conversion Dictionary <string, double> ConversionPercentages = new Dictionary <string, double>(); if (videoQuality == VideoQuality.p360) { ConversionPercentages["sd"] = 0.9; ConversionPercentages["audio"] = 0.1; } else { ConversionPercentages["sd"] = 0.2; ConversionPercentages["hd"] = 0.7; ConversionPercentages["audio"] = 0.1; } // Convert to SD string command = String.Format("-i \"{0}\" -an -b:v {1}k -s {2} -vcodec libx264 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -pass 1 -passlogfile \"{3}\" \"{4}\"", mediaDir, ServerTools.VideoParams.p360.Video.Bitrate, ServerTools.VideoParams.p360.Video.Resolution, outputPasslogFile, outputVidSd); var result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "sd", mediaInfo.Video.Duration); //Convert to HD if (videoQuality != VideoQuality.p360) { command = String.Format("-i \"{0}\" -an -b:v {1}k -s {2} -vcodec libx264 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -pass 1 -passlogfile \"{3}\" \"{4}\"", mediaDir, videoParams.Video.Bitrate, videoParams.Video.Resolution, outputPasslogFile, outputVidHd); result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "hd", mediaInfo.Video.Duration); } //Convert Audio command = String.Format("-i \"{0}\" -vn -strict experimental -c:a aac -b:a 128k \"{1}\"", mediaDir, outputAudio); result = ffmpeg.RunCommand(command, mediaId, ConversionPercentages, "audio", mediaInfo.Video.Duration); //Extract thumbnail from the middle of the video command = String.Format(" -ss {0} -i \"{1}\" -vframes 1 -an -s 360x240 \"{2}\" ", (mediaInfo.Video.Duration / 2.0).ToString(CultureInfo.InvariantCulture), mediaDir, outputThumbnail); result = ffmpeg.RunCommand(command); if (mediaInfo.Audio.HasAudio) { //Convert to mobile (add sound to sd video) command = String.Format("-i \"{0}\" -i \"{1}\" -c:v copy -c:a copy \"{2}\"", outputVidSd, outputAudio, outputMobile); result = ffmpeg.RunCommand(command); //Convert to mobile Hd if (videoQuality != VideoQuality.p360) { command = String.Format("-i \"{0}\" -i \"{1}\" -c:v copy -c:a copy \"{2}\"", outputVidHd, outputAudio, outputMobileHd); result = ffmpeg.RunCommand(command); } } else { File.Copy(outputVidSd, outputMobile); if (videoQuality != VideoQuality.p360) { File.Copy(outputVidHd, outputMobileHd); } } //Segment videos and audio Mp4Box mp4Box = new Mp4Box(); if (videoQuality == VideoQuality.p360) { command = String.Format("-dash 2000 -frag 2000 -bs-switching no -segment-name \"%s_\" -url-template -out \"{0}\" \"{1}\" \"{2}\" ", Path.Combine(segmentsDir, "video.mpd"), outputVidSd, outputAudio); } else { command = String.Format("-dash 2000 -frag 2000 -bs-switching no -segment-name \"%s_\" -url-template -out \"{0}\" \"{1}\" \"{2}\" \"{3}\" ", Path.Combine(segmentsDir, "video.mpd"), outputVidSd, outputVidHd, outputAudio); } result = mp4Box.RunCommand(command); File.Delete(mediaDir); File.Delete(outputVidSd); if (File.Exists(outputVidHd)) { File.Delete(outputVidHd); } File.Delete(outputAudio); dbRecord.IsBeingConverted = false; db.Entry(dbRecord).State = EntityState.Modified; db.SaveChanges(); HttpContext.Current.Cache[mediaId.ToString()] = 1.0; }, TaskCreationOptions.LongRunning); }
public FFMPEGStream(string args) { Process = FFMPEG.RunFFMPEGCommand(args, false); Stream = Process.StandardInput.BaseStream; }
/// <summary> /// Initializes a new instance of the <see cref="FrapperWrapper"/> class. /// </summary> /// <param name="frapper">The frapper.</param> public FrapperWrapper(FFMPEG frapper) { Frapper = frapper; }
public static void RecordTrack(MainWindow game, bool is1080P, bool smooth, bool music) { var flag = game.Track.GetFlag(); if (flag == null) { return; } var resolution = new Size(is1080P ? 1920 : 1280, is1080P ? 1080 : 720); var oldsize = game.RenderSize; var invalid = false; using (var trk = game.Track.CreateTrackReader()) { Recording = true; game.Track.Reset(); Recording1080p = is1080P; //Set colors back to default for triggers linerider.Utils.Constants.TriggerBGColor = new Color4((byte)game.Track.StartingBGColorR, (byte)game.Track.StartingBGColorG, (byte)game.Track.StartingBGColorB, (byte)255); linerider.Utils.Constants.StaticTriggerBGColor = new Color4((byte)game.Track.StartingBGColorR, (byte)game.Track.StartingBGColorG, (byte)game.Track.StartingBGColorB, (byte)255); linerider.Utils.Constants.StaticTriggerLineColorChange = Color.FromArgb(255, game.Track.StartingLineColorR, game.Track.StartingLineColorG, game.Track.StartingLineColorB); linerider.Utils.Constants.TriggerLineColorChange = Color.FromArgb(255, game.Track.StartingLineColorR, game.Track.StartingLineColorG, game.Track.StartingLineColorB); var state = game.Track.GetStart(); var frame = flag.FrameID; game.Canvas.SetCanvasSize(game.RenderSize.Width, game.RenderSize.Height); game.Canvas.Layout(); if (frame > 400) //many frames, will likely lag the game. Update the window as a fallback. { game.Title = Program.WindowTitle + " [Validating flag]"; game.ProcessEvents(); } for (var i = 0; i < frame; i++) { state = trk.TickBasic(state); } for (var i = 0; i < state.Body.Length; i++) { if (state.Body[i].Location != flag.State.Body[i].Location || state.Body[i].Previous != flag.State.Body[i].Previous) { invalid = true; break; } } var frontbuffer = SafeFrameBuffer.GenFramebuffer(); SafeFrameBuffer.BindFramebuffer(FramebufferTarget.Framebuffer, frontbuffer); var rbo2 = SafeFrameBuffer.GenRenderbuffer(); SafeFrameBuffer.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rbo2); SafeFrameBuffer.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Rgb8, resolution.Width, resolution.Height); SafeFrameBuffer.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, rbo2); SafeFrameBuffer.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); if (!invalid) { _screenshotbuffer = new byte[game.RenderSize.Width * game.RenderSize.Height * 3];// 3 bytes per pixel string errormessage = "An unknown error occured during recording."; game.Title = Program.WindowTitle + " [Recording | Hold ESC to cancel]"; game.ProcessEvents(); var filename = Program.UserDirectory + game.Track.Name + ".mp4"; var flagbackup = flag; var hardexit = false; var recmodesave = Settings.Local.RecordingMode; Settings.Local.RecordingMode = true; game.Track.StartIgnoreFlag(); game.Render(); var dir = Program.UserDirectory + game.Track.Name + "_rec"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var firstframe = GrabScreenshot(game, frontbuffer); SaveScreenshot(game.RenderSize.Width, game.RenderSize.Height, firstframe, dir + Path.DirectorySeparatorChar + "tmp" + 0 + ".png"); int framecount = smooth ? ((frame + 1) * 60) / 40 : frame + 1; //Add a extra frame double frametime = 0; Stopwatch sw = Stopwatch.StartNew(); for (var i = 0; i < framecount; i++) { if (hardexit) { break; } if (smooth) { var oldspot = frametime; frametime += 40f / 60f; if (i == 0) { //bugfix: //frame blending uses the previous frame. //so the first frame would be recorded twice, //instead of blended game.Track.Update(1); } else if ((int)frametime != (int)oldspot) { game.Track.Update(1); } var blend = frametime - Math.Truncate(frametime); game.Render((float)blend); } else { game.Track.Update(1); game.Render(); } try { var screenshot = GrabScreenshot(game, frontbuffer); SaveScreenshot(game.RenderSize.Width, game.RenderSize.Height, screenshot, dir + Path.DirectorySeparatorChar + "tmp" + (i + 1) + ".png"); } catch { hardexit = true; errormessage = "An error occured when saving the frame."; } if (Keyboard.GetState()[Key.Escape]) { hardexit = true; errormessage = "The user manually cancelled recording."; } if (sw.ElapsedMilliseconds > 500) { game.Title = string.Format("{0} [Recording {1:P} | Hold ESC to cancel]", Program.WindowTitle, i / (double)framecount); game.ProcessEvents(); } } game.ProcessEvents(); if (!hardexit) { var parameters = new FFMPEGParameters(); parameters.AddOption("framerate", smooth ? "60" : "40"); parameters.AddOption("i", "\"" + dir + Path.DirectorySeparatorChar + "tmp%d.png" + "\""); if (music && !string.IsNullOrEmpty(game.Track.Song.Location) && game.Track.Song.Enabled) { var fn = Program.UserDirectory + "Songs" + Path.DirectorySeparatorChar + game.Track.Song.Location; parameters.AddOption("ss", game.Track.Song.Offset.ToString(Program.Culture)); parameters.AddOption("i", "\"" + fn + "\""); parameters.AddOption("c:a", "aac"); } double duration = framecount / (smooth ? 60.0 : 40.0); parameters.AddOption("t", duration.ToString(Program.Culture)); parameters.AddOption("vf", "vflip");//we save images upside down expecting ffmpeg to flip more efficiently. // ffmpeg x264 encoding doc: // https://trac.ffmpeg.org/wiki/Encode/H.264 parameters.AddOption("c:v", "libx264"); // we don't care _too_ much about filesize parameters.AddOption("preset", "fast"); parameters.AddOption("crf", "17"); // increase player compatibility: parameters.AddOption("pix_fmt", "yuv420p"); // this optimizes the encoding for animation // how well lr fits into that category i'm not sure. parameters.AddOption("tune", "animation"); parameters.OutputFilePath = filename; var failed = false; if (File.Exists(filename)) { try { File.Delete(filename); } catch { Program.NonFatalError("A file with the name " + game.Track.Name + ".mp4 already exists"); failed = true; errormessage = "Cannot replace a file of the existing name " + game.Track.Name + ".mp4."; } } if (!failed) { game.Title = Program.WindowTitle + " [Encoding Video | 0%]"; game.ProcessEvents(); try { FFMPEG.Execute(parameters, (string s) => { int idx = s.IndexOf("frame=", StringComparison.InvariantCulture); if (idx != -1) { idx += "frame=".Length; for (; idx < s.Length; idx++) { if (char.IsNumber(s[idx])) { break; } } var space = s.IndexOf(" ", idx, StringComparison.InvariantCulture); if (space != -1) { var sub = s.Substring(idx, space - idx); var parsedint = -1; if (int.TryParse(sub, out parsedint)) { game.Title = Program.WindowTitle + string.Format(" [Encoding Video | {0:P} | Hold ESC to cancel]", parsedint / (double)framecount); game.ProcessEvents(); if (Keyboard.GetState()[Key.Escape]) { hardexit = true; errormessage = "The user manually cancelled recording."; return(false); } } } } return(true); }); } catch (Exception e) { linerider.Utils.ErrorLog.WriteLine( "ffmpeg error" + Environment.NewLine + e); hardexit = true; errormessage = "An ffmpeg error occured.\n" + e.Message; } } } try { Directory.Delete(dir, true); } catch { Program.NonFatalError("Unable to delete " + dir); } if (hardexit) { try { File.Delete(filename); } catch { Program.NonFatalError("Unable to delete " + filename); } } Settings.Local.RecordingMode = recmodesave; game.Title = Program.WindowTitle; game.Track.Stop(); game.ProcessEvents(); var openwindows = game.Canvas.GetOpenWindows(); foreach (var window in openwindows) { var w = window as WindowControl; w?.Close(); } if (File.Exists(filename)) { AudioService.Beep(); } else { game.Canvas.ShowError(errormessage); } } SafeFrameBuffer.BindFramebuffer(FramebufferTarget.Framebuffer, 0); SafeFrameBuffer.DeleteFramebuffer(frontbuffer); SafeFrameBuffer.DeleteRenderbuffers(1, new[] { rbo2 }); game.RenderSize = oldsize; Recording = false; game.Canvas.SetSize(game.RenderSize.Width, game.RenderSize.Height); _screenshotbuffer = null; } }
/// <summary> /// The FFMPEGWrapper Class Constructor /// </summary> public FFmpegWrapper() { ffmpeg = new FFMPEG(Instructor___Omlate.Config.FFmpegPath); }
public override void Dispose(GameBoy gb) { VideoStream.Close(); AudioStream.Close(); FFMPEG.RunFFMPEGCommand("-y -i movies/video.mp4 -i movies/audio.mp3 -c:v copy -c:a copy -shortest movies/" + Movie + ".mp4"); }
static void Main(string[] args) { FFMPEG ffmpeg = new FFMPEG("ffmpeg.exe"); pythonProcess = new ProcessStartInfo { FileName = "python.exe", CreateNoWindow = true, // No window UseShellExecute = false, RedirectStandardOutput = true }; var dirs = Directory.GetFileSystemEntries("test-clean/LibriSpeech/test-clean", "*.txt", SearchOption.AllDirectories); IDictionary <string, string> dataset = new Dictionary <string, string>(); foreach (var transcriptionFile in dirs) { FileInfo fileInf = new FileInfo(transcriptionFile); foreach (var sentenceLine in File.ReadAllLines(transcriptionFile)) { var sentenceSplit = sentenceLine.Split(' '); string audioName = fileInf.FullName.Replace(fileInf.Name, $"{sentenceSplit[0]}.flac"); string sentence = string.Join(" ", sentenceSplit.ToList().Skip(1).ToArray()).ToLower(); dataset.Add(audioName, sentence); } } const uint N_CEP = 26; const uint N_CONTEXT = 9; const uint BEAM_WIDTH = 200; const float LM_ALPHA = 0.75f; const float LM_BETA = 1.85f; const string modelVersion = "0.4.1"; List <Sentence> samples = new List <Sentence>(); using (var sttClient = new DeepSpeechClient.DeepSpeech()) { var result = 1; Console.WriteLine("Loading model..."); try { result = sttClient.CreateModel($"{modelVersion}/output_graph.pbmm", N_CEP, N_CONTEXT, $"{modelVersion}/alphabet.txt", BEAM_WIDTH); } catch (IOException ex) { Console.WriteLine("Error loading lm."); Console.WriteLine(ex.Message); } if (result == 0) { Console.WriteLine("Loadin LM..."); try { result = sttClient.EnableDecoderWithLM( $"{modelVersion}/alphabet.txt", $"{modelVersion}/lm.binary", $"{modelVersion}/trie", LM_ALPHA, LM_BETA); } catch (IOException ex) { Console.WriteLine("Error loading lm."); Console.WriteLine(ex.Message); } foreach (var sentencePair in dataset) { ConvertFileToWav(sentencePair.Key, ffmpeg); var waveBuffer = new WaveBuffer(File.ReadAllBytes(TempAudioFile)); Console.WriteLine("Running inference...."); string speechResult = sttClient.SpeechToText(waveBuffer.ShortBuffer, Convert.ToUInt32(waveBuffer.MaxSize / 2), 16000); Sentence sentenceResult = RunPythonWER(sentencePair.Value, speechResult); Console.WriteLine("================================================================================"); Console.WriteLine($"Recognized text: {speechResult}"); Console.WriteLine($"Correct text: {sentencePair.Value}"); Console.WriteLine($"WER {Math.Round(sentenceResult.Wer,2)*100} %"); Console.WriteLine("================================================================================"); Console.WriteLine(); samples.Add(sentenceResult); waveBuffer.Clear(); } } else { Console.WriteLine("Error loding the model."); } } double totalLevenshtein = samples.Select(x => x.Levenshtein).Sum(); int totalLabelLength = samples.Select(x => x.Length).Sum(); double finalWer = totalLevenshtein / totalLabelLength; File.WriteAllText("result.txt", finalWer.ToString(), Encoding.UTF8); Console.WriteLine($"Final WER: {finalWer} %"); Console.ReadKey(); }
public static void RecordTrack(GLWindow game, bool is1080P) { var flag = game.Track.GetFlag(); if (flag == null) { return; } var resolution = new Size(is1080P ? 1920 : 1280, is1080P ? 1080 : 720); var oldsize = game.RenderSize; var invalid = false; var state = new Rider(); game.Track.Reset(state); var frame = flag.Frame; Recording = true; Recording1080p = is1080P; game.Canvas.SetSize(game.RenderSize.Width, game.RenderSize.Height); game.Canvas.FindChildByName("buttons").Position(Pos.CenterH); if (frame > 400) //many frames, will likely lag the game. Update the window as a fallback. { if (frame > (20 * (60 * 40))) //too many frames, could lag the game very bad. { return; } game.Title = Program.WindowTitle + " [Validating flag]"; game.ProcessEvents(); } for (var i = 0; i < frame; i++) { game.Track.Tick(state); } for (var i = 0; i < state.ModelAnchors.Length; i++) { if (state.ModelAnchors[i].Position != flag.State.ModelAnchors[i].Position || state.ModelAnchors[i].Prev != flag.State.ModelAnchors[i].Prev) { invalid = true; break; } } var frontbuffer = SafeFrameBuffer.GenFramebuffer(); SafeFrameBuffer.BindFramebuffer(FramebufferTarget.Framebuffer, frontbuffer); var rbo2 = SafeFrameBuffer.GenRenderbuffer(); SafeFrameBuffer.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rbo2); SafeFrameBuffer.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Rgb8, resolution.Width, resolution.Height); SafeFrameBuffer.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, rbo2); SafeFrameBuffer.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); if (!invalid) { string errormessage = "An unknown error occured during recording."; game.Title = Program.WindowTitle + " [Recording | Hold ESC to cancel]"; game.ProcessEvents(); var filename = Program.CurrentDirectory + game.Track.Name + ".mp4"; var flagbackup = flag; var hardexit = false; game.Track.Flag(); var recmodesave = game.SettingRecordingMode; game.SettingRecordingMode = true; game.Track.Start(true, true, false, false); game.Render(); var dir = Program.CurrentDirectory + game.Track.Name + "_rec"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var firstframe = GrabScreenshot(game, frontbuffer); SaveScreenshot(game.RenderSize.Width, game.RenderSize.Height, firstframe, dir + Path.DirectorySeparatorChar + "tmp" + 0 + ".png"); int[] savethreads = { 0 }; for (var i = 0; i < frame; i++) { if (hardexit) { break; } game.Track.Update(1); game.Render(); var screenshot = GrabScreenshot(game, frontbuffer); var objtopass = new Tuple <byte[], int>(screenshot, i + 1); savethreads[0] += 1; var save = new Task(t => { var passed = (Tuple <byte[], int>)t; try { SaveScreenshot(game.RenderSize.Width, game.RenderSize.Height, passed.Item1, dir + Path.DirectorySeparatorChar + "tmp" + passed.Item2 + ".png"); } catch { hardexit = true; errormessage = "An error occured when saving the frame."; } finally { Interlocked.Decrement(ref savethreads[0]); } }, objtopass); save.Start(); if (Keyboard.GetState()[Key.Escape]) { hardexit = true; errormessage = "The user manually cancelled recording."; } if (i % 40 == 0) { game.Title = string.Format("{0} [Recording {1:P}% | Hold ESC to cancel]", Program.WindowTitle, i / (double)frame); game.ProcessEvents(); } } if (!hardexit) { var parameters = new FFMPEGParameters(); parameters.AddOption("framerate", "40"); parameters.AddOption("i", "\"" + dir + Path.DirectorySeparatorChar + "tmp%d.png" + "\""); parameters.AddOption("vf", "vflip");//we save images upside down expecting ffmpeg to flip more efficiently. parameters.AddOption("c:v", "libx264"); parameters.AddOption("preset", "veryfast"); parameters.AddOption("qp", "0"); // parameters.AddOption("scale",is1080p?"1920:1080":"1280:720"); parameters.OutputFilePath = filename; var failed = false; while (savethreads[0] != 0) { Thread.Sleep(1); } if (File.Exists(filename)) { try { File.Delete(filename); } catch { Program.NonFatalError("A file with the name " + game.Track.Name + ".mp4 already exists"); failed = true; errormessage = "Cannot replace a file of the existing name " + game.Track.Name + ".mp4."; } } if (!failed) { game.Title = Program.WindowTitle + " [Encoding Video | 0%]"; game.ProcessEvents(); try { FFMPEG.Execute(parameters, (string s) => { int idx = s.IndexOf("frame=", StringComparison.InvariantCulture); if (idx != -1) { idx += "frame=".Length; for (; idx < s.Length; idx++) { if (char.IsNumber(s[idx])) { break; } } var space = s.IndexOf(" ", idx, StringComparison.InvariantCulture); if (space != -1) { var sub = s.Substring(idx, space - idx); var parsedint = -1; if (int.TryParse(sub, out parsedint)) { game.Title = Program.WindowTitle + string.Format(" [Encoding Video | {0:P}% | Hold ESC to cancel]", parsedint / (double)frame); game.ProcessEvents(); if (Keyboard.GetState()[Key.Escape]) { hardexit = true; errormessage = "The user manually cancelled recording."; return(false); } } } } return(true); }); } catch (Exception e) { Program.NonFatalError("ffmpeg error.\r\n" + e); hardexit = true; errormessage = "An ffmpeg error occured."; } } } try { Directory.Delete(dir, true); } catch { Program.NonFatalError("Unable to delete " + dir); } if (hardexit) { try { File.Delete(filename); } catch { Program.NonFatalError("Unable to delete " + filename); } } game.SettingRecordingMode = recmodesave; game.Title = Program.WindowTitle; game.Track.RestoreFlag(flagbackup); game.Track.Stop(); game.ProcessEvents(); var openwindows = game.Canvas.GetOpenWindows(); foreach (var window in openwindows) { var w = window as WindowControl; w?.Close(); } if (File.Exists(filename)) { try { AudioPlayback.Init(); MemoryStream ms = new MemoryStream(GameResources.beep); SoundStream str = new SoundStream(ms); str.Play(0, 1); int count = 0; while (str.Playing) { Thread.Sleep(1); count += 1; if (count >= 3000)//in case something weird happens { break; } } str.Dispose(); ms.Dispose(); } catch { //ignored } } else { PopupWindow.Error(game.Canvas, game, errormessage, "Error!"); } } SafeFrameBuffer.BindFramebuffer(FramebufferTarget.Framebuffer, 0); SafeFrameBuffer.DeleteFramebuffer(frontbuffer); SafeFrameBuffer.DeleteRenderbuffers(1, new[] { rbo2 }); game.RenderSize = oldsize; Recording = false; game.Canvas.SetSize(game.RenderSize.Width, game.RenderSize.Height); game.Canvas.FindChildByName("buttons").Position(Pos.CenterH); }