public static Subtitle ReadSrt(string filepath) { string context = System.IO.File.ReadAllText(filepath); var subtitleobject = new Subtitle(); subtitleobject.Format = SubtitleFormat.SRT; //subtitleobject.Context = context; var timelinematchcollection = Regex.Matches(context,_timelineRegexPattern); for (int i = 0; i < timelinematchcollection.Count; i++) { Match match = timelinematchcollection[i]; Line line = new Line(); string timeline = match.Value; var lineTimes = Regex.Matches(timeline, _anyDigitRegexPattern); line.LineNumber = Convert.ToInt32(lineTimes[0].Value); line.StartTime = new LineTime(); line.StartTime.Hours = Convert.ToInt32(lineTimes[1].Value); line.StartTime.Minutes = Convert.ToInt32(lineTimes[2].Value); line.StartTime.Seconds = Convert.ToInt32(lineTimes[3].Value); line.StartTime.MilliSeconds = Convert.ToInt32(lineTimes[4].Value); line.EndTime = new LineTime(); line.EndTime.Hours = Convert.ToInt32(lineTimes[5].Value); line.EndTime.Minutes = Convert.ToInt32(lineTimes[6].Value); line.EndTime.Seconds = Convert.ToInt32(lineTimes[7].Value); line.EndTime.MilliSeconds = Convert.ToInt32(lineTimes[8].Value); if (i + 1 == timelinematchcollection.Count) line.Context = StringTools.SubstrinString(context, match.Index + match.Length, context.Length,true).Trim(Environment.NewLine.ToArray()).Replace(Environment.NewLine,"</br>"); else line.Context=StringTools.SubstrinString(context,match.Index+match.Length,timelinematchcollection[i+1].Index,true).Trim(Environment.NewLine.ToArray()).Replace(Environment.NewLine,"</br>"); line.ConstantContext = String.Copy(line.Context); subtitleobject.Lines.Add(line); } subtitleobject.Lines.SkipUndo = false; return subtitleobject; }
public KaroakeEffectWindow(Subtitle subtitle,System.Collections.IList lines,string title) { lines = lines as List<SubLine>; if (subtitle == null || lines == null||lines.Count<1) return; InitializeComponent(); _subtitle = subtitle; Title = title; FlowDocument flowdocument = new FlowDocument(); Paragraph para = new Paragraph(); string str = _lines.OrderByDescending(x => x.Context.Length).ToList()[0].Context.Replace("</br>", Environment.NewLine); str = HtmlTools.RemoveHtmlTags(str); var run = new Run(str); run.Foreground = Brushes.White; para.Inlines.Add(run); flowdocument.Blocks.Add(para); preview.Document = flowdocument; }
/// <summary> /// show an open file dialog,get a file and read it and set CurrentSubtitle Instant /// </summary> private void OpenSubtitleFile() { var openfiledialog = new Microsoft.Win32.OpenFileDialog { Title = "select a subtitle file", Filter = "Supported Files| *.srt|All Files|*.*", Multiselect = false }; if (openfiledialog.ShowDialog() == true) { var selectedfile = openfiledialog.FileName; if (FileTools.CheckFileReadable(selectedfile) == false) if (MessageBox.Show("the file you selected cannot read.\nretry with another file?", "cannot read file!", MessageBoxButton.YesNo, MessageBoxImage.Error) == MessageBoxResult.Yes) OpenSubtitleFile(); else return; else { var subtitleformat = SubtitleTools.DetectSubtitleFormat(selectedfile, true); if (subtitleformat == SubtitleFormat.UNKNOWN) { MessageBox.Show("the file you selected is not a correct supported subtitle. \ncannot continue.", "not in correct format", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (subtitleformat == SubtitleFormat.SRT) { _CurrentSubtitle = SrtTools.ReadSrt(selectedfile); //FindAndPlayVideo(openfiledialog.FileName); } } if (_CurrentSubtitle != null) { DataContext = _CurrentSubtitle; mainListView.SelectedIndex = 0; } } else return; }