SubtitleEdit/libse/SubtitleFormats/QuickTimeText.cs

144 lines
5.4 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class QuickTimeText : SubtitleFormat
{
private static readonly Regex RegexTimeCodes = new Regex(@"^\[\d\d:\d\d:\d\d.\d\d\]", RegexOptions.Compiled);
2017-07-22 14:59:20 +02:00
public override string Extension => ".txt";
2016-02-06 07:52:53 +01:00
2017-07-22 14:59:20 +02:00
public override string Name => "QuickTime text";
2016-02-06 07:52:53 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
if (lines != null && lines.Count > 0 && lines[0].StartsWith("{\\rtf", StringComparison.Ordinal))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return false;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
var subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
2017-07-22 14:59:20 +02:00
bool isMine = subtitle.Paragraphs.Count > _errorCount;
if (isMine && new UnknownSubtitle80().IsMine(lines, fileName))
2019-01-19 14:40:37 +01:00
{
2017-07-22 14:59:20 +02:00
return false;
2019-01-19 14:40:37 +01:00
}
2017-07-22 14:59:20 +02:00
return isMine;
2016-02-06 07:52:53 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
2016-06-07 06:31:03 +02:00
const string header = @"{QTtext} {font:Tahoma}
2016-02-06 07:52:53 +01:00
{plain} {size:20}
{timeScale:30}
{width:160} {height:32}
{timestamps:absolute} {language:0}";
var sb = new StringBuilder();
2016-06-07 06:31:03 +02:00
sb.AppendLine(header);
2016-02-06 07:52:53 +01:00
foreach (Paragraph p in subtitle.Paragraphs)
{
//[00:00:07.12]
//dêtre perdu dans un brouillard de pensées,
//[00:00:17.06] (this line is optional!)
// (blank line optional too)
//[00:00:26.26]
//tout le temps,
//[00:00:35.08]
2017-07-22 14:59:20 +02:00
sb.AppendLine($"{EncodeTimeCode(p.StartTime) + Environment.NewLine}{HtmlUtil.RemoveHtmlTags(p.Text) + Environment.NewLine}{EncodeTimeCode(p.EndTime) + Environment.NewLine}");
2016-02-06 07:52:53 +01:00
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
//[00:00:07.12]
2017-07-22 14:59:20 +02:00
return $"[{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}.{MillisecondsToFramesMaxFrameRate(time.Milliseconds):00}]";
2016-02-06 07:52:53 +01:00
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
//[00:00:07.12]
//dêtre perdu dans un brouillard de pensées,
//[00:00:17.06] (this line is optional!)
// (blank line optional too)
//[00:00:26.26]
//tout le temps,
//[00:00:35.08]
Paragraph p = null;
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (string line in lines)
{
if (RegexTimeCodes.IsMatch(line))
{
string temp = line.Substring(0, RegexTimeCodes.Match(line).Length);
string[] parts = temp.Split(new[] { '.', ':', '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
if (p == null || string.IsNullOrEmpty(p.Text))
{
try
{
string text = string.Empty;
int indexOfEndTime = line.IndexOf(']');
if (indexOfEndTime > 0 && indexOfEndTime + 1 < line.Length)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
text = line.Substring(indexOfEndTime + 1);
2019-01-19 14:40:37 +01:00
}
p = new Paragraph(DecodeTimeCodeFramesFourParts(parts), DecodeTimeCodeFramesFourParts(parts), text);
2016-02-06 07:52:53 +01:00
}
catch
{
_errorCount++;
}
}
else
{
p.EndTime = DecodeTimeCodeFramesFourParts(parts);
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(p);
string text = string.Empty;
int indexOfEndTime = line.IndexOf(']');
if (indexOfEndTime > 0 && indexOfEndTime + 1 < line.Length)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
text = line.Substring(indexOfEndTime + 1);
2019-01-19 14:40:37 +01:00
}
p = new Paragraph(DecodeTimeCodeFramesFourParts(parts), DecodeTimeCodeFramesFourParts(parts), text);
2016-02-06 07:52:53 +01:00
}
}
}
else if (string.IsNullOrWhiteSpace(line))
{
// skip these lines
}
else if (p != null)
{
if (string.IsNullOrEmpty(p.Text))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p.Text = line;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
else
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p.Text = p.Text + Environment.NewLine + line;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
else
{
_errorCount++;
}
}
subtitle.Renumber();
}
}
}