Added new subtitle format

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1768 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2013-03-30 19:25:36 +00:00
parent 3a6b4ba203
commit 6734b257cf
4 changed files with 111 additions and 2 deletions

View File

@ -107,7 +107,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
return s; return s;
} }
public static string ReadTag(string s, string tag) public static string ReadTag(string s, string tag)
{ {
int startIndex = s.IndexOf("\"" + tag + "\""); int startIndex = s.IndexOf("\"" + tag + "\"");
@ -121,8 +120,11 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
res = Json.ConvertJsonSpecialCharacters(res); res = Json.ConvertJsonSpecialCharacters(res);
res = res.Replace("\\\"", "@__1"); res = res.Replace("\\\"", "@__1");
int endIndex = res.IndexOf("\"}"); int endIndex = res.IndexOf("\"}");
int endAlternate = res.IndexOf("\",");
if (endIndex == -1) if (endIndex == -1)
endIndex = res.IndexOf("\","); endIndex = endAlternate;
else if (endAlternate > 0 && endAlternate < endIndex)
endIndex = endAlternate;
if (endIndex == -1) if (endIndex == -1)
return null; return null;
if (res.Length > 1) if (res.Length > 1)

View File

@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class JsonType4 : SubtitleFormat
{
public override string Extension
{
get { return ".json"; }
}
public override string Name
{
get { return "JSON Type 4"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
var subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.Append("[");
int count = 0;
string guid = Guid.NewGuid().ToString();
string segmentTypeId = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 24);
foreach (Paragraph p in subtitle.Paragraphs)
{
string id = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 24);
if (count > 0)
sb.Append(",");
sb.Append("{\"hitType\":\"tag\",\"subTrack\":null,\"tags\":[],\"track\":\"Closed Captioning\",\"startTime\":");
sb.Append(p.StartTime.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"guid\":\"" + guid + "\",\"segmentTypeId\":\"" + segmentTypeId + "\",\"endTime\":");
sb.Append(p.EndTime.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"id\":\"" + id + "\",\"metadata\":{\"Text\":\"");
sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "\\n") + "\"");
sb.Append(",\"ID\":\"\",\"Language\":\"en\"}}");
count++;
}
sb.Append("]");
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
foreach (string s in lines)
sb.Append(s);
int startIndex = sb.ToString().IndexOf("[{\"hitType");
if (startIndex == -1)
return;
string text = sb.ToString().Substring(startIndex);
foreach (string line in text.Replace("},{", Environment.NewLine).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
string s = line.Trim() + "}";
string start = Json.ReadTag(s, "startTime");
string end = Json.ReadTag(s, "endTime");
string content = Json.ReadTag(s, "Text");
if (start != null && end != null && content != null)
{
double startSeconds;
double endSeconds;
if (double.TryParse(start, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out startSeconds) &&
double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds) &&
content != null)
{
content = content.Replace("<br />", Environment.NewLine);
content = content.Replace("<br>", Environment.NewLine);
content = content.Replace("<br/>", Environment.NewLine);
content = content.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(content, startSeconds * 1000.0, endSeconds * 1000.0));
}
else
{
_errorCount++;
}
}
else
{
_errorCount++;
}
}
subtitle.Renumber(1);
}
}
}

View File

@ -58,6 +58,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new Json(), new Json(),
new JsonType2(), new JsonType2(),
new JsonType3(), new JsonType3(),
new JsonType4(),
new MicroDvd(), new MicroDvd(),
new MidwayInscriberCGX(), new MidwayInscriberCGX(),
new MPlayer2(), new MPlayer2(),

View File

@ -788,6 +788,7 @@
<Compile Include="Logic\SubtitleFormats\CaraokeXml.cs" /> <Compile Include="Logic\SubtitleFormats\CaraokeXml.cs" />
<Compile Include="Logic\SubtitleFormats\Cavena890.cs" /> <Compile Include="Logic\SubtitleFormats\Cavena890.cs" />
<Compile Include="Logic\SubtitleFormats\CheetahCaption.cs" /> <Compile Include="Logic\SubtitleFormats\CheetahCaption.cs" />
<Compile Include="Logic\SubtitleFormats\JsonType4.cs" />
<Compile Include="Logic\SubtitleFormats\DCinemaSmpte.cs" /> <Compile Include="Logic\SubtitleFormats\DCinemaSmpte.cs" />
<Compile Include="Logic\SubtitleFormats\DCSubtitle.cs" /> <Compile Include="Logic\SubtitleFormats\DCSubtitle.cs" />
<Compile Include="Logic\SubtitleFormats\DvdStudioProSpace.cs" /> <Compile Include="Logic\SubtitleFormats\DvdStudioProSpace.cs" />