SubtitleEdit/libse/SubtitleFormats/CsvNuendo.cs

105 lines
3.8 KiB
C#
Raw Normal View History

2017-12-09 08:39:31 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class CsvNuendo : SubtitleFormat
{
2017-12-21 20:33:36 +01:00
private static readonly Regex CsvLine = new Regex("^.*,[+\\d+:]+,[+\\d+:]+,\".+", RegexOptions.Compiled);
2017-12-22 17:15:04 +01:00
private const string LineFormat = "{1}{0}{2}{0}{3}{0}{4}";
private static string Header = string.Format(LineFormat, ",", "\"Character\"", "\"Timecode In\"", "\"Timecode Out\"", "\"Dialogue\"");
2017-12-09 08:39:31 +01:00
public override string Extension => ".csv";
public override string Name => "Csv Nuendo";
public override bool IsMine(List<string> lines, string fileName)
{
int fine = 0;
var sb = new StringBuilder();
foreach (string line in lines)
{
sb.Append(line);
if (CsvLine.IsMatch(line))
2019-01-19 14:40:37 +01:00
{
2017-12-09 08:39:31 +01:00
fine++;
2019-01-19 14:40:37 +01:00
}
2017-12-09 08:39:31 +01:00
}
return fine > 0 && sb.ToString().Contains(Header);
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine(Header);
foreach (Paragraph p in subtitle.Paragraphs)
{
2017-12-22 17:15:04 +01:00
string text = string.IsNullOrEmpty(p.Text) ? string.Empty : "\"" + p.Text.Replace("\"", "\"\"").Replace(Environment.NewLine, "\n") + "\"";
string actor = string.IsNullOrEmpty(p.Actor) ? string.Empty : "\"" + p.Actor.Replace(",", " ").Replace("\"", string.Empty) + "\"";
sb.AppendLine(string.Format(LineFormat, ",", actor, p.StartTime.ToHHMMSSFF(), p.EndTime.ToHHMMSSFF(), text));
2017-12-09 08:39:31 +01:00
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
bool continuation = false;
Paragraph p = null;
foreach (string line in lines)
{
if (CsvLine.IsMatch(line))
{
2017-12-21 20:33:36 +01:00
string[] parts = line.Split(',');
2017-12-09 08:39:31 +01:00
if (parts.Length == 4)
2019-01-19 14:40:37 +01:00
{
2017-12-09 08:39:31 +01:00
try
{
2017-12-21 20:33:36 +01:00
var actor = Utilities.FixQuotes(parts[0]);
2017-12-09 08:39:31 +01:00
var start = DecodeTime(parts[1]);
var end = DecodeTime(parts[2]);
string text = Utilities.FixQuotes(parts[3]);
p = new Paragraph(start, end, text);
if (!string.IsNullOrEmpty(actor))
2019-01-19 14:40:37 +01:00
{
2017-12-09 08:39:31 +01:00
p.Actor = actor;
2019-01-19 14:40:37 +01:00
}
2017-12-09 08:39:31 +01:00
subtitle.Paragraphs.Add(p);
continuation = parts[3].StartsWith('"') && !parts[3].EndsWith('"');
}
catch
{
_errorCount++;
}
2019-01-19 14:40:37 +01:00
}
2017-12-09 08:39:31 +01:00
}
else
{
if (continuation)
{
if (p.Text.Length < 300)
2019-01-19 14:40:37 +01:00
{
2017-12-09 08:39:31 +01:00
p.Text = (p.Text + Environment.NewLine + line.TrimEnd('"')).Trim();
2019-01-19 14:40:37 +01:00
}
2017-12-09 08:39:31 +01:00
continuation = !line.TrimEnd().EndsWith('"');
}
else
{
_errorCount++;
}
}
}
subtitle.Renumber();
}
private TimeCode DecodeTime(string s)
{
2017-12-21 20:33:36 +01:00
return DecodeTimeCodeFramesFourParts(s.Split(new char[] { ':' }));
2017-12-09 08:39:31 +01:00
}
}
}