/// <summary> /// Parses a subtitle file stream. /// We try all the parsers registered in the _subFormatToParser dictionary /// </summary> /// <param name="stream">The subtitle file stream</param> /// <param name="encoding">The stream encoding</param> /// <param name="subFormat">The preferred subFormat to try first (if we have a clue with the subtitle file name for example)</param> /// <returns>The corresponding list of SubtitleItem, null if parsing failed</returns> public List <SubtitleItem> ParseStream(Stream stream, Encoding encoding, SubtitlesFormat subFormat) { var dictionary = _subFormatToParser // start the parsing by the specified format .OrderBy(dic => Math.Abs(String.Compare(dic.Key.Name, subFormat.Name, StringComparison.Ordinal))) .ToDictionary(entry => entry.Key, entry => entry.Value); return(ParseStream(stream, encoding, dictionary)); }
public SubZir ParseStream(Stream stream, Encoding encoding, SubtitlesFormat subFormat = null) { var dictionary = subFormat != null? _subFormatToParser .OrderBy(dic => Math.Abs(string.Compare(dic.Key.Name, subFormat.Name, StringComparison.Ordinal))) .ToDictionary(entry => entry.Key, entry => entry.Value) : _subFormatToParser; return(ParseStream(stream, encoding, dictionary)); }
/// <summary> /// Parses a subtitle file stream. /// We try all the parsers registered in the _subFormatToParser dictionary /// </summary> /// <param name="stream">The subtitle file stream</param> /// <param name="encoding">The stream encoding</param> /// <param name="subFormat">The preferred subFormat to try first (if we have a clue with the subtitle file name for example)</param> /// <returns>The corresponding list of SubtitleItem, null if parsing failed</returns> public List<SubtitleItem> ParseStream(Stream stream, Encoding encoding, SubtitlesFormat subFormat = null) { var dictionary = subFormat != null ? _subFormatToParser // start the parsing by the specified format .OrderBy(dic => Math.Abs(string.Compare(dic.Key.Name, subFormat.Name, StringComparison.Ordinal))) .ToDictionary(entry => entry.Key, entry => entry.Value): _subFormatToParser; return ParseStream(stream, encoding, dictionary); }
static void Main(string[] args) { var parser = new SubtitlesParser.Classes.Parsers.SubParser(); var allFiles = BrowseTestSubtitlesFiles(); foreach (var file in allFiles) { var fileName = Path.GetFileName(file); using (var fileStream = File.OpenRead(file)) { try { SubtitlesFormat format = parser.GetFormat(fileName); List <SubtitleItem> items = parser.ParseStream(fileStream, Encoding.UTF8, format); if (items.Any()) { //Console.WriteLine("Parsing of file {0}: SUCCESS ({1} items - {2}% corrupted)", // fileName, items.Count, (items.Count(it => it.StartTime <= 0 || it.EndTime <= 0) * 100)/ items.Count); foreach (var item in items) { //Console.WriteLine(item); } var duplicates = items.GroupBy(it => new { it.StartTime, it.EndTime }).Where(grp => grp.Count() > 1); //Console.WriteLine("{0} duplicate items", duplicates.Count()); //Console.WriteLine("----------------"); } else { throw new ArgumentException("Not items found!"); } } catch (Exception ex) { Console.WriteLine("Parsing of file {0}: FAILURE\n{1}", fileName, ex); } } Console.WriteLine("----------------------"); } Console.ReadLine(); }