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 SubViewer20 : SubtitleFormat
|
|
|
|
|
{
|
|
|
|
|
private enum ExpectingLine
|
|
|
|
|
{
|
|
|
|
|
TimeCodes,
|
|
|
|
|
Text
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 00:31:19 +02:00
|
|
|
|
private static readonly Regex RegexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d.\d+,\d\d:\d\d:\d\d.\d+$", RegexOptions.Compiled);
|
|
|
|
|
|
2016-02-08 21:11:03 +01:00
|
|
|
|
public override string Extension
|
|
|
|
|
{
|
|
|
|
|
get { return ".sub"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "SubViewer 2.0"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool IsTimeBased
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool IsMine(List<string> lines, string fileName)
|
|
|
|
|
{
|
|
|
|
|
var sbv = new YouTubeSbv();
|
2016-07-23 00:31:19 +02:00
|
|
|
|
if (sbv.IsMine(lines, fileName) && !string.Join(string.Empty, lines.ToArray()).Contains("[br]"))
|
2016-02-08 21:11:03 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var subtitle = new Subtitle();
|
|
|
|
|
LoadSubtitle(subtitle, lines, fileName);
|
|
|
|
|
return subtitle.Paragraphs.Count > _errorCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToText(Subtitle subtitle, string title)
|
|
|
|
|
{
|
|
|
|
|
const string paragraphWriteFormat = "{0:00}:{1:00}:{2:00}.{3:00},{4:00}:{5:00}:{6:00}.{7:00}{8}{9}";
|
|
|
|
|
const string header = @"[INFORMATION]
|
|
|
|
|
[TITLE]{0}
|
|
|
|
|
[AUTHOR]
|
|
|
|
|
[SOURCE]
|
|
|
|
|
[PRG]
|
|
|
|
|
[FILEPATH]
|
|
|
|
|
[DELAY]0
|
|
|
|
|
[CD TRACK]0
|
|
|
|
|
[COMMENT]
|
|
|
|
|
[END INFORMATION]
|
|
|
|
|
[SUBTITLE]
|
|
|
|
|
[COLF]&H000000,[STYLE]bd,[SIZE]25,[FONT]Arial
|
|
|
|
|
";
|
|
|
|
|
//00:00:06.61,00:00:13.75
|
|
|
|
|
//text1[br]text2
|
|
|
|
|
var sb = new StringBuilder();
|
2016-09-13 16:51:25 +02:00
|
|
|
|
if (subtitle.Header != null && subtitle.Header.Contains("[INFORMATION]"))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine(subtitle.Header);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.AppendFormat(header, title);
|
|
|
|
|
}
|
2016-02-08 21:11:03 +01:00
|
|
|
|
foreach (Paragraph p in subtitle.Paragraphs)
|
|
|
|
|
{
|
2016-02-28 07:55:16 +01:00
|
|
|
|
string text = p.Text.Replace(Environment.NewLine, "[br]");
|
|
|
|
|
text = text.Replace("<i>", "{\\i1}");
|
|
|
|
|
text = text.Replace("</i>", "{\\i0}");
|
2016-03-01 08:05:44 +01:00
|
|
|
|
text = text.Replace("<b>", "{\\b1}");
|
2016-02-28 07:55:16 +01:00
|
|
|
|
text = text.Replace("</b>", "{\\b0}");
|
|
|
|
|
text = text.Replace("<u>", "{\\u1}");
|
|
|
|
|
text = text.Replace("</u>", "{\\u0}");
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
|
|
|
|
sb.AppendLine(string.Format(paragraphWriteFormat,
|
|
|
|
|
p.StartTime.Hours,
|
|
|
|
|
p.StartTime.Minutes,
|
|
|
|
|
p.StartTime.Seconds,
|
|
|
|
|
RoundTo2Cifres(p.StartTime.Milliseconds),
|
|
|
|
|
p.EndTime.Hours,
|
|
|
|
|
p.EndTime.Minutes,
|
|
|
|
|
p.EndTime.Seconds,
|
|
|
|
|
RoundTo2Cifres(p.EndTime.Milliseconds),
|
|
|
|
|
Environment.NewLine,
|
|
|
|
|
text));
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString().Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int RoundTo2Cifres(int milliseconds)
|
|
|
|
|
{
|
2016-07-23 00:31:19 +02:00
|
|
|
|
return (int)Math.Round(milliseconds / 10.0);
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
|
|
|
|
{
|
|
|
|
|
var paragraph = new Paragraph();
|
2016-07-23 00:31:19 +02:00
|
|
|
|
var expecting = ExpectingLine.TimeCodes;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
_errorCount = 0;
|
2016-02-14 20:23:11 +01:00
|
|
|
|
char[] splitChars = { ':', ',', '.' };
|
2016-02-08 21:11:03 +01:00
|
|
|
|
subtitle.Paragraphs.Clear();
|
2016-09-13 16:51:25 +02:00
|
|
|
|
var header = new StringBuilder();
|
2016-02-08 21:11:03 +01:00
|
|
|
|
foreach (string line in lines)
|
|
|
|
|
{
|
2016-09-13 16:51:25 +02:00
|
|
|
|
if (subtitle.Paragraphs.Count == 0 && expecting == ExpectingLine.TimeCodes && line.StartsWith("["))
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-09-13 16:51:25 +02:00
|
|
|
|
header.AppendLine(line);
|
|
|
|
|
}
|
|
|
|
|
else if (line.Length > 20 && RegexTimeCodes.IsMatch(line))
|
|
|
|
|
{
|
|
|
|
|
var parts = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
|
2016-02-28 07:55:16 +01:00
|
|
|
|
if (parts.Length == 8)
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-02-28 07:55:16 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-23 00:31:19 +02:00
|
|
|
|
paragraph.StartTime = DecodeTimeCode(parts, 0);
|
|
|
|
|
paragraph.EndTime = DecodeTimeCode(parts, 4);
|
2016-02-28 07:55:16 +01:00
|
|
|
|
expecting = ExpectingLine.Text;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2016-07-23 00:31:19 +02:00
|
|
|
|
_errorCount++;
|
2016-02-28 07:55:16 +01:00
|
|
|
|
expecting = ExpectingLine.TimeCodes;
|
|
|
|
|
}
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-23 00:31:19 +02:00
|
|
|
|
else if (expecting == ExpectingLine.Text && line.Length > 0)
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-07-23 00:31:19 +02:00
|
|
|
|
string text = line.Replace("[br]", Environment.NewLine);
|
|
|
|
|
text = text.Replace("{\\i1}", "<i>");
|
|
|
|
|
text = text.Replace("{\\i0}", "</i>");
|
|
|
|
|
text = text.Replace("{\\i}", "</i>");
|
|
|
|
|
text = text.Replace("{\\b1}", "<b>");
|
|
|
|
|
text = text.Replace("{\\b0}", "</b>");
|
|
|
|
|
text = text.Replace("{\\b}", "</b>");
|
|
|
|
|
text = text.Replace("{\\u1}", "<u>");
|
|
|
|
|
text = text.Replace("{\\u0}", "</u>");
|
|
|
|
|
text = text.Replace("{\\u}", "</u>");
|
2016-02-28 07:55:16 +01:00
|
|
|
|
|
2016-07-23 00:31:19 +02:00
|
|
|
|
paragraph.Text = text;
|
|
|
|
|
subtitle.Paragraphs.Add(paragraph);
|
|
|
|
|
paragraph = new Paragraph();
|
|
|
|
|
expecting = ExpectingLine.TimeCodes;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
subtitle.Renumber();
|
2016-09-13 16:51:25 +02:00
|
|
|
|
if (header.ToString().Contains("[INFORMATION]"))
|
|
|
|
|
{
|
|
|
|
|
subtitle.Header = header.ToString().TrimEnd();
|
|
|
|
|
}
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
2016-07-23 00:31:19 +02:00
|
|
|
|
|
|
|
|
|
public static TimeCode DecodeTimeCode(string[] encodedTimeCode, int index)
|
|
|
|
|
{
|
|
|
|
|
// Hours, Minutes, Seconds, Milliseconds / 10.
|
|
|
|
|
return new TimeCode(int.Parse(encodedTimeCode[index]),
|
|
|
|
|
int.Parse(encodedTimeCode[index + 1]),
|
|
|
|
|
int.Parse(encodedTimeCode[index + 2]),
|
|
|
|
|
int.Parse(encodedTimeCode[index + 3]) * 10);
|
|
|
|
|
}
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|