override public void readSubtitles(string path) { base.readSubtitles(path); if (textLines == null || textLines.Length <= 0) { return; } if (subtitlesUnits != null && subtitlesUnits.Count > 0) { subtitlesUnits.Clear(); } else { subtitlesUnits = new List <SubtitlesUnit>(); } Regex numReg = new Regex(@"^[1-9]\d*$"); int lens = textLines.Length; SRTUnit timeSRT = null; for (int i = 0; i < lens; i++) { string tempStr = textLines[i]; tempStr = tempStr.Trim(); if (tempStr.Length == 0) { continue; } else { if (TimeReg.IsMatch(tempStr)) { timeSRT = new SRTUnit(tempStr); subtitlesUnits.Add(timeSRT); } else { if (timeSRT != null && !numReg.IsMatch(tempStr)) { timeSRT.Contents.Add(tempStr); } } } } }
/// <summary> /// 将当前字幕和参数中的字幕,按照时间进行合并,如果开始和结束时间相同,合并为同一个字幕元素,当前字幕在上方 /// </summary> /// <param name="subtitles">要参与合并的字幕</param> /// <param name="outPath">合并后字幕要输出的位置, 如果此值不为空,且为正确的路径,回自动写入合并后的字幕</param> /// <returns>合并后的字幕</returns> public ReadSubtitles mergeSubtitlesByTime(ReadSubtitles subtitles, uint type = SubtitlesType.Type_Current, string outPath = "") { ReadSRT rtn = new ReadSRT(); rtn.subtitlesUnits = new List <SubtitlesUnit>(); int lens1 = subtitlesUnits.Count; int lens2 = subtitles.subtitlesUnits.Count; rtn.fileInUrl = outPath == "" ? fileInUrl : outPath; SRTUnit tempUnit; int i = 0, j = 0; while (i < lens1 || j < lens2) { tempUnit = new SRTUnit(); if (i < lens1) { if (j < lens2) { if (subtitlesUnits[i].BeginTime == subtitles.subtitlesUnits[j].BeginTime && subtitlesUnits[i].EndTime == subtitles.subtitlesUnits[j].EndTime) { tempUnit.BeginTime = subtitlesUnits[i].BeginTime; tempUnit.EndTime = subtitlesUnits[i].EndTime; tempUnit.Contents.AddRange(subtitlesUnits[i].Contents); tempUnit.Contents.AddRange(subtitles.subtitlesUnits[j].Contents); i++; j++; } else if (subtitlesUnits[i].BeginTime < subtitles.subtitlesUnits[j].BeginTime) { tempUnit.BeginTime = subtitlesUnits[i].BeginTime; tempUnit.EndTime = subtitlesUnits[i].EndTime; tempUnit.Contents.AddRange(subtitlesUnits[i].Contents); i++; } else { tempUnit.BeginTime = subtitles.subtitlesUnits[j].BeginTime; tempUnit.EndTime = subtitles.subtitlesUnits[j].EndTime; tempUnit.Contents.AddRange(subtitles.subtitlesUnits[j].Contents); j++; } } else { tempUnit.BeginTime = subtitlesUnits[i].BeginTime; tempUnit.EndTime = subtitlesUnits[i].EndTime; tempUnit.Contents.AddRange(subtitlesUnits[i].Contents); i++; } } else { tempUnit.BeginTime = subtitles.subtitlesUnits[j].BeginTime; tempUnit.EndTime = subtitles.subtitlesUnits[j].EndTime; tempUnit.Contents.AddRange(subtitles.subtitlesUnits[j].Contents); j++; } rtn.subtitlesUnits.Add(tempUnit); } ReadSubtitles rs = this.convertTo(type); if (outPath != null && outPath.Length > 0) { rs.writeSubtitles(); } return(rs); }