From 89b4e1fcfa47b043bf445104b159f9889cf43db0 Mon Sep 17 00:00:00 2001 From: niksedk Date: Mon, 25 Apr 2011 12:41:18 +0000 Subject: [PATCH] Can now import+ocr bdn-xml w images git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@411 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Forms/Main.cs | 97 +++++++++++++++------ src/Forms/Main.resx | 2 +- src/Forms/VobSubOcr.cs | 124 ++++++++++++++++++++++++-- src/Logic/Paragraph.cs | 3 + src/Logic/SubtitleFormats/BdnXml.cs | 129 ++++++++++++++++++++++++++++ src/SubtitleEdit.csproj | 1 + src/Test/Test.csproj | 8 -- 7 files changed, 319 insertions(+), 45 deletions(-) create mode 100644 src/Logic/SubtitleFormats/BdnXml.cs diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 36147b758..f8974a852 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -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 list = new List(); + 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 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) diff --git a/src/Forms/Main.resx b/src/Forms/Main.resx index 3ec2c15db..3a2ade696 100644 --- a/src/Forms/Main.resx +++ b/src/Forms/Main.resx @@ -684,7 +684,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAD2 - CAAAAk1TRnQBSQFMAgEBAgEAAWgBBAFoAQQBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CAAAAk1TRnQBSQFMAgEBAgEAAYABBAGAAQQBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA diff --git a/src/Forms/VobSubOcr.cs b/src/Forms/VobSubOcr.cs index 788a72380..867ce8867 100644 --- a/src/Forms/VobSubOcr.cs +++ b/src/Forms/VobSubOcr.cs @@ -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); + } } } diff --git a/src/Logic/Paragraph.cs b/src/Logic/Paragraph.cs index 31f10d1e5..8dea4d172 100644 --- a/src/Logic/Paragraph.cs +++ b/src/Logic/Paragraph.cs @@ -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) diff --git a/src/Logic/SubtitleFormats/BdnXml.cs b/src/Logic/SubtitleFormats/BdnXml.cs new file mode 100644 index 000000000..d8fbd81b6 --- /dev/null +++ b/src/Logic/SubtitleFormats/BdnXml.cs @@ -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 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 = + "" + Environment.NewLine + + ""; + + 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 lines, string fileName) + { + _errorCount = 0; + + StringBuilder sb = new StringBuilder(); + lines.ForEach(line => sb.AppendLine(line)); + + string xmlString = sb.ToString(); + if (!xmlString.Contains(" + diff --git a/src/Test/Test.csproj b/src/Test/Test.csproj index 9dd8e73a9..6a9430042 100644 --- a/src/Test/Test.csproj +++ b/src/Test/Test.csproj @@ -33,19 +33,11 @@ 4 - - ..\obj\Release\AxInterop.WMPLib.dll - ..\DLLs\Interop.QuartzTypeLib.dll False True - - ..\obj\Release\Interop.WMPLib.dll - False - True - ..\DLLs\NHunspell.dll