Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==== original
- public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding, bool batchMode, double? sourceFrameRate = null, bool loadSubtitle = true)
- {
- FileName = fileName;
- Paragraphs = new List<Paragraph>();
- List<string> lines;
- try
- {
- lines = ReadLinesFromFile(fileName, useThisEncoding, out encoding);
- }
- catch (Exception exception)
- {
- System.Diagnostics.Debug.WriteLine(exception.Message);
- encoding = Encoding.UTF8;
- return null;
- }
- var ext = Path.GetExtension(fileName).ToLowerInvariant();
- foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension == ext && !p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
- {
- if (subtitleFormat.IsMine(lines, fileName))
- {
- return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
- }
- }
- foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension != ext || p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
- {
- if (subtitleFormat.IsMine(lines, fileName))
- {
- return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
- }
- }
- if (useThisEncoding == null)
- {
- return LoadSubtitle(fileName, out encoding, Encoding.Unicode);
- }
- return null;
- }
- === ai refact suggestion
- public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding = null, bool batchMode = false, double? sourceFrameRate = null, bool loadSubtitle = true)
- {
- FileName = fileName;
- Paragraphs = new List<Paragraph>();
- List<string> lines = ReadLinesFromFileSafely(fileName, useThisEncoding, out encoding);
- if (lines == null)
- {
- return null;
- }
- return DetermineSubtitleFormat(fileName, batchMode, sourceFrameRate, lines, loadSubtitle);
- }
- private List<String> ReadLinesFromFileSafely(string fileName, Encoding useThisEncoding, out Encoding encoding)
- {
- try
- {
- return ReadLinesFromFile(fileName, useThisEncoding, out encoding);
- }
- catch (Exception exception)
- {
- System.Diagnostics.Debug.WriteLine(exception.Message);
- encoding = Encoding.UTF8;
- return null;
- }
- }
- private SubtitleFormat DetermineSubtitleFormat(string fileName, bool batchMode, double? sourceFrameRate, List<String> lines, bool loadSubtitle)
- {
- var ext = Path.GetExtension(fileName).ToLowerInvariant();
- var allFormats = SubtitleFormat.AllSubtitleFormats;
- foreach (var order in new[] { true, false })
- {
- foreach (var subtitleFormat in allFormats.Where(p => p.Extension == ext == order && !p.Name.StartsWith("Unknown", StringComparison.Ordinal) == order))
- {
- if (subtitleFormat.IsMine(lines, fileName))
- {
- return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
- }
- }
- }
- if (useThisEncoding == Encoding.Unicode)
- {
- return null;
- }
- return LoadSubtitle(fileName, out _, Encoding.Unicode);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement