|
| 1 | +using OKEGui.Model; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace OKEGui.JobProcessor |
| 8 | +{ |
| 9 | + public class ChapterChecker |
| 10 | + { |
| 11 | + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); |
| 12 | + |
| 13 | + private readonly OKEFile ChapterFile; |
| 14 | + private readonly long LengthInMiliSec; |
| 15 | + private readonly SortedDictionary<string, string> Chapters; |
| 16 | + |
| 17 | + public ChapterChecker(OKEFile chapterFile, long lengthInMiliSec) |
| 18 | + { |
| 19 | + ChapterFile = chapterFile; |
| 20 | + LengthInMiliSec = lengthInMiliSec; |
| 21 | + Chapters = ReadChapters(chapterFile); |
| 22 | + } |
| 23 | + |
| 24 | + private static SortedDictionary<string, string> ReadChapters(OKEFile file) |
| 25 | + { |
| 26 | + SortedDictionary<string, string> chapters = new SortedDictionary<string, string>(); |
| 27 | + string fileContent = File.ReadAllText(file.GetFullPath()); |
| 28 | + string[] chapterLines = fileContent.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); |
| 29 | + for (int i = 0; i < chapterLines.Length / 2; i++) |
| 30 | + { |
| 31 | + string strTime = chapterLines[i + i].Split(new char[] { '=' })[1]; |
| 32 | + string name = chapterLines[i + i + 1].Split(new char[] { '=' })[1]; |
| 33 | + chapters.Add(strTime, name); |
| 34 | + } |
| 35 | + return chapters; |
| 36 | + } |
| 37 | + |
| 38 | + private static long StrToMilisec(string str) |
| 39 | + { |
| 40 | + string[] hms = str.Split(new char[] { ':' }); |
| 41 | + if (hms.Length != 3) |
| 42 | + { |
| 43 | + throw new ArgumentException(str + "无法识别为时间!"); |
| 44 | + } |
| 45 | + long h = long.Parse(hms[0]); |
| 46 | + long m = long.Parse(hms[1]); |
| 47 | + double s = double.Parse(hms[2]); |
| 48 | + return h * 3600000 + m * 60000 + (long)(s * 1000 + 0.5); |
| 49 | + } |
| 50 | + |
| 51 | + private void WriteChapter() |
| 52 | + { |
| 53 | + int idx = 0; |
| 54 | + string allText = ""; |
| 55 | + foreach (KeyValuePair<string, string> chapter in Chapters) |
| 56 | + { |
| 57 | + idx++; |
| 58 | + string strIdx = idx.ToString("D2"); |
| 59 | + allText += "CHAPTER" + strIdx + "=" + chapter.Key + Environment.NewLine; |
| 60 | + allText += "CHAPTER" + strIdx + "NAME=" + chapter.Value + Environment.NewLine; |
| 61 | + } |
| 62 | + string filePath = ChapterFile.GetFullPath(); |
| 63 | + File.Move(filePath, Path.ChangeExtension(filePath, ".bak") + ChapterFile.GetExtension()); |
| 64 | + File.WriteAllText(filePath, allText); |
| 65 | + } |
| 66 | + |
| 67 | + public void RemoveUnnecessaryEnd() |
| 68 | + { |
| 69 | + List<string> toRemove = new List<string>(); |
| 70 | + foreach (KeyValuePair<string, string> chapter in Chapters) |
| 71 | + { |
| 72 | + long timeInMiliSec = StrToMilisec(chapter.Key); |
| 73 | + if (timeInMiliSec > LengthInMiliSec - 1000) |
| 74 | + { |
| 75 | + Logger.Info(chapter.Value + ":" + chapter.Key + "的时间在文件结尾1秒内,删除。"); |
| 76 | + toRemove.Add(chapter.Key); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (toRemove.Count > 0) |
| 81 | + { |
| 82 | + foreach (string key in toRemove) |
| 83 | + { |
| 84 | + Chapters.Remove(key); |
| 85 | + } |
| 86 | + WriteChapter(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public bool IsEmpty() |
| 91 | + { |
| 92 | + if (Chapters.ContainsKey("00:00:00.000")) |
| 93 | + { |
| 94 | + return Chapters.Count == 1; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + return Chapters.Count == 0; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments