Added new subtitle format

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1911 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2013-07-04 20:26:40 +00:00
parent d780e9e230
commit 8ecef2d739
6 changed files with 173 additions and 2 deletions

View File

@ -757,7 +757,7 @@ namespace Nikse.SubtitleEdit.Forms
var fi = new FileInfo(fileName);
if (fi.Length < 1024 * 1024 && !done) // max 1 mb
{
format = sub.LoadSubtitle(fileName, out encoding, null);
format = sub.LoadSubtitle(fileName, out encoding, null, true);
if (format == null)
{
@ -773,6 +773,7 @@ namespace Nikse.SubtitleEdit.Forms
var pac = new Pac();
if (pac.IsMine(null, fileName))
{
pac.BatchMode = true;
pac.LoadSubtitle(sub, null, fileName);
format = pac;
}
@ -931,12 +932,13 @@ namespace Nikse.SubtitleEdit.Forms
if (sf.Name.ToLower().Replace(" ", string.Empty) == toFormat.ToLower() || sf.Name.ToLower().Replace(" ", string.Empty) == toFormat.Replace(" ", string.Empty).ToLower())
{
targetFormatFound = true;
sf.BatchMode = true;
outputFileName = FormatOutputFileNameForBatchConvert(fileName, sf.Extension, outputFolder, overwrite);
Console.Write(string.Format("{0}: {1} -> {2}...", count, Path.GetFileName(fileName), outputFileName));
if (sf.IsFrameBased && !sub.WasLoadedWithFrameNumbers)
sub.CalculateFrameNumbersFromTimeCodesNoCheck(Configuration.Settings.General.CurrentFrameRate);
else if (sf.IsTimeBased && sub.WasLoadedWithFrameNumbers)
sub.CalculateTimeCodesFromFrameNumbers(Configuration.Settings.General.CurrentFrameRate);
sub.CalculateTimeCodesFromFrameNumbers(Configuration.Settings.General.CurrentFrameRate);
File.WriteAllText(outputFileName, sub.ToText(sf), targetEncoding);
if (format.GetType() == typeof(Sami) || format.GetType() == typeof(SamiModern))
{

View File

@ -106,6 +106,11 @@ namespace Nikse.SubtitleEdit.Logic
}
public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding)
{
return LoadSubtitle(fileName, out encoding, useThisEncoding, false);
}
public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding, bool batchMode)
{
FileName = fileName;
@ -149,6 +154,7 @@ namespace Nikse.SubtitleEdit.Logic
if (subtitleFormat.IsMine(lines, fileName))
{
Header = null;
subtitleFormat.BatchMode = batchMode;
subtitleFormat.LoadSubtitle(this, lines, fileName);
_format = subtitleFormat;
_wasLoadedWithFrameNumbers = _format.IsFrameBased;

View File

@ -1095,6 +1095,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private void GetCodePage( byte[] buffer, int index, int endDelimiter)
{
if (BatchMode)
{
_codePage = -2;
return;
}
byte[] previewBuffer = null;
if (buffer != null)

View File

@ -185,6 +185,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new UnknownSubtitle58(),
new UnknownSubtitle59(),
new UnknownSubtitle60(),
new UnknownSubtitle61(),
};
string path = Configuration.PluginsDirectory;
@ -332,6 +333,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
}
public bool BatchMode { get; set; }
public static string ToUtf8XmlString(XmlDocument xml, bool omitXmlDeclaration)
{
XmlWriterSettings settings = new XmlWriterSettings();

View File

@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class UnknownSubtitle61 : SubtitleFormat
{
//00:02:23.59
//קרוליין: פשוט תשימי את זה בפייסבוק או משהו.
//00:02:25.78
//הם בטוח יאהבו את זה.
//00:02:27.78
//ליזי: אוקיי. אז אני מניחה שזה זמן הנתינה
static Regex regexTimeCodes1 = new Regex(@"^\d\d:\d\d:\d\d\.\d\d$", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Unknown 61"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
var subtitle = new Subtitle();
var sb = new StringBuilder();
foreach (string line in lines)
sb.AppendLine(line);
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(EncodeTimeCode(p.StartTime));
sb.AppendLine(EncodeTimeCode(p.EndTime));
sb.AppendLine(Utilities.RemoveHtmlTags(p.Text));
sb.AppendLine();
}
return sb.ToString();
}
private string EncodeTimeCode(TimeCode time)
{
return string.Format("{0:00}:{1:00}:{2:00}.{3:00}", time.Hours, time.Minutes, time.Seconds, time.Milliseconds / 10);
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
bool expectStartTime = true;
var p = new Paragraph();
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
string s = line.Trim();
var match = regexTimeCodes1.Match(s);
if (match.Success && s.Length == 11)
{
if (!expectStartTime)
_errorCount++;
if (p != null && p.StartTime.TotalMilliseconds > 0)
{
subtitle.Paragraphs.Add(p);
if (string.IsNullOrEmpty(p.Text))
_errorCount++;
}
p = new Paragraph();
string[] parts = s.Split(":.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
{
p.StartTime = DecodeTimeCode(parts);
expectStartTime = false;
}
catch (Exception exception)
{
_errorCount++;
System.Diagnostics.Debug.WriteLine(exception.Message);
expectStartTime = true;
}
}
}
else if (line.Trim().Length > 0 && !expectStartTime)
{
p.Text = (p.Text + Environment.NewLine + line).Trim();
if (p.Text.Length > 5000)
{
_errorCount += 10;
return;
}
}
else if (line.Trim().Length == 0)
{
expectStartTime = true;
}
}
if (p != null && p.StartTime.TotalMilliseconds > 0)
subtitle.Paragraphs.Add(p);
bool allNullEndTime = true;
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
{
p = subtitle.Paragraphs[i];
if (p.EndTime.TotalMilliseconds != 0)
allNullEndTime = false;
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text);
if (i < subtitle.Paragraphs.Count - 2 &&p.EndTime.TotalMilliseconds >= subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds)
p.EndTime.TotalMilliseconds = subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds - Configuration.Settings.General.MininumMillisecondsBetweenLines;
}
if (!allNullEndTime)
subtitle.Paragraphs.Clear();
subtitle.RemoveEmptyLines();
subtitle.Renumber(1);
}
private TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string msDiv10 = parts[3];
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), int.Parse(msDiv10) * 10);
}
}
}

View File

@ -809,6 +809,7 @@
<Compile Include="Logic\SubtitleFormats\CaraokeXml.cs" />
<Compile Include="Logic\SubtitleFormats\Cavena890.cs" />
<Compile Include="Logic\SubtitleFormats\CheetahCaption.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle61.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle60.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle59.cs" />
<Compile Include="Logic\SubtitleFormats\DvdSubtitleSystem.cs" />
@ -1426,6 +1427,7 @@
</Content>
<Content Include="Languages\cs-CZ.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<SubType>Designer</SubType>
</Content>
<Content Include="Languages\da-DK.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>