SubtitleEdit/libse/SubtitleFormats/PinnacleImpression.cs

118 lines
4.3 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 PinnacleImpression : SubtitleFormat
{
private static readonly Regex RegexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d \d\d:\d\d:\d\d:\d\d ", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Pinnacle Impression";
2016-02-08 21:11:03 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
2017-01-20 22:25:06 +01:00
var sb = new StringBuilder();
2016-02-08 21:11:03 +01:00
foreach (string line in lines)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
sb.AppendLine(line);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
if (sb.ToString().Contains("#INPOINT OUTPOINT PATH"))
{
2017-08-03 12:43:52 +02:00
return base.IsMine(lines, fileName);
2016-02-08 21:11:03 +01:00
}
return false;
}
public override string ToText(Subtitle subtitle, string title)
{
2017-01-20 22:25:06 +01:00
var sb = new StringBuilder();
2016-02-08 21:11:03 +01:00
sb.AppendLine(@"-------------------------------------------------
#INPOINT OUTPOINT PATH
-------------------------------------------------");
foreach (Paragraph p in subtitle.Paragraphs)
{
//00:03:15:22 00:03:23:10 This is line one.
//This is line two.
2017-08-03 12:43:52 +02:00
sb.AppendLine($"{EncodeTimeCode(p.StartTime)} {EncodeTimeCode(p.EndTime)} {HtmlUtil.RemoveHtmlTags(p.Text)}");
2016-02-08 21:11:03 +01:00
}
sb.AppendLine(@"-------------------------------------------------");
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
//00:03:15:22 (last is frame)
int frames = time.Milliseconds / (1000 / 30);
2017-08-03 12:43:52 +02:00
return $"{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}:{frames:00}";
2016-02-08 21:11:03 +01:00
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
//00:03:15:22 00:03:23:10 This is line one.
//This is line two.
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 start = temp.Substring(0, 11);
string end = temp.Substring(12, 11);
string[] startParts = start.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
string text = line.Remove(0, RegexTimeCodes.Match(line).Length - 1).Trim();
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
subtitle.Paragraphs.Add(p);
}
}
else if (string.IsNullOrWhiteSpace(line.Trim('-')))
{
// skip these lines
}
else if (!string.IsNullOrWhiteSpace(line) && p != null)
{
if (string.IsNullOrEmpty(p.Text))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
p.Text = 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
p.Text = p.Text + Environment.NewLine + line;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
int milliseconds = (int)Math.Round(1000.0 / 30.0 * int.Parse(frames));
2016-02-08 21:11:03 +01:00
if (milliseconds > 999)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
milliseconds = 999;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds);
2016-02-08 21:11:03 +01:00
}
}
}