diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 2a09dba37..797959504 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -2593,7 +2593,28 @@ namespace Nikse.SubtitleEdit.Forms MessageBox.Show(_language.FileIsEmptyOrShort); } else + { + string[] arr = File.ReadAllLines(fileName, Utilities.GetEncodingFromFile(fileName)); + var sb = new StringBuilder(); + foreach (string l in arr) + sb.AppendLine(l); + string xmlAsString = sb.ToString().Trim(); + if (xmlAsString.Contains("http://www.w3.org/ns/ttml") && xmlAsString.Contains(" lines, string fileName) + { + var subtitle = new Subtitle(); + + var sb = new StringBuilder(); + foreach (string line in lines) + sb.AppendLine(line); + + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + string format = "{0}: {1} {2} {3:00}:{4:00}"; + var sb = new StringBuilder(); + int count = 1; + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.AppendLine(string.Format(format, count, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), p.Duration.Seconds, MillisecondsToFramesMaxFrameRate(p.Duration.Milliseconds))); + sb.AppendLine(p.Text); + sb.AppendLine(); + count++; + } + return sb.ToString().Trim(); + } + + private string EncodeTimeCode(TimeCode time) + { + return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds)); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + bool expectStartTime = true; + var p = new Paragraph(); + subtitle.Paragraphs.Clear(); + foreach (string line in lines) + { + string s = line.Trim().Replace("*", string.Empty); + var match = regexTimeCodes.Match(s); + if (match.Success) + { + string[] parts = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 4) + { + try + { + if (!string.IsNullOrEmpty(p.Text)) + { + subtitle.Paragraphs.Add(p); + p = new Paragraph(); + } + p.StartTime = DecodeTimeCode(parts[1]); + p.EndTime = DecodeTimeCode(parts[2]); + expectStartTime = false; + } + catch (Exception exception) + { + _errorCount++; + System.Diagnostics.Debug.WriteLine(exception.Message); + } + } + } + else if (line.Trim().Length == 0) + { + if (p != null) + { + if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + _errorCount++; + else + subtitle.Paragraphs.Add(p); + p = new Paragraph(); + } + } + else if (line.Trim().Length > 0 && !expectStartTime) + { + p.Text = (p.Text + Environment.NewLine + line).Trim(); + if (p.Text.Length > 500) + { + _errorCount+=10; + return; + } + while (p.Text.Contains(Environment.NewLine + " ")) + p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); + } + } + if (!string.IsNullOrEmpty(p.Text)) + subtitle.Paragraphs.Add(p); + + subtitle.RemoveEmptyLines(); + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string part) + { + string[] parts = part.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + + string hour = parts[0]; + string minutes = parts[1]; + string seconds = parts[2]; + string frames = parts[3]; + + return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames))); + } + + } +} + diff --git a/src/Logic/VideoPlayers/LibVlcDynamic.cs b/src/Logic/VideoPlayers/LibVlcDynamic.cs index 67c39226c..c6a5a893c 100644 --- a/src/Logic/VideoPlayers/LibVlcDynamic.cs +++ b/src/Logic/VideoPlayers/LibVlcDynamic.cs @@ -590,8 +590,10 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers _libVlcDLL = LoadLibrary(dllFile); LoadLibVlcDynamic(); } - else + else if (!Directory.Exists(videoFileName)) + { return; + } OnVideoLoaded = onVideoLoaded; OnVideoEnded = onVideoEnded; diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index d4d590b99..93eb20c37 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -815,6 +815,7 @@ +