Can now import+ocr bdn-xml w images

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@411 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2011-04-25 12:41:18 +00:00
parent 46a20a0468
commit 89b4e1fcfa
7 changed files with 319 additions and 45 deletions

View File

@ -1220,6 +1220,12 @@ namespace Nikse.SubtitleEdit.Forms
return;
}
if (Path.GetExtension(fileName).ToLower() == ".mkv")
{
ImportSubtitleFromMatroskaFile();
return;
}
var fi = new FileInfo(fileName);
if (fi.Length > 1024 * 1024 * 10) // max 10 mb
{
@ -1275,6 +1281,24 @@ namespace Nikse.SubtitleEdit.Forms
}
}
if (format == null)
{
var bdnXml = new BdnXml();
string[] arr = File.ReadAllLines(fileName);
List<string> list = new List<string>();
foreach (string l in arr)
list.Add(l);
if (bdnXml.IsMine(list, fileName))
{
if (ContinueNewOrExit())
{
ImportAndOcrBdnXml(fileName, bdnXml, list);
}
return;
}
}
_fileDateTime = File.GetLastWriteTime(fileName);
if (GetCurrentSubtitleFormat().IsFrameBased)
@ -1414,6 +1438,18 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private void ImportAndOcrBdnXml(string fileName, BdnXml bdnXml, List<string> list)
{
Subtitle bdnSubtitle = new Subtitle();
bdnXml.LoadSubtitle(bdnSubtitle, list, fileName);
bdnSubtitle.FileName = fileName;
var formSubOcr = new VobSubOcr();
formSubOcr.Initialize(bdnSubtitle, Configuration.Settings.VobSubOcr);
if (formSubOcr.ShowDialog(this) == DialogResult.OK)
{
}
}
private void ShowUnknownSubtitle()
{
var unknownSubtitle = new UnknownSubtitle();
@ -4853,40 +4889,45 @@ namespace Nikse.SubtitleEdit.Forms
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
openFileDialog1.InitialDirectory = Path.GetDirectoryName(openFileDialog1.FileName);
bool isValid;
var matroska = new Matroska();
var subtitleList = matroska.GetMatroskaSubtitleTracks(openFileDialog1.FileName, out isValid);
if (isValid)
ImportSubtitleFromMatroskaFile();
}
}
private void ImportSubtitleFromMatroskaFile()
{
bool isValid;
var matroska = new Matroska();
var subtitleList = matroska.GetMatroskaSubtitleTracks(openFileDialog1.FileName, out isValid);
if (isValid)
{
if (subtitleList.Count == 0)
{
if (subtitleList.Count == 0)
{
MessageBox.Show(_language.NoSubtitlesFound);
}
else
{
if (ContinueNewOrExit())
{
if (subtitleList.Count > 1)
{
MatroskaSubtitleChooser subtitleChooser = new MatroskaSubtitleChooser();
subtitleChooser.Initialize(subtitleList);
if (subtitleChooser.ShowDialog(this) == DialogResult.OK)
{
LoadMatroskaSubtitle(subtitleList[subtitleChooser.SelectedIndex], openFileDialog1.FileName);
}
}
else
{
LoadMatroskaSubtitle(subtitleList[0], openFileDialog1.FileName);
}
}
}
MessageBox.Show(_language.NoSubtitlesFound);
}
else
{
MessageBox.Show(string.Format(_language.NotAValidMatroskaFileX, openFileDialog1.FileName));
if (ContinueNewOrExit())
{
if (subtitleList.Count > 1)
{
MatroskaSubtitleChooser subtitleChooser = new MatroskaSubtitleChooser();
subtitleChooser.Initialize(subtitleList);
if (subtitleChooser.ShowDialog(this) == DialogResult.OK)
{
LoadMatroskaSubtitle(subtitleList[subtitleChooser.SelectedIndex], openFileDialog1.FileName);
}
}
else
{
LoadMatroskaSubtitle(subtitleList[0], openFileDialog1.FileName);
}
}
}
}
else
{
MessageBox.Show(string.Format(_language.NotAValidMatroskaFileX, openFileDialog1.FileName));
}
}
private void LoadMatroskaSubtitle(MatroskaSubtitleInfo matroskaSubtitleInfo, string fileName)

View File

@ -684,7 +684,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAD2
CAAAAk1TRnQBSQFMAgEBAgEAAWgBBAFoAQQBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
CAAAAk1TRnQBSQFMAgEBAgEAAYABBAGAAQQBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA

View File

@ -98,6 +98,10 @@ namespace Nikse.SubtitleEdit.Forms
OcrFixEngine _ocrFixEngine;
int _tessnetOcrAutoFixes;
Subtitle _bdnXmlOriginal;
Subtitle _bdnXmlSubtitle;
string _bdnFileName;
public VobSubOcr()
{
InitializeComponent();
@ -300,8 +304,6 @@ namespace Nikse.SubtitleEdit.Forms
Text = Configuration.Settings.Language.VobSubOcr.TitleBluRay;
}
private void LoadImageCompareCharacterDatabaseList()
{
try
@ -486,6 +488,44 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private void LoadBdnXml()
{
_subtitle = new Subtitle();
_bdnXmlSubtitle = new Subtitle();
int max = _bdnXmlOriginal.Paragraphs.Count;
for (int i = 0; i < max; i++)
{
var x = _bdnXmlOriginal.Paragraphs[i];
if ((checkBoxShowOnlyForced.Checked && x.Forced) ||
checkBoxShowOnlyForced.Checked == false)
{
_bdnXmlSubtitle.Paragraphs.Add(new Paragraph(x));
Paragraph p = new Paragraph(x);
p.Text = string.Empty;
_subtitle.Paragraphs.Add(p);
}
}
_subtitle.Renumber(1);
FixShortDisplayTimes(_subtitle);
subtitleListView1.Fill(_subtitle);
subtitleListView1.SelectIndexAndEnsureVisible(0);
numericUpDownStartNumber.Maximum = max;
if (numericUpDownStartNumber.Maximum > 0 && numericUpDownStartNumber.Minimum <= 1)
numericUpDownStartNumber.Value = 1;
buttonOK.Enabled = true;
buttonCancel.Enabled = true;
buttonStartOcr.Enabled = true;
buttonStop.Enabled = false;
buttonNewCharacterDatabase.Enabled = true;
buttonEditCharacterDatabase.Enabled = true;
buttonStartOcr.Focus();
}
private void LoadBluRaySup()
{
_subtitle = new Subtitle();
@ -586,6 +626,17 @@ namespace Nikse.SubtitleEdit.Forms
private Bitmap GetSubtitleBitmap(int index)
{
if (_bdnXmlSubtitle != null)
{
if (index >= 0 && index < _bdnXmlSubtitle.Paragraphs.Count)
{
string fileName = Path.Combine(Path.GetDirectoryName(_bdnFileName), _bdnXmlSubtitle.Paragraphs[index].Text);
if (File.Exists(fileName))
return new Bitmap(fileName);
}
return null;
}
if (_bluRaySubtitlesOriginal != null)
{
if (_bluRaySubtitles[index].Palettes.Count == 0 && _defaultPaletteInfo == null)
@ -620,7 +671,9 @@ namespace Nikse.SubtitleEdit.Forms
private long GetSubtitleStartTimeMilliseconds(int index)
{
if (_bluRaySubtitlesOriginal != null)
if (_bdnXmlSubtitle != null)
return (long)_bdnXmlSubtitle.Paragraphs[index].StartTime.TotalMilliseconds;
else if (_bluRaySubtitlesOriginal != null)
return (_bluRaySubtitles[index].StartTime + 45) / 90;
else
return (long)_vobSubMergedPackist[index].StartTime.TotalMilliseconds;
@ -628,7 +681,9 @@ namespace Nikse.SubtitleEdit.Forms
private long GetSubtitleEndTimeMilliseconds(int index)
{
if (_bluRaySubtitlesOriginal != null)
if (_bdnXmlSubtitle != null)
return (long)_bdnXmlSubtitle.Paragraphs[index].EndTime.TotalMilliseconds;
else if (_bluRaySubtitlesOriginal != null)
return (_bluRaySubtitles[index].EndTime + 45) / 90;
else
return (long)_vobSubMergedPackist[index].EndTime.TotalMilliseconds;
@ -636,7 +691,9 @@ namespace Nikse.SubtitleEdit.Forms
private int GetSubtitleCount()
{
if (_bluRaySubtitlesOriginal != null)
if (_bdnXmlSubtitle != null)
return _bdnXmlSubtitle.Paragraphs.Count;
else if (_bluRaySubtitlesOriginal != null)
return _bluRaySubtitles.Count;
else
return _vobSubMergedPackist.Count;
@ -1061,7 +1118,22 @@ namespace Nikse.SubtitleEdit.Forms
private void FormVobSubOcr_Shown(object sender, EventArgs e)
{
if (_bluRaySubtitlesOriginal != null)
if (_bdnXmlOriginal != null)
{
LoadBdnXml();
bool hasForcedSubtitles = false;
foreach (var x in _bdnXmlOriginal.Paragraphs)
{
if (x.Forced)
{
hasForcedSubtitles = true;
break;
}
}
checkBoxShowOnlyForced.Enabled = hasForcedSubtitles;
checkBoxUseTimeCodesFromIdx.Visible = false;
}
else if (_bluRaySubtitlesOriginal != null)
{
LoadBluRaySup();
bool hasForcedSubtitles = false;
@ -1995,7 +2067,9 @@ namespace Nikse.SubtitleEdit.Forms
{
Subtitle oldSubtitle = new Subtitle(_subtitle);
subtitleListView1.BeginUpdate();
if (_bluRaySubtitlesOriginal != null)
if (_bdnXmlOriginal != null)
LoadBdnXml();
else if (_bluRaySubtitlesOriginal != null)
LoadBluRaySup();
else
LoadVobRip();
@ -2016,7 +2090,7 @@ namespace Nikse.SubtitleEdit.Forms
subtitleListView1.Fill(_subtitle);
subtitleListView1.EndUpdate();
}
private void checkBoxUseTimeCodesFromIdx_CheckedChanged(object sender, EventArgs e)
{
Subtitle oldSubtitle = new Subtitle(_subtitle);
@ -2058,5 +2132,39 @@ namespace Nikse.SubtitleEdit.Forms
_ocrFixEngine.SpellCheckDictionaryName = LanguageString;
}
internal void Initialize(Subtitle bdnSubtitle, VobSubOcrSettings vobSubOcrSettings)
{
_bdnXmlOriginal = bdnSubtitle;
_bdnFileName = bdnSubtitle.FileName;
buttonOK.Enabled = false;
buttonCancel.Enabled = false;
buttonStartOcr.Enabled = false;
buttonStop.Enabled = false;
buttonNewCharacterDatabase.Enabled = false;
buttonEditCharacterDatabase.Enabled = false;
labelStatus.Text = string.Empty;
progressBar1.Visible = false;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
numericUpDownPixelsIsSpace.Value = 11; // vobSubOcrSettings.XOrMorePixelsMakesSpace;
_vobSubOcrSettings = vobSubOcrSettings;
InitializeModi();
InitializeTesseract();
LoadImageCompareCharacterDatabaseList();
if (Configuration.Settings.VobSubOcr.LastOcrMethod == "BitmapCompare" && comboBoxOcrMethod.Items.Count > 1)
comboBoxOcrMethod.SelectedIndex = 1;
else if (Configuration.Settings.VobSubOcr.LastOcrMethod == "MODI" && comboBoxOcrMethod.Items.Count > 2)
comboBoxOcrMethod.SelectedIndex = 2;
else
comboBoxOcrMethod.SelectedIndex = 0;
groupBoxImagePalette.Visible = false;
Text = Configuration.Settings.Language.VobSubOcr.TitleBluRay;
Text += " - " + Path.GetFileName(_bdnFileName);
}
}
}

View File

@ -26,6 +26,8 @@ namespace Nikse.SubtitleEdit.Logic
public int EndFrame { get; set; }
public bool Forced { get; set; }
public Paragraph()
{
StartTime = new TimeCode(TimeSpan.FromSeconds(0));
@ -48,6 +50,7 @@ namespace Nikse.SubtitleEdit.Logic
EndTime = new TimeCode(paragraph.EndTime.TimeSpan);
StartFrame = paragraph.StartFrame;
EndFrame = paragraph.EndFrame;
Forced = paragraph.Forced;
}
public Paragraph(int startFrame, int endFrame, string text)

View File

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class BdnXml : SubtitleFormat
{
public override string Extension
{
get { return ".xml"; }
}
public override string Name
{
get { return "BDN Xml"; }
}
public override bool HasLineNumber
{
get { return false; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
Subtitle subtitle = new Subtitle();
this.LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > 0;
}
public override string ToText(Subtitle subtitle, string title)
{
string xmlStructure =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + Environment.NewLine +
"<Subtitle/>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlStructure);
foreach (Paragraph p in subtitle.Paragraphs)
{
XmlNode paragraph = xml.CreateElement("Paragraph");
XmlNode number = xml.CreateElement("Number");
number.InnerText = p.Number.ToString();
paragraph.AppendChild(number);
XmlNode start = xml.CreateElement("StartMilliseconds");
start.InnerText = p.StartTime.TotalMilliseconds.ToString();
paragraph.AppendChild(start);
XmlNode end = xml.CreateElement("EndMilliseconds");
end.InnerText = p.EndTime.TotalMilliseconds.ToString();
paragraph.AppendChild(end);
XmlNode text = xml.CreateElement("Text");
text.InnerText = Utilities.RemoveHtmlTags(p.Text);
paragraph.AppendChild(text);
xml.DocumentElement.AppendChild(paragraph);
}
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xml.Save(writer);
return Encoding.UTF8.GetString(ms.ToArray()).Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
StringBuilder sb = new StringBuilder();
lines.ForEach(line => sb.AppendLine(line));
string xmlString = sb.ToString();
if (!xmlString.Contains("<BDN"))
return;
XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(xmlString);
}
catch
{
_errorCount = 1;
return;
}
foreach (XmlNode node in xml.DocumentElement.SelectNodes("Events/Event"))
{
try
{
string start = node.Attributes["InTC"].InnerText;
string end = node.Attributes["OutTC"].InnerText;
string text = node.SelectSingleNode("Graphic").InnerText;
Paragraph p = new Paragraph(text, GetMillisecondsFromTimeCode(start), GetMillisecondsFromTimeCode(end));
if (node.Attributes["Forced"] != null && node.Attributes["Forced"].InnerText.ToLower() == "true")
p.Forced = true;
subtitle.Paragraphs.Add(p);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
_errorCount++;
}
}
subtitle.Renumber(1);
}
private double GetMillisecondsFromTimeCode(string time)
{
string[] arr = time.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
return new TimeSpan(0, int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]), int.Parse(arr[3])).TotalMilliseconds;
}
}
}

View File

@ -534,6 +534,7 @@
<Compile Include="Logic\SubtitleFormats\AdobeEncore.cs" />
<Compile Include="Logic\SubtitleFormats\AdobeEncoreTabs.cs" />
<Compile Include="Logic\SubtitleFormats\AdobeEncoreWithLineNumbers.cs" />
<Compile Include="Logic\SubtitleFormats\BdnXml.cs" />
<Compile Include="Logic\SubtitleFormats\Cavena890.cs" />
<Compile Include="Logic\SubtitleFormats\DCSubtitle.cs" />
<Compile Include="Logic\SubtitleFormats\FinalCutProTextXml.cs" />

View File

@ -33,19 +33,11 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AxInterop.WMPLib">
<HintPath>..\obj\Release\AxInterop.WMPLib.dll</HintPath>
</Reference>
<Reference Include="Interop.QuartzTypeLib">
<HintPath>..\DLLs\Interop.QuartzTypeLib.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
<Private>True</Private>
</Reference>
<Reference Include="Interop.WMPLib">
<HintPath>..\obj\Release\Interop.WMPLib.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="NHunspell">
<HintPath>..\DLLs\NHunspell.dll</HintPath>