SubtitleEdit/libse/SubtitleFormats/Titra.cs

111 lines
3.8 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 Titra : SubtitleFormat
{
//* 1 : 01:01:31:19 01:01:33:04 22c
private static readonly Regex RegexTimeCodes = new Regex(@"^\* \d+ :\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t\d+c", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Titra";
2016-02-06 07:52:53 +01:00
private static int GetMaxCharsForDuration(double durationSeconds)
{
return (int)Math.Round(15.7 * durationSeconds);
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine(@"TVS - TITRA FILM
Titre VO : L'heure d'é
Titre VST :
Création : 23/10/2009 - 16:31
Révision : 26/10/2009 - 17:48
Langue VO : Français
Langue VST : Espagnol
Bobine : e01
BEWARE : No more than 40 characters ON A LINE
ATTENTION : Pas plus de 40 caractères PAR LIGNE
");
const string writeFormat = "* {0} :\t{1}\t{2}\t{3}{4}{5}";
int index = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
index++;
var text = HtmlUtil.RemoveHtmlTags(p.Text, true);
sb.AppendLine(string.Format(writeFormat, index, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), GetMaxCharsForDuration(p.Duration.TotalSeconds) + "c", Environment.NewLine, text));
sb.AppendLine();
if (!text.Contains(Environment.NewLine))
sb.AppendLine();
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
//00:03:15:22 (last is frame)
return time.ToHHMMSSFF();
}
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;
_errorCount = 0;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
if (RegexTimeCodes.IsMatch(line))
{
try
{
var arr = line.Split('\t');
string start = arr[1];
string end = arr[2];
string[] startParts = start.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
p = new Paragraph(DecodeTimeCodeFramesFourParts(startParts), DecodeTimeCodeFramesFourParts(endParts), string.Empty);
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(p);
}
}
catch
{
_errorCount += 10;
}
}
else if (string.IsNullOrWhiteSpace(line))
{
// skip these lines
}
else if (p != null)
{
if (string.IsNullOrEmpty(p.Text))
p.Text = line;
else
p.Text = p.Text + Environment.NewLine + line;
}
else
{
_errorCount++;
}
}
subtitle.Renumber();
}
}
}