SubtitleEdit/libse/SubtitleFormats/Idx.cs

142 lines
4.8 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
// TODO: Working on added edit capabilities for idx files...
public class Idx : SubtitleFormat
{
// timestamp: 00:00:01:401, filepos: 000000000
2016-02-25 07:38:13 +01:00
private static readonly Regex RegexTimeCodes = new Regex(@"^timestamp: \d+:\d+:\d+:\d+, filepos: [\dabcdefABCDEF]+$", RegexOptions.Compiled);
2016-02-08 21:11:03 +01:00
private readonly Hashtable _nonTimeCodes = new Hashtable();
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Extension => ".idx";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "VobSub index file";
2016-02-08 21:11:03 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
int subtitleCount = 0;
foreach (string line in lines)
{
if (line.StartsWith("timestamp: ", StringComparison.Ordinal))
{
if (++subtitleCount > 10)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return true;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
return false;
}
public override string ToText(Subtitle subtitle, string title)
{
// timestamp: 00:00:01:401, filepos: 000000000
const string paragraphWriteFormat = "timestamp: {0}, filepos: {1}";
var tempNonTimeCodes = new Hashtable();
if (subtitle.OriginalFormat != null)
2016-02-08 21:11:03 +01:00
{
foreach (DictionaryEntry de in ((Idx)subtitle.OriginalFormat)._nonTimeCodes)
{
tempNonTimeCodes.Add(de.Key, de.Value);
}
2016-02-08 21:11:03 +01:00
}
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
var removeList = new List<int>();
foreach (DictionaryEntry de in tempNonTimeCodes)
{
if (Convert.ToInt32(de.Key) < Convert.ToInt32(p.Text))
{
sb.AppendLine(de.Value.ToString());
removeList.Add(Convert.ToInt32(de.Key));
}
}
foreach (int key in removeList)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
tempNonTimeCodes.Remove(key);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
sb.AppendLine(string.Format(paragraphWriteFormat, p.StartTime, p.Text));
}
foreach (DictionaryEntry de in tempNonTimeCodes)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
sb.AppendLine(de.Value.ToString());
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
subtitle.Paragraphs.Clear();
2016-02-12 07:01:17 +01:00
char[] splitChars = { ',', ':' };
2016-02-08 21:11:03 +01:00
foreach (string line in lines)
{
2016-02-25 07:38:13 +01:00
if (RegexTimeCodes.IsMatch(line))
2016-02-08 21:11:03 +01:00
{
2016-02-12 07:01:17 +01:00
Paragraph p = GetParagraph(line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries));
2016-02-08 21:11:03 +01:00
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(p);
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
_errorCount++;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
else
{
if (subtitle.Paragraphs.Count == 0 ||
!int.TryParse(subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].Text, out var place))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
place = -1;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
if (_nonTimeCodes.ContainsKey(place))
2019-01-19 14:40:37 +01:00
{
_nonTimeCodes[place] += Environment.NewLine + 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
{
_nonTimeCodes.Add(place, line);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
}
2016-02-12 07:01:17 +01:00
private static Paragraph GetParagraph(string[] parts)
2016-02-08 21:11:03 +01:00
{
// timestamp: 00:00:01:401, filepos: 000000000
if (parts.Length == 7)
{
if (int.TryParse(parts[1], out var hours) &&
int.TryParse(parts[2], out var minutes) &&
int.TryParse(parts[3], out var seconds) &&
int.TryParse(parts[4], out var milliseconds))
2016-02-08 21:11:03 +01:00
{
return new Paragraph
{
StartTime = { TimeSpan = new TimeSpan(0, hours, minutes, seconds, milliseconds) },
Text = parts[6]
};
}
}
return null;
}
}
}