SubtitleEdit/libse/SubtitleFormats/Edl.cs

132 lines
6.3 KiB
C#
Raw Normal View History

2016-03-19 20:03:25 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class Edl : SubtitleFormat
{
2016-03-23 22:06:56 +01:00
private static readonly Regex Regex = new Regex(@"^\d+\s+[A-Z]{2}\s+[A-Z]\s+[A-Z]\s+\d\d:\d\d:\d\d:\d\d\s+\d\d:\d\d:\d\d:\d\d\s+\d\d:\d\d:\d\d:\d\d\s+\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
private const string TextPrefix = "* FROM CLIP NAME: ";
2016-03-19 20:03:25 +01:00
2017-08-03 12:43:52 +02:00
public override string Extension => ".edl";
2016-03-19 20:03:25 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "EDL";
2016-03-19 20:03:25 +01:00
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine("TITLE: " + title);
if (Configuration.Settings.General.CurrentFrameRate % 1.0 > 0.01)
2016-03-23 22:06:56 +01:00
sb.AppendLine("FCM: NON-DROP FRAME");
else
2016-03-19 20:03:25 +01:00
sb.AppendLine("FCM: DROP FRAME");
2016-03-23 22:06:56 +01:00
sb.AppendLine();
const string writeFormat = "{0:000000} {1} {2} {3} {4} {5} {6} {7}";
2016-03-19 20:03:25 +01:00
for (int index = 0; index < subtitle.Paragraphs.Count; index++)
{
2016-03-23 22:06:56 +01:00
int no = index + 1;
2016-03-19 20:03:25 +01:00
var p = subtitle.Paragraphs[index];
2016-03-23 22:06:56 +01:00
if (index == 0 && p.StartTime.TotalSeconds > 1)
{
var start = new TimeCode(p.StartTime.TotalMilliseconds - 1000.0);
var end = new TimeCode(p.StartTime.TotalMilliseconds - 1);
sb.AppendLine(string.Format(writeFormat, no, "BL", "V", "C", EncodeTimeCode(start), EncodeTimeCode(end), EncodeTimeCode(start), EncodeTimeCode(end)));
sb.AppendLine();
}
2016-03-19 20:03:25 +01:00
var text = HtmlUtil.RemoveHtmlTags(p.Text, true);
sb.AppendLine(string.Format(writeFormat, no, "AX", "V", "C", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime)));
2016-03-23 22:06:56 +01:00
sb.AppendLine(TextPrefix + text);
2016-03-19 20:03:25 +01:00
sb.AppendLine();
2016-03-23 22:06:56 +01:00
var next = subtitle.GetParagraphOrDefault(no);
if (next != null && next.StartTime.TotalMilliseconds > p.EndTime.TotalMilliseconds + 100)
{
var start = new TimeCode(p.EndTime.TotalMilliseconds + 1);
var end = new TimeCode(start.TotalMilliseconds + 1000);
if (end.TotalMilliseconds >= next.StartTime.TotalMilliseconds)
{
end = new TimeCode(next.StartTime.TotalMilliseconds - 1);
}
sb.AppendLine(string.Format(writeFormat, no, "BL", "V", "C", EncodeTimeCode(start), EncodeTimeCode(end), EncodeTimeCode(start), EncodeTimeCode(end)));
sb.AppendLine();
}
2016-03-19 20:03:25 +01:00
}
return sb.ToString().Trim() + Environment.NewLine;
}
private static string EncodeTimeCode(TimeCode timeCode)
{
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", timeCode.Hours, timeCode.Minutes, timeCode.Seconds, MillisecondsToFramesMaxFrameRate(timeCode.Milliseconds));
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{ //002 AX V C 01:00:01:15 01:00:04:18 00:00:01:15 00:00:04:18
2016-03-23 22:06:56 +01:00
//000002 AX V C 01:00:04:00 01:00:05:00 00:00:02:05 00:00:03:05
2016-03-19 20:03:25 +01:00
_errorCount = 0;
Paragraph lastParagraph = null;
int count = 0;
2016-03-23 22:06:56 +01:00
var splitChar = new[] { ' ' };
2016-03-19 20:03:25 +01:00
foreach (string line in lines)
{
bool isTimeCode = false;
if (line.Length > 0)
{
bool success = false;
2016-03-23 22:06:56 +01:00
if (line.Length > 65 && line.Length < 85 && line.IndexOf(':') > 20)
2016-03-19 20:03:25 +01:00
{
var match = Regex.Match(line);
if (match.Success)
{
isTimeCode = true;
2016-03-23 22:06:56 +01:00
if (lastParagraph != null && Math.Abs(lastParagraph.StartTime.TotalMilliseconds + 1) > 0.001)
2016-03-19 20:03:25 +01:00
subtitle.Paragraphs.Add(lastParagraph);
2016-03-23 22:06:56 +01:00
var arr = line.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
try
{
if (arr.Length == 8 && arr[1] != "BL")
{
var start = DecodeTimeCodeFrames(arr[6], SplitCharColon);
var end = DecodeTimeCodeFrames(arr[7], SplitCharColon);
lastParagraph = new Paragraph(start, end, string.Empty);
success = true;
}
else
{
lastParagraph = new Paragraph(string.Empty, -1, -1);
}
}
catch
{
_errorCount++;
}
2016-03-19 20:03:25 +01:00
}
}
if (!isTimeCode && !string.IsNullOrWhiteSpace(line) && lastParagraph != null && Utilities.GetNumberOfLines(lastParagraph.Text) < 5)
{
lastParagraph.Text = (lastParagraph.Text + Environment.NewLine + line).Trim();
success = true;
}
if (!success && count > 9)
_errorCount++;
}
count++;
}
if (lastParagraph != null)
2016-03-23 22:06:56 +01:00
{
2016-03-19 20:03:25 +01:00
subtitle.Paragraphs.Add(lastParagraph);
2016-03-23 22:06:56 +01:00
}
foreach (var paragraph in subtitle.Paragraphs)
{
if (paragraph.Text.StartsWith(TextPrefix, StringComparison.Ordinal))
{
paragraph.Text = paragraph.Text.Remove(0, TextPrefix.Length).TrimStart();
}
}
2016-03-19 20:03:25 +01:00
subtitle.Renumber();
}
}
}