mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-10-27 22:42:38 +01:00
Added format SMIL Timesheet (data-* version) - Issue 121
git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1623 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
parent
103087710c
commit
d4faf96d04
214
src/Logic/SubtitleFormats/SmilTimesheetData.cs
Normal file
214
src/Logic/SubtitleFormats/SmilTimesheetData.cs
Normal file
@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
{
|
||||
/// <summary>
|
||||
/// http://wam.inrialpes.fr/timesheets/annotations/video.html
|
||||
/// </summary>
|
||||
public class SmilTimesheetData : SubtitleFormat
|
||||
{
|
||||
public override string Extension
|
||||
{
|
||||
get { return ".html"; }
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "SMIL Timesheet"; }
|
||||
}
|
||||
|
||||
public override bool IsTimeBased
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool IsMine(List<string> lines, string fileName)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (string l in lines)
|
||||
sb.AppendLine(l);
|
||||
if (!sb.ToString().Contains(" data-begin"))
|
||||
return false;
|
||||
|
||||
var subtitle = new Subtitle();
|
||||
LoadSubtitle(subtitle, lines, fileName);
|
||||
return subtitle.Paragraphs.Count > _errorCount;
|
||||
}
|
||||
|
||||
public override string ToText(Subtitle subtitle, string title)
|
||||
{
|
||||
string header =
|
||||
@"<!DOCTYPE html>
|
||||
<html>
|
||||
<head>" + Environment.NewLine +
|
||||
"\t<meta charset=\"utf-8\">" + Environment.NewLine +
|
||||
"\t<title>SMIL Timesheet</title>" + @"
|
||||
</head>
|
||||
<body>" + Environment.NewLine +
|
||||
"\t<div id=\"media\" data-timecontainer=\"excl\" data-timeaction=\"display\">" + Environment.NewLine +
|
||||
"\t\t<video data-syncmaster=\"true\" data-timeaction=\"none\" controls=\"true\" preload=\"auto\">" + Environment.NewLine +
|
||||
"\t\t\t<source type=\"video/webm\" src=\"_TITLE_.webm\"/>" + Environment.NewLine +
|
||||
"\t\t\t<source type=\"video/ogg\" src=\"_TITLE_.ogv\" />" + Environment.NewLine +
|
||||
"\t\t\t<source type=\"video/mp4\" src=\"_TITLE_.mp4\" />" + Environment.NewLine +
|
||||
"\t\t\tYour browser does not support the HTML5 <video> tag" + Environment.NewLine +
|
||||
"\t\t</video>" + Environment.NewLine;
|
||||
|
||||
string paragraphWriteFormatOpen = "\t\t<p data-begin=\"{0}\">\r\n{1}\r\n</p>";
|
||||
string paragraphWriteFormat = "\t\t<p data-begin=\"{0}\" data-end=\"{1}\">\r\n{2}\r\n</p>";
|
||||
int count = 1;
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine(header.Replace("_TITLE_", title));
|
||||
foreach (Paragraph p in subtitle.Paragraphs)
|
||||
{
|
||||
Paragraph next = subtitle.GetParagraphOrDefault(count);
|
||||
string text = p.Text;
|
||||
if (next != null && Math.Abs(next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds) < 100)
|
||||
sb.AppendLine(string.Format(paragraphWriteFormatOpen, EncodeTime(p.StartTime), text));
|
||||
else
|
||||
sb.AppendLine(string.Format(paragraphWriteFormat, EncodeTime(p.StartTime), EncodeTime(p.EndTime), text));
|
||||
count++;
|
||||
}
|
||||
sb.AppendLine("\t</div>");
|
||||
sb.AppendLine("</body>");
|
||||
sb.AppendLine("</html>");
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
|
||||
private string EncodeTime(TimeCode time)
|
||||
{
|
||||
//3:15:22
|
||||
if (time.Hours > 0)
|
||||
return string.Format("{0:00}:{1:00}:{2:00}.{3:00}", time.Hours, time.Minutes, time.Seconds, time.Milliseconds / 10);
|
||||
if (time.Minutes > 9)
|
||||
return string.Format("{0:00}:{1:00}.{2:00}", time.Minutes, time.Seconds, time.Milliseconds / 10);
|
||||
return string.Format("{0}:{1:00}.{2:00}", time.Minutes, time.Seconds, time.Milliseconds / 10);
|
||||
}
|
||||
|
||||
private TimeCode DecodeTimeCode(string[] s)
|
||||
{
|
||||
if (s.Length == 3)
|
||||
return new TimeCode(0, int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]) * 10);
|
||||
return new TimeCode(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]), int.Parse(s[3]) * 10);
|
||||
}
|
||||
|
||||
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
||||
{
|
||||
_errorCount = 0;
|
||||
var sb = new StringBuilder();
|
||||
foreach (string l in lines)
|
||||
sb.AppendLine(l);
|
||||
string allInput = sb.ToString();
|
||||
string allInputLower = allInput.ToLower();
|
||||
const string syncTag = "<p ";
|
||||
int syncStartPos = allInputLower.IndexOf(syncTag);
|
||||
int index = syncStartPos + syncTag.Length;
|
||||
while (syncStartPos >= 0)
|
||||
{
|
||||
int syncEndPos = allInputLower.IndexOf("</p>", index);
|
||||
if (syncEndPos > 0)
|
||||
{
|
||||
string s = allInput.Substring(syncStartPos+2, syncEndPos - syncStartPos -2);
|
||||
int indexOfBegin = s.IndexOf(" data-begin=");
|
||||
int indexOfAttributesEnd = s.IndexOf(">");
|
||||
if (indexOfBegin >= 0 && indexOfAttributesEnd > indexOfBegin)
|
||||
{
|
||||
string text = s.Substring(indexOfAttributesEnd).Remove(0, 1).Trim();
|
||||
text = text.Replace("<br>", Environment.NewLine);
|
||||
text = text.Replace("<br/>", Environment.NewLine);
|
||||
text = text.Replace("<br />", Environment.NewLine);
|
||||
text = text.Replace("\t", " ");
|
||||
while (text.Contains(" "))
|
||||
text = text.Replace(" ", " ");
|
||||
while (text.Contains(Environment.NewLine + " "))
|
||||
text = text.Replace(Environment.NewLine + " ", Environment.NewLine);
|
||||
while (text.Contains(Environment.NewLine + Environment.NewLine))
|
||||
text = text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
|
||||
|
||||
string begin = s.Substring(indexOfBegin + " data-begin=".Length);
|
||||
var tcBegin = new StringBuilder();
|
||||
for (int i = 0; i <= 10; i++)
|
||||
{
|
||||
if (begin.Length > i && "0123456789:.".Contains(begin[i].ToString()))
|
||||
{
|
||||
tcBegin.Append(begin[i].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
var tcEnd = new StringBuilder();
|
||||
int indexOfEnd = s.IndexOf(" data-end=");
|
||||
if (indexOfEnd >= 0)
|
||||
{
|
||||
string end = s.Substring(indexOfEnd + " data-end=".Length);
|
||||
for (int i = 0; i <= 10; i++)
|
||||
{
|
||||
if (end.Length > i && "0123456789:.".Contains(end[i].ToString()))
|
||||
{
|
||||
tcEnd.Append(end[i].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var arr = tcBegin.ToString().Split(".:".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
||||
if (arr.Length == 3 || arr.Length == 4)
|
||||
{
|
||||
var p = new Paragraph();
|
||||
p.Text = text;
|
||||
try
|
||||
{
|
||||
p.StartTime = DecodeTimeCode(arr);
|
||||
if (tcEnd.Length > 0)
|
||||
{
|
||||
arr = tcEnd.ToString().Split(".:".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
||||
p.EndTime = DecodeTimeCode(arr);
|
||||
}
|
||||
subtitle.Paragraphs.Add(p);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_errorCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (syncEndPos <= 0)
|
||||
{
|
||||
syncStartPos = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
syncStartPos = allInputLower.IndexOf(syncTag, syncEndPos);
|
||||
index = syncStartPos + syncTag.Length;
|
||||
}
|
||||
}
|
||||
|
||||
index = 1;
|
||||
foreach (Paragraph paragraph in subtitle.Paragraphs)
|
||||
{
|
||||
Paragraph next = subtitle.GetParagraphOrDefault(index);
|
||||
if (next != null)
|
||||
{
|
||||
paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1;
|
||||
}
|
||||
else if (paragraph.EndTime.TotalMilliseconds < 50)
|
||||
{
|
||||
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
|
||||
}
|
||||
if (paragraph.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
|
||||
{
|
||||
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
foreach (Paragraph p2 in subtitle.Paragraphs)
|
||||
p2.Text = Utilities.HtmlDecode(p2.Text);
|
||||
|
||||
subtitle.Renumber(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -71,6 +71,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
new SamiModern(),
|
||||
new Scenarist(),
|
||||
new ScenaristClosedCaptions(),
|
||||
new SmilTimesheetData(),
|
||||
new SonyDVDArchitect(),
|
||||
new SonyDVDArchitectExplicitDuration(),
|
||||
new SonyDVDArchitectLineAndDuration(),
|
||||
|
@ -90,7 +90,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
}
|
||||
if (paragraph.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
|
||||
{
|
||||
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text);
|
||||
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
@ -804,6 +804,7 @@
|
||||
<Compile Include="Logic\SubtitleFormats\SamiModern.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\Scenarist.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\ScenaristClosedCaptions.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\SmilTimesheetData.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\SoftNiSub.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\Son.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\SonicScenaristBitmaps.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user