Advertisement
ivandrofly

LoadSubtitle From Subtitle edit project

Jul 21st, 2023
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. ==== original
  2. public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding, bool batchMode, double? sourceFrameRate = null, bool loadSubtitle = true)
  3.         {
  4.             FileName = fileName;
  5.             Paragraphs = new List<Paragraph>();
  6.             List<string> lines;
  7.             try
  8.             {
  9.                 lines = ReadLinesFromFile(fileName, useThisEncoding, out encoding);
  10.             }
  11.             catch (Exception exception)
  12.             {
  13.                 System.Diagnostics.Debug.WriteLine(exception.Message);
  14.                 encoding = Encoding.UTF8;
  15.                 return null;
  16.             }
  17.  
  18.             var ext = Path.GetExtension(fileName).ToLowerInvariant();
  19.             foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension == ext && !p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
  20.             {
  21.                 if (subtitleFormat.IsMine(lines, fileName))
  22.                 {
  23.                     return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
  24.                 }
  25.             }
  26.  
  27.             foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension != ext || p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
  28.             {
  29.                 if (subtitleFormat.IsMine(lines, fileName))
  30.                 {
  31.                     return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
  32.                 }
  33.             }
  34.  
  35.             if (useThisEncoding == null)
  36.             {
  37.                 return LoadSubtitle(fileName, out encoding, Encoding.Unicode);
  38.             }
  39.  
  40.             return null;
  41.         }
  42.  
  43. === ai refact suggestion
  44.  
  45.  
  46.  
  47. public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding = null, bool batchMode = false, double? sourceFrameRate = null, bool loadSubtitle = true)
  48. {
  49.     FileName = fileName;
  50.     Paragraphs = new List<Paragraph>();
  51.     List<string> lines = ReadLinesFromFileSafely(fileName, useThisEncoding, out encoding);
  52.  
  53.     if (lines == null)
  54.     {
  55.         return null;
  56.     }
  57.  
  58.     return DetermineSubtitleFormat(fileName, batchMode, sourceFrameRate, lines, loadSubtitle);
  59. }
  60.  
  61. private List<String> ReadLinesFromFileSafely(string fileName, Encoding useThisEncoding, out Encoding encoding)
  62. {
  63.     try
  64.     {
  65.         return ReadLinesFromFile(fileName, useThisEncoding, out encoding);
  66.     }
  67.     catch (Exception exception)
  68.     {
  69.         System.Diagnostics.Debug.WriteLine(exception.Message);
  70.         encoding = Encoding.UTF8;
  71.         return null;
  72.     }
  73. }
  74.  
  75. private SubtitleFormat DetermineSubtitleFormat(string fileName, bool batchMode, double? sourceFrameRate, List<String> lines, bool loadSubtitle)
  76. {
  77.     var ext = Path.GetExtension(fileName).ToLowerInvariant();
  78.     var allFormats = SubtitleFormat.AllSubtitleFormats;
  79.  
  80.     foreach (var order in new[] { true, false })
  81.     {
  82.         foreach (var subtitleFormat in allFormats.Where(p => p.Extension == ext == order && !p.Name.StartsWith("Unknown", StringComparison.Ordinal) == order))
  83.         {
  84.             if (subtitleFormat.IsMine(lines, fileName))
  85.             {
  86.                 return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
  87.             }
  88.         }
  89.     }
  90.  
  91.     if (useThisEncoding == Encoding.Unicode)
  92.     {
  93.         return null;
  94.     }
  95.  
  96.     return LoadSubtitle(fileName, out _, Encoding.Unicode);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement