Some updates for format "sif"

This commit is contained in:
Nikolaj Olsson 2016-03-27 18:03:54 +02:00
parent 0f195f3097
commit a485d5dd3a

View File

@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class SwiftInterchange2 : SubtitleFormat
{
private const string ItalicPrefix = "<fontstyle-italic>";
private string _fileName;
public override string Extension
{
get { return ".sif"; }
@ -26,17 +30,63 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (lines.Count > 0 && lines[0] != null && lines[0].StartsWith("{\\rtf1"))
return false;
_fileName = fileName;
var subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
private string GetOriginatingSwift(Subtitle subtitle)
{
string lang = "English (USA)";
string languageCode = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);
if (languageCode == "nl")
{
lang = "Dutch (Netherlands)";
}
else if (languageCode == "de")
{
lang = "German (German)";
}
return "Open 25 " + lang;
// examples:
// Line21 30 DROP English (USA)
// Open 25 German (German)
// Open 25 Dutch (Netherlands)
//TODO: Frame rate
}
private string GetVideoFileName(string title)
{
string fileNameNoExt = null;
if (_fileName != null)
{
fileNameNoExt = _fileName.Substring(0, _fileName.Length - Path.GetExtension(_fileName).Length);
}
foreach (var ext in Utilities.GetMovieFileExtensions())
{
if (!string.IsNullOrEmpty(fileNameNoExt) && File.Exists(Path.Combine(fileNameNoExt, ext)))
{
return Path.Combine(fileNameNoExt, ext);
}
if (!string.IsNullOrEmpty(title) && File.Exists(Path.Combine(title, ext)))
{
return Path.Combine(title, ext);
}
}
if (string.IsNullOrEmpty(title))
{
return "Unknown.mpg";
}
return title + ".mpg";
}
public override string ToText(Subtitle subtitle, string title)
{
string date = string.Format("{0:00}/{1:00}/{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
const string header = @"# SWIFT INTERCHANGE FILE V2
# DO NOT EDIT LINES BEGINNING WITH '#' SIGN
# Originating Swift: Line21 30 DROP English (USA)
# Originating Swift: [ORIGINATING_SWIFT]
# VIDEO CLIP : [VIDEO_FILE]
# BROADCAST DATE : [DATE]
# REVISION DATE : [DATE]
@ -47,33 +97,43 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
# AUTO TX : false
# CURRENT STYLE : None
# STYLE DATE : None
# STYLE Time : None
# SUBTITLE [1] RU3
# TIMEIN 01:00:00:06
# DURATION 03:21 AUTO
# TIMEOUT --:--:--:--
# START ROW BOTTOM
# ALIGN CENTRE JUSTIFY LEFT
# ROW 0";
# STYLE Time : None";
var sb = new StringBuilder();
sb.AppendLine(header.Replace("[DATE]", date).Replace("[VIDEO_FILE]", title + ".mpg"));
var videoFileName = GetVideoFileName(title);
sb.AppendLine(header.Replace("[DATE]", date).Replace("[VIDEO_FILE]", videoFileName).Replace("[ORIGINATING_SWIFT]", GetOriginatingSwift(subtitle)));
sb.AppendLine();
sb.AppendLine();
const string paragraphWriteFormat = @"# SUBTITLE [{3}] RU3
const string paragraphWriteFormat = @"# SUBTITLE {3}
# TIMEIN {0}
# DURATION {1} AUTO
# TIMEOUT --:--:--:--
# TIMEOUT {2}
# START ROW BOTTOM
# ALIGN CENTRE JUSTIFY LEFT
# ROW 0
{2}";
int count = 2;
# ALIGN CENTRE JUSTIFY LEFT";
int count = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
string startTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, MillisecondsToFramesMaxFrameRate(p.StartTime.Milliseconds));
string endTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, MillisecondsToFramesMaxFrameRate(p.EndTime.Milliseconds));
string duration = string.Format("{0:00}:{1:00}", p.Duration.Seconds, MillisecondsToFramesMaxFrameRate(p.Duration.Milliseconds));
sb.AppendLine(string.Format(paragraphWriteFormat, startTime, duration, HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " ")), count));
sb.AppendLine();
sb.AppendLine(string.Format(paragraphWriteFormat, startTime, duration, endTime, count));
string text = HtmlUtil.RemoveHtmlTags(p.Text);
if (p.Text.StartsWith("<i>") && p.Text.EndsWith("</i>"))
{
text = ItalicPrefix + text;
}
var arr = text.SplitToLines();
for (int rowNo = 0; rowNo < arr.Length; rowNo++)
{
if (rowNo == arr.Length - 1)
{
sb.AppendLine("# ROW " + rowNo);
}
else
{
sb.AppendLine("# ROW " + rowNo + " RETURN");
}
sb.AppendLine(arr[rowNo]);
}
sb.AppendLine();
count++;
}
@ -137,13 +197,21 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
subtitle.Paragraphs.Add(p);
subtitle.RemoveEmptyLines();
subtitle.Renumber();
foreach (var paragraph in subtitle.Paragraphs)
{
if (paragraph.Text.StartsWith(ItalicPrefix))
{
paragraph.Text = "<i>" + paragraph.Text.Remove(0, ItalicPrefix.Length).TrimStart() + "</i>";
}
}
}
private static bool GetTimeCode(TimeCode timeCode, string timeString)
{
try
{
string[] timeParts = timeString.Split(new[] { ':', '.' });
string[] timeParts = timeString.Split(':', '.');
timeCode.Hours = int.Parse(timeParts[0]);
timeCode.Minutes = int.Parse(timeParts[1]);
timeCode.Seconds = int.Parse(timeParts[2]);
@ -155,5 +223,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
return false;
}
}
}
}