private bool ImportSubtitleFromTransportStream(string fileName) { ShowStatus(_language.ParsingTransportStream); Refresh(); var tsParser = new TransportStreamParser(); tsParser.Parse(fileName, (pos, total) => UpdateProgress(pos, total, _language.ParsingTransportStreamFile)); ShowStatus(string.Empty); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); if (tsParser.SubtitlePacketIds.Count == 0) { MessageBox.Show(_language.NoSubtitlesFound); return false; } int packedId = tsParser.SubtitlePacketIds[0]; if (tsParser.SubtitlePacketIds.Count > 1) { using (var subChooser = new TransportStreamSubtitleChooser()) { subChooser.Initialize(tsParser, fileName); if (subChooser.ShowDialog(this) == DialogResult.Cancel) return false; packedId = tsParser.SubtitlePacketIds[subChooser.SelectedIndex]; } } var subtitles = tsParser.GetDvbSubtitles(packedId); using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); return true; } return false; } }
private bool LoadBluRaySubFromMatroska(MatroskaTrackInfo matroskaSubtitleInfo, MatroskaFile matroska) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show(_language.NoSupportEncryptedVobSub); } ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; var sub = matroska.GetSubtitle(matroskaSubtitleInfo.TrackNumber, MatroskaProgress); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); Cursor.Current = Cursors.Default; int noOfErrors = 0; string lastError = string.Empty; MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); var subtitles = new List<BluRaySupParser.PcsData>(); var log = new StringBuilder(); foreach (var p in sub) { byte[] buffer = null; if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { var outStream = new MemoryStream(); var outZStream = new zlib.ZOutputStream(outStream); var inStream = new MemoryStream(p.Data); try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { var tc = new TimeCode(p.Start); lastError = tc + ": " + exception.Message + ": " + exception.StackTrace; noOfErrors++; } finally { outZStream.Close(); inStream.Close(); } } else { buffer = p.Data; } if (buffer != null && buffer.Length > 100) { var ms = new MemoryStream(buffer); var list = BluRaySupParser.ParseBluRaySup(ms, log, true); foreach (var sup in list) { sup.StartTime = (long)((p.Start - 1) * 90.0); sup.EndTime = (long)((p.End - 1) * 90.0); subtitles.Add(sup); // fix overlapping if (subtitles.Count > 1 && sub[subtitles.Count - 2].End > sub[subtitles.Count - 1].Start) subtitles[subtitles.Count - 2].EndTime = subtitles[subtitles.Count - 1].StartTime - 1; } ms.Close(); } else if (subtitles.Count > 0) { var lastSub = subtitles[subtitles.Count - 1]; if (lastSub.StartTime == lastSub.EndTime) { lastSub.EndTime = (long)((p.Start - 1) * 90.0); if (lastSub.EndTime - lastSub.StartTime > 1000000) lastSub.EndTime = lastSub.StartTime; } } } if (noOfErrors > 0) { MessageBox.Show(string.Format("{0} error(s) occurred during extraction of bdsup\r\n\r\n{1}", noOfErrors, lastError)); } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, matroska.Path); if (_loading) { formSubOcr.Icon = (Icon)Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); return true; } } return false; }
private void ImportSubtitleFromDvbSupFile(string fileName) { using (var formSubOcr = new VobSubOcr()) { var subtitles = TransportStreamParser.GetDvbSup(fileName); formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); } } }
private void ImportAndOcrSrt(Subtitle subtitle) { using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitle, Configuration.Settings.VobSubOcr, false); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingBdnXml); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(formSubOcr.FileName, ".srt"); SetTitle(); _converted = true; } } }
private bool LoadVobSubFromMatroska(MatroskaTrackInfo matroskaSubtitleInfo, MatroskaFile matroska) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show(_language.NoSupportEncryptedVobSub); } ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; var sub = matroska.GetSubtitle(matroskaSubtitleInfo.TrackNumber, MatroskaProgress); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); Cursor.Current = Cursors.Default; MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); List<VobSubMergedPack> mergedVobSubPacks = new List<VobSubMergedPack>(); var idx = new Core.VobSub.Idx(matroskaSubtitleInfo.CodecPrivate.SplitToLines()); foreach (var p in sub) { if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { bool error = false; var outStream = new MemoryStream(); var outZStream = new zlib.ZOutputStream(outStream); var inStream = new MemoryStream(p.Data); byte[] buffer = null; try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { MessageBox.Show(exception.Message + Environment.NewLine + Environment.NewLine + exception.StackTrace); error = true; } finally { outZStream.Close(); inStream.Close(); } if (!error && buffer.Length > 2) mergedVobSubPacks.Add(new VobSubMergedPack(buffer, TimeSpan.FromMilliseconds(p.Start), 32, null)); } else { mergedVobSubPacks.Add(new VobSubMergedPack(p.Data, TimeSpan.FromMilliseconds(p.Start), 32, null)); } if (mergedVobSubPacks.Count > 0) mergedVobSubPacks[mergedVobSubPacks.Count - 1].EndTime = TimeSpan.FromMilliseconds(p.End); // fix overlapping (some versions of Handbrake makes overlapping time codes - thx Hawke) if (mergedVobSubPacks.Count > 1 && mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime > mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime) mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime = TimeSpan.FromMilliseconds(mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime.TotalMilliseconds - 1); } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(mergedVobSubPacks, idx.Palette, Configuration.Settings.VobSubOcr, null); // TODO: language??? if (_loading) { formSubOcr.Icon = (Icon)Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { ResetSubtitle(); _subtitle.Paragraphs.Clear(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(matroska.Path); _converted = true; Text = Title; Configuration.Settings.Save(); return true; } } return false; }
private void ImportAndOcrSon(string fileName, Son format, List<string> list) { using (var formSubOcr = new VobSubOcr()) { var sub = new Subtitle(); format.LoadSubtitle(sub, list, fileName); sub.FileName = fileName; formSubOcr.Initialize(sub, Configuration.Settings.VobSubOcr, true); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { this.MakeHistoryForUndo(this._language.BeforeImportingBdnXml); this.FileNew(); this._subtitle.Paragraphs.Clear(); this.SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); this._subtitle.WasLoadedWithFrameNumbers = false; this._subtitle.CalculateFrameNumbersFromTimeCodes(this.CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { this._subtitle.Paragraphs.Add(p); } this.ShowSource(); this.SubtitleListview1.Fill(this._subtitle, this._subtitleAlternate); this._subtitleListViewIndex = -1; this.SubtitleListview1.FirstVisibleIndex = -1; this.SubtitleListview1.SelectIndexAndEnsureVisible(0); this._fileName = Path.ChangeExtension(formSubOcr.FileName, ".srt"); this.SetTitle(); this._converted = true; } } }
private void ImportAndOcrBluRaySup(string fileName, bool showInTaskbar) { var log = new StringBuilder(); var subtitles = BluRaySupParser.ParseBluRaySup(fileName, log); if (subtitles.Count == 0) { string msg = _language.BlurayNotSubtitlesFound + Environment.NewLine + Environment.NewLine + log.ToString(); if (msg.Length > 800) msg = msg.Substring(0, 800); MessageBox.Show(msg.Trim() + "..."); return; } using (var vobSubOcr = new VobSubOcr()) { if (showInTaskbar) { vobSubOcr.Icon = (Icon)Icon.Clone(); vobSubOcr.ShowInTaskbar = true; vobSubOcr.ShowIcon = true; } vobSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); vobSubOcr.FileName = Path.GetFileName(fileName); if (vobSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingBluRaySupFile); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in vobSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(vobSubOcr.FileName, ".srt"); SetTitle(); _converted = true; Configuration.Settings.Save(); } } }
private void ImportAndOcrSpDvdSup(string fileName, bool showInTaskbar) { var spList = new List<SpHeader>(); using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var buffer = new byte[SpHeader.SpHeaderLength]; int bytesRead = fs.Read(buffer, 0, buffer.Length); var header = new SpHeader(buffer); while (header.Identifier == "SP" && bytesRead > 0 && header.NextBlockPosition > 4) { buffer = new byte[header.NextBlockPosition]; bytesRead = fs.Read(buffer, 0, buffer.Length); if (bytesRead == buffer.Length) { header.AddPicture(buffer); spList.Add(header); } buffer = new byte[SpHeader.SpHeaderLength]; bytesRead = fs.Read(buffer, 0, buffer.Length); header = new SpHeader(buffer); } } using (var vobSubOcr = new VobSubOcr()) { if (showInTaskbar) { vobSubOcr.Icon = (Icon)this.Icon.Clone(); vobSubOcr.ShowInTaskbar = true; vobSubOcr.ShowIcon = true; } vobSubOcr.Initialize(fileName, null, Configuration.Settings.VobSubOcr, spList); if (vobSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingVobSubFile); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in vobSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(vobSubOcr.FileName, ".srt"); SetTitle(); _converted = true; Configuration.Settings.Save(); } } }
private void ImportAndOcrVobSubSubtitleNew(string fileName, bool showInTaskbar) { if (!IsVobSubFile(fileName, true)) { return; } using (var vobSubOcr = new VobSubOcr()) { if (showInTaskbar) { vobSubOcr.Icon = (Icon)this.Icon.Clone(); vobSubOcr.ShowInTaskbar = true; vobSubOcr.ShowIcon = true; } if (vobSubOcr.Initialize(fileName, Configuration.Settings.VobSubOcr, this) && vobSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingVobSubFile); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in vobSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(vobSubOcr.FileName, ".srt"); SetTitle(); _converted = true; Configuration.Settings.Save(); } } }
private void LoadBluRaySubFromMatroska(MatroskaSubtitleInfo matroskaSubtitleInfo, string fileName) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show("Encrypted vobsub content not supported"); } bool isValid; var matroska = new Matroska(); ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; List<SubtitleSequence> sub = matroska.GetMatroskaSubtitle(fileName, (int)matroskaSubtitleInfo.TrackNumber, out isValid, MatroskaProgress); Cursor.Current = Cursors.Default; int noOfErrors = 0; string lastError = string.Empty; if (isValid) { MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); var subtitles = new List<Nikse.SubtitleEdit.Logic.BluRaySup.BluRaySupParser.PcsData>(); StringBuilder log = new StringBuilder(); foreach (SubtitleSequence p in sub) { byte[] buffer = null; if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { MemoryStream outStream = new MemoryStream(); ComponentAce.Compression.Libs.zlib.ZOutputStream outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream); MemoryStream inStream = new MemoryStream(p.BinaryData); try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { TimeCode tc = new TimeCode(TimeSpan.FromMilliseconds(p.StartMilliseconds)); lastError = tc.ToString() + ": " + exception.Message + ": " + exception.StackTrace; noOfErrors++; } finally { outStream.Close(); outZStream.Close(); inStream.Close(); } } else { buffer = p.BinaryData; } if (buffer != null && buffer.Length > 100) { MemoryStream ms = new MemoryStream(buffer); var list = BluRaySupParser.ParseBluRaySup(ms, log, true); foreach (var sup in list) { sup.StartTime = (long)((p.StartMilliseconds - 45) * 90.0); sup.EndTime = (long)((p.EndMilliseconds - 45) * 90.0); subtitles.Add(sup); // fix overlapping if (subtitles.Count > 1 && sub[subtitles.Count - 2].EndMilliseconds > sub[subtitles.Count - 1].StartMilliseconds) subtitles[subtitles.Count - 2].EndTime = subtitles[subtitles.Count - 1].StartTime - 1; } ms.Close(); } } if (noOfErrors > 0) { MessageBox.Show(string.Format("{0} errror(s) occured during extraction of bdsup\r\n\r\n{1}", noOfErrors, lastError)); } var formSubOcr = new VobSubOcr(); _formPositionsAndSizes.SetPositionAndSize(formSubOcr); formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); if (_loading) { formSubOcr.Icon = (Icon)this.Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); } _formPositionsAndSizes.SavePositionAndSize(formSubOcr); } }
private bool ImportSubtitleFromTransportStream(string fileName) { if (string.IsNullOrEmpty(_language.ParsingTransportStream)) ShowStatus("Parsing transport stream - please wait..."); else ShowStatus(_language.ParsingTransportStream); Refresh(); var tsParser = new Nikse.SubtitleEdit.Logic.TransportStream.TransportStreamParser(); tsParser.ParseTsFile(fileName); ShowStatus(string.Empty); if (tsParser.SubtitlePacketIds.Count == 0) { MessageBox.Show(_language.NoSubtitlesFound); return false; } int packedId = tsParser.SubtitlePacketIds[0]; if (tsParser.SubtitlePacketIds.Count > 1) { var subChooser = new TransportStreamSubtitleChooser(); _formPositionsAndSizes.SetPositionAndSize(subChooser); subChooser.Initialize(tsParser, fileName); if (subChooser.ShowDialog(this) == DialogResult.Cancel) return false; packedId = tsParser.SubtitlePacketIds[subChooser.SelectedIndex]; _formPositionsAndSizes.SavePositionAndSize(subChooser); } var subtitles = tsParser.GetDvbSubtitles(packedId); var formSubOcr = new VobSubOcr(); _formPositionsAndSizes.SetPositionAndSize(formSubOcr); formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); _formPositionsAndSizes.SavePositionAndSize(formSubOcr); return true; } _formPositionsAndSizes.SavePositionAndSize(formSubOcr); return false; }
private void LoadVobSubFromMatroska(MatroskaSubtitleInfo matroskaSubtitleInfo, string fileName) { if (matroskaSubtitleInfo.ContentEncodingType == 1) { MessageBox.Show("Encrypted vobsub content not supported"); } bool isValid; var matroska = new Matroska(); ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; List<SubtitleSequence> sub = matroska.GetMatroskaSubtitle(fileName, (int)matroskaSubtitleInfo.TrackNumber, out isValid, MatroskaProgress); Cursor.Current = Cursors.Default; if (isValid) { MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; _subtitle.Paragraphs.Clear(); List<VobSubMergedPack> mergedVobSubPacks = new List<VobSubMergedPack>(); Nikse.SubtitleEdit.Logic.VobSub.Idx idx = new Logic.VobSub.Idx(matroskaSubtitleInfo.CodecPrivate.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); foreach (SubtitleSequence p in sub) { if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib { bool error = false; MemoryStream outStream = new MemoryStream(); ComponentAce.Compression.Libs.zlib.ZOutputStream outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream); MemoryStream inStream = new MemoryStream(p.BinaryData); byte[] buffer = null; try { CopyStream(inStream, outZStream); buffer = new byte[outZStream.TotalOut]; outStream.Position = 0; outStream.Read(buffer, 0, buffer.Length); } catch (Exception exception) { MessageBox.Show(exception.Message + Environment.NewLine + Environment.NewLine + exception.StackTrace); error = true; } finally { outStream.Close(); outZStream.Close(); inStream.Close(); } if (!error) mergedVobSubPacks.Add(new VobSubMergedPack(buffer, TimeSpan.FromMilliseconds(p.StartMilliseconds), 32, null)); } else { mergedVobSubPacks.Add(new VobSubMergedPack(p.BinaryData, TimeSpan.FromMilliseconds(p.StartMilliseconds), 32, null)); } mergedVobSubPacks[mergedVobSubPacks.Count - 1].EndTime = TimeSpan.FromMilliseconds(p.EndMilliseconds); // fix overlapping (some versions of Handbrake makes overlapping time codes - thx Hawke) if (mergedVobSubPacks.Count > 1 && mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime > mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime) mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime = TimeSpan.FromMilliseconds(mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime.TotalMilliseconds - 1); } var formSubOcr = new VobSubOcr(); _formPositionsAndSizes.SetPositionAndSize(formSubOcr); formSubOcr.Initialize(mergedVobSubPacks, idx.Palette, Configuration.Settings.VobSubOcr, null); //TODO - language??? if (_loading) { formSubOcr.Icon = (Icon)this.Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { ResetSubtitle(); _subtitle.Paragraphs.Clear(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(fileName); _converted = true; Text = Title; Configuration.Settings.Save(); } _formPositionsAndSizes.SavePositionAndSize(formSubOcr); } }
private void ImportAndOcrSst(string fileName, SonicScenaristBitmaps format, List<string> list) { Subtitle sub = new Subtitle(); format.LoadSubtitle(sub, list, fileName); sub.FileName = fileName; var formSubOcr = new VobSubOcr(); _formPositionsAndSizes.SetPositionAndSize(formSubOcr); formSubOcr.Initialize(sub, Configuration.Settings.VobSubOcr, true); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingBdnXml); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(formSubOcr.FileName, ".srt"); SetTitle(); _converted = true; } _formPositionsAndSizes.SavePositionAndSize(formSubOcr); }
private void ImportAndOcrBluRaySup(string fileName, bool showInTaskbar) { var log = new StringBuilder(); var subtitles = BluRaySupParser.ParseBluRaySup(fileName, log); if (subtitles.Count > 0) { var vobSubOcr = new VobSubOcr(); if (showInTaskbar) { vobSubOcr.Icon = (Icon)this.Icon.Clone(); vobSubOcr.ShowInTaskbar = true; vobSubOcr.ShowIcon = true; } _formPositionsAndSizes.SetPositionAndSize(vobSubOcr); vobSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName); vobSubOcr.FileName = Path.GetFileName(fileName); if (vobSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingBluRaySupFile); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (Paragraph p in vobSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.ChangeExtension(vobSubOcr.FileName, ".srt"); SetTitle(); _converted = true; Configuration.Settings.Save(); } _formPositionsAndSizes.SavePositionAndSize(vobSubOcr); } }
private bool ImportSubtitleFromDivX(string fileName) { var count = 0; var list = new List<XSub>(); using (var f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var searchBuffer = new byte[2048]; long pos = 0; long length = f.Length - 50; while (pos < length) { f.Position = pos; int readCount = f.Read(searchBuffer, 0, searchBuffer.Length); for (int i = 0; i < readCount; i++) { if (searchBuffer[i] != 0x5b || (i + 4 < readCount && (searchBuffer[i + 1] < 0x30 || searchBuffer[i + 1] > 0x39 || searchBuffer[i + 3] != 0x3a))) { continue; } f.Position = pos + i + 1; var buffer = new byte[26]; f.Read(buffer, 0, buffer.Length); if (buffer[2] == 0x3a && // : buffer[5] == 0x3a && // : buffer[8] == 0x2e && // . buffer[12] == 0x2d && // - buffer[15] == 0x3a && // : buffer[18] == 0x3a && // : buffer[21] == 0x2e && // . buffer[25] == 0x5d) // ] { // subtitle time code string timeCode = Encoding.ASCII.GetString(buffer, 0, 25); f.Read(buffer, 0, 2); int width = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int height = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int x = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int y = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int xEnd = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int yEnd = BitConverter.ToUInt16(buffer, 0); f.Read(buffer, 0, 2); int RleLength = BitConverter.ToUInt16(buffer, 0); var colorBuffer = new byte[4 * 3]; // four colors with rgb (3 bytes) f.Read(colorBuffer, 0, colorBuffer.Length); buffer = new byte[RleLength]; int bytesRead = f.Read(buffer, 0, buffer.Length); if (width > 0 && height > 0 && bytesRead == buffer.Length) { var xSub = new XSub(timeCode, width, height, colorBuffer, buffer); list.Add(xSub); count++; } } } pos += searchBuffer.Length; } } if (count == 0) { return false; } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(list, Configuration.Settings.VobSubOcr, fileName); // TODO: language??? if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; FileNew(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(fileName); _converted = true; Text = Title; Configuration.Settings.Save(); OpenVideo(fileName); } } return true; }
private void ToolStripMenuItemImportDvdSubtitlesClick(object sender, EventArgs e) { if (!ContinueNewOrExit()) { return; } using (var formSubRip = new DvdSubRip(Handle)) { if (formSubRip.ShowDialog(this) == DialogResult.OK) { using (var showSubtitles = new DvdSubRipChooseLanguage()) { showSubtitles.Initialize(formSubRip.MergedVobSubPacks, formSubRip.Palette, formSubRip.Languages, formSubRip.SelectedLanguage); if (formSubRip.Languages.Count == 1 || showSubtitles.ShowDialog(this) == DialogResult.OK) { using (var formSubOcr = new VobSubOcr()) { var subs = formSubRip.MergedVobSubPacks; if (showSubtitles.SelectedVobSubMergedPacks != null) subs = showSubtitles.SelectedVobSubMergedPacks; formSubOcr.Initialize(subs, formSubRip.Palette, Configuration.Settings.VobSubOcr, formSubRip.SelectedLanguage); if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle); FileNew(); _subtitle.Paragraphs.Clear(); SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); _subtitle.WasLoadedWithFrameNumbers = false; _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate); foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) { _subtitle.Paragraphs.Add(p); } ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0, true); _fileName = string.Empty; Text = Title; Configuration.Settings.Save(); } } } } } } }
private void LoadMp4Subtitle(string fileName, Trak mp4SubtitleTrack) { if (mp4SubtitleTrack.Mdia.IsVobSubSubtitle) { var subPicturesWithTimeCodes = new List<VobSubOcr.SubPicturesWithSeparateTimeCodes>(); for (int i = 0; i < mp4SubtitleTrack.Mdia.Minf.Stbl.EndTimeCodes.Count; i++) { if (mp4SubtitleTrack.Mdia.Minf.Stbl.SubPictures.Count > i) { var start = TimeSpan.FromSeconds(mp4SubtitleTrack.Mdia.Minf.Stbl.StartTimeCodes[i]); var end = TimeSpan.FromSeconds(mp4SubtitleTrack.Mdia.Minf.Stbl.EndTimeCodes[i]); subPicturesWithTimeCodes.Add(new VobSubOcr.SubPicturesWithSeparateTimeCodes(mp4SubtitleTrack.Mdia.Minf.Stbl.SubPictures[i], start, end)); } } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subPicturesWithTimeCodes, Configuration.Settings.VobSubOcr, fileName); // TODO: language??? if (formSubOcr.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; FileNew(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(fileName); _converted = true; Text = Title; Configuration.Settings.Save(); } } } else { MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; FileNew(); for (int i = 0; i < mp4SubtitleTrack.Mdia.Minf.Stbl.EndTimeCodes.Count; i++) { if (mp4SubtitleTrack.Mdia.Minf.Stbl.Texts.Count > i) { var start = TimeSpan.FromSeconds(mp4SubtitleTrack.Mdia.Minf.Stbl.StartTimeCodes[i]); var end = TimeSpan.FromSeconds(mp4SubtitleTrack.Mdia.Minf.Stbl.EndTimeCodes[i]); string text = mp4SubtitleTrack.Mdia.Minf.Stbl.Texts[i]; var p = new Paragraph(text, start.TotalMilliseconds, end.TotalMilliseconds); if (p.EndTime.TotalMilliseconds - p.StartTime.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds) p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds; if (mp4SubtitleTrack.Mdia.IsClosedCaption && string.IsNullOrEmpty(text)) { // do not add empty lines } else { _subtitle.Paragraphs.Add(p); } } } SetEncoding(Encoding.UTF8); ShowStatus(_language.SubtitleImportedFromMatroskaFile); _subtitle.Renumber(); _subtitle.WasLoadedWithFrameNumbers = false; if (fileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".m4v", StringComparison.OrdinalIgnoreCase)) { _fileName = fileName.Substring(0, fileName.Length - 4); Text = Title + " - " + _fileName; } else { Text = Title; } _fileDateTime = new DateTime(); _converted = true; SubtitleListview1.Fill(_subtitle, _subtitleAlternate); if (_subtitle.Paragraphs.Count > 0) SubtitleListview1.SelectIndexAndEnsureVisible(0); ShowSource(); } }
private bool LoadDvbFromMatroska(MatroskaTrackInfo matroskaSubtitleInfo, MatroskaFile matroska, bool batchMode) { ShowStatus(_language.ParsingMatroskaFile); Refresh(); Cursor.Current = Cursors.WaitCursor; var sub = matroska.GetSubtitle(matroskaSubtitleInfo.TrackNumber, MatroskaProgress); TaskbarList.SetProgressState(Handle, TaskbarButtonProgressFlags.NoProgress); Cursor.Current = Cursors.Default; MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile); _subtitleListViewIndex = -1; if (!batchMode) ResetSubtitle(); _subtitle.Paragraphs.Clear(); var subtitleImages = new List<DvbSubPes>(); var subtitle = new Subtitle(); Utilities.LoadMatroskaTextSubtitle(matroskaSubtitleInfo, matroska, sub, _subtitle); for (int index = 0; index < sub.Count; index++) { try { var msub = sub[index]; DvbSubPes pes = null; if (msub.Data.Length > 9 && msub.Data[0] == 15 && msub.Data[1] >= SubtitleSegment.PageCompositionSegment && msub.Data[1] <= SubtitleSegment.DisplayDefinitionSegment) // sync byte + segment id { var buffer = new byte[msub.Data.Length + 3]; Buffer.BlockCopy(msub.Data, 0, buffer, 2, msub.Data.Length); buffer[0] = 32; buffer[1] = 0; buffer[buffer.Length - 1] = 255; pes = new DvbSubPes(0, buffer); } else if (VobSubParser.IsMpeg2PackHeader(msub.Data)) { pes = new DvbSubPes(msub.Data, Mpeg2Header.Length); } else if (VobSubParser.IsPrivateStream1(msub.Data, 0)) { pes = new DvbSubPes(msub.Data, 0); } else if (msub.Data.Length > 9 && msub.Data[0] == 32 && msub.Data[1] == 0 && msub.Data[2] == 14 && msub.Data[3] == 16) { pes = new DvbSubPes(0, msub.Data); } if (pes == null && subtitle.Paragraphs.Count > 0) { var last = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1]; if (last.Duration.TotalMilliseconds < 100) { last.EndTime.TotalMilliseconds = msub.Start; if (last.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds) { last.EndTime.TotalMilliseconds = last.StartTime.TotalMilliseconds + 3000; } } } if (pes != null && pes.PageCompositions != null && pes.PageCompositions.Any(p => p.Regions.Count > 0)) { subtitleImages.Add(pes); subtitle.Paragraphs.Add(new Paragraph(string.Empty, msub.Start, msub.End)); } } catch { // continue } } if (subtitleImages.Count == 0) { return false; } for (int index = 0; index < subtitle.Paragraphs.Count; index++) { var p = subtitle.Paragraphs[index]; if (p.Duration.TotalMilliseconds < 200) { p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 3000; } var next = subtitle.GetParagraphOrDefault(index + 1); if (next != null && next.StartTime.TotalMilliseconds < p.EndTime.TotalMilliseconds) { p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; } } using (var formSubOcr = new VobSubOcr()) { formSubOcr.Initialize(subtitle, subtitleImages, Configuration.Settings.VobSubOcr, null); // TODO: language??? if (_loading) { formSubOcr.Icon = (Icon)Icon.Clone(); formSubOcr.ShowInTaskbar = true; formSubOcr.ShowIcon = true; } if (formSubOcr.ShowDialog(this) == DialogResult.OK) { ResetSubtitle(); _subtitle.Paragraphs.Clear(); _subtitle.WasLoadedWithFrameNumbers = false; foreach (var p in formSubOcr.SubtitleFromOcr.Paragraphs) _subtitle.Paragraphs.Add(p); ShowSource(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); _subtitleListViewIndex = -1; SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0); _fileName = Path.GetFileNameWithoutExtension(matroska.Path); _converted = true; Text = Title; Configuration.Settings.Save(); return true; } } return false; }