public static string GetSubtitleSubrip(FileInfo file) { if (FFmpegExecutable == null) { throw new NotSupportedException(); } if (file == null) { throw new ArgumentNullException(nameof(file)); } try { using (var p = new Process()) { var sti = p.StartInfo; #if !DEBUG sti.CreateNoWindow = true; #endif sti.UseShellExecute = false; sti.FileName = FFmpegExecutable; sti.Arguments = $"-i \"{file.FullName}\" -map s:0 -f srt pipe:"; sti.LoadUserProfile = false; sti.RedirectStandardOutput = true; p.Start(); var lastPosition = 0L; using (var reader = new StreamReader(StreamManager.GetStream())) { using (var pump = new StreamPump( p.StandardOutput.BaseStream, reader.BaseStream, 100)) { pump.Pump(null); while (!p.WaitForExit(20000)) { if (lastPosition != reader.BaseStream.Position) { lastPosition = reader.BaseStream.Position; continue; } p.Kill(); throw new NotSupportedException("ffmpeg timed out"); } if (!pump.Wait(2000)) { throw new NotSupportedException("ffmpeg pump timed out"); } reader.BaseStream.Seek(0, SeekOrigin.Begin); var rv = string.Empty; string line; while ((line = reader.ReadLine()) != null) { rv += regAssStrip.Replace(line.Trim(), string.Empty) + "\n"; } if (!string.IsNullOrWhiteSpace(rv)) { return(rv); } } } } } catch (Exception ex) { throw new NotSupportedException(ex.Message, ex); } throw new NotSupportedException( "File does not contain a valid subtitle"); }
private static IDictionary <string, string> IdentifyInternalFromProcess( FileInfo file) { using (var p = new Process()) { var sti = p.StartInfo; #if !DEBUG sti.CreateNoWindow = true; #endif sti.UseShellExecute = false; sti.FileName = FFmpegExecutable; sti.Arguments = $"-i \"{file.FullName}\""; sti.LoadUserProfile = false; sti.RedirectStandardError = true; p.Start(); IDictionary <string, string> rv = new Dictionary <string, string>(); using (var reader = new StreamReader(StreamManager.GetStream())) { using (var pump = new StreamPump( p.StandardError.BaseStream, reader.BaseStream, 4096)) { pump.Pump(null); if (!p.WaitForExit(3000)) { throw new NotSupportedException("ffmpeg timed out"); } if (!pump.Wait(1000)) { throw new NotSupportedException("ffmpeg pump timed out"); } reader.BaseStream.Seek(0, SeekOrigin.Begin); var output = reader.ReadToEnd(); var match = regDuration.Match(output); if (match.Success) { int h, m, s; if (int.TryParse(match.Groups[1].Value, out h) && int.TryParse(match.Groups[2].Value, out m) && int.TryParse(match.Groups[3].Value, out s)) { int ms; if (match.Groups.Count < 5 || !int.TryParse(match.Groups[4].Value, out ms)) { ms = 0; } var ts = new TimeSpan(0, h, m, s, ms * 10); var tss = ts.TotalSeconds.ToString( CultureInfo.InvariantCulture); rv.Add("LENGTH", tss); } } match = regDimensions.Match(output); if (match.Success) { int w, h; if (int.TryParse(match.Groups[1].Value, out w) && int.TryParse(match.Groups[2].Value, out h)) { rv.Add("VIDEO_WIDTH", w.ToString()); rv.Add("VIDEO_HEIGHT", h.ToString()); } } } } if (rv.Count == 0) { throw new NotSupportedException("File not supported"); } return(rv); } }