SubtitleEdit/libse/SubtitleFormats/MPlayer2.cs

173 lines
6.4 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class MPlayer2 : SubtitleFormat
{
private static readonly Regex RegexMPlayer2Line = new Regex(@"^\[-?\d+]\[-?\d+].*$", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".mpl";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "MPlayer2";
2016-02-08 21:11:03 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
int errors = 0;
var trimmedLines = new List<string>();
foreach (string line in lines)
{
int indexOfStartBracket = line.IndexOf('[');
if (!string.IsNullOrWhiteSpace(line) && line.Length < 250 && indexOfStartBracket >= 0 && indexOfStartBracket < 10)
{
string s = RemoveIllegalSpacesAndFixEmptyCodes(line);
if (RegexMPlayer2Line.IsMatch(s))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
trimmedLines.Add(line);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
else
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
errors++;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
else
{
errors++;
}
}
return trimmedLines.Count > errors;
}
private static string RemoveIllegalSpacesAndFixEmptyCodes(string line)
{
var s = line;
int index = s.IndexOf(']');
if (index >= 0 && index < s.Length)
2016-02-08 21:11:03 +01:00
{
index = s.IndexOf(']', index + 1);
if (index >= 0 && index + 1 < s.Length)
2016-02-08 21:11:03 +01:00
{
var indexOfBrackets = s.IndexOf("[]", StringComparison.Ordinal);
2016-02-08 21:11:03 +01:00
if (indexOfBrackets >= 0 && indexOfBrackets < index)
{
s = s.Insert(indexOfBrackets + 1, "0"); // set empty time codes to zero
2016-02-08 21:11:03 +01:00
index++;
}
while (s.Contains(' ') && s.IndexOf(' ') < index)
2016-02-08 21:11:03 +01:00
{
s = s.Remove(s.IndexOf(' '), 1);
2016-02-08 21:11:03 +01:00
index--;
}
}
}
return s;
2016-02-08 21:11:03 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.Append('[');
sb.Append((int)(p.StartTime.TotalMilliseconds / 100));
sb.Append("][");
sb.Append(((int)(p.EndTime.TotalMilliseconds / 100)));
sb.Append(']');
var parts = p.Text.SplitToLines();
int count = 0;
bool italicOn = false;
foreach (string line in parts)
{
if (count > 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
sb.Append('|');
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
if (line.StartsWith("<i>", StringComparison.Ordinal) || italicOn)
2016-02-08 21:11:03 +01:00
{
italicOn = true;
sb.Append('/');
}
if (line.Contains("</i>"))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
italicOn = false;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
sb.Append(HtmlUtil.RemoveHtmlTags(line));
count++;
}
sb.AppendLine();
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
foreach (string line in lines)
{
string s = RemoveIllegalSpacesAndFixEmptyCodes(line);
if (RegexMPlayer2Line.IsMatch(s))
{
try
{
int textIndex = s.IndexOf(']') + 1;
textIndex = s.IndexOf(']', textIndex) + 1;
if (textIndex < s.Length)
{
string text = s.Substring(textIndex);
if (text.StartsWith('/') && (Utilities.CountTagInText(text, '|') == 0 || text.Contains("|/")))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
text = "<i>" + text.TrimStart('/').Replace("|/", Environment.NewLine) + "</i>";
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
else if (text.StartsWith('/') && text.Contains('|') && !text.Contains("|/"))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
text = "<i>" + text.TrimStart('/').Replace("|", "</i>" + Environment.NewLine);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
else if (text.Contains("|/"))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
text = text.Replace("|/", Environment.NewLine + "<i>") + "</i>";
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
text = text.Replace("|", Environment.NewLine);
string temp = s.Substring(0, textIndex - 1);
string[] frames = temp.Replace("][", ":").Replace("[", string.Empty).Replace("]", string.Empty).Split(':');
double startSeconds = double.Parse(frames[0]) / 10;
double endSeconds = double.Parse(frames[1]) / 10;
2019-01-19 12:29:38 +01:00
if (Math.Abs(startSeconds) < 0.01 && subtitle.Paragraphs.Count > 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
startSeconds = (subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].EndTime.TotalMilliseconds / 1000) + 0.1;
2019-01-19 14:40:37 +01:00
}
2019-01-19 12:29:38 +01:00
if (Math.Abs(endSeconds) < 0.01)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
endSeconds = startSeconds;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(new Paragraph(text, startSeconds * 1000, endSeconds * 1000));
}
}
catch
{
_errorCount++;
}
}
else
{
_errorCount++;
}
}
subtitle.Renumber();
}
}
}