Exemplo n.º 1
0
        public void OpenSubIdx(string vobSubFileName, string idxFileName)
        {
            VobSubPacks = new List <VobSubPack>();

            if (!string.IsNullOrEmpty(idxFileName) && File.Exists(idxFileName))
            {
                var idx = new Idx(idxFileName);
                IdxPalette   = idx.Palette;
                IdxLanguages = idx.Languages;
                if (idx.IdxParagraphs.Count > 0)
                {
                    var buffer = new byte[0x800]; // 2048
                    using (var fs = new FileStream(vobSubFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        foreach (var p in idx.IdxParagraphs)
                        {
                            if (p.FilePosition + 100 < fs.Length)
                            {
                                long position = p.FilePosition;
                                fs.Seek(position, SeekOrigin.Begin);
                                fs.Read(buffer, 0, 0x0800);
                                if (IsSubtitlePack(buffer) || IsPrivateStream1(buffer, 0))
                                {
                                    var vsp = new VobSubPack(buffer, p);
                                    VobSubPacks.Add(vsp);

                                    if (IsPrivateStream1(buffer, 0))
                                    {
                                        position += vsp.PacketizedElementaryStream.Length + 6;
                                    }
                                    else
                                    {
                                        position += 0x800;
                                    }

                                    int currentSubPictureStreamId = 0;
                                    if (vsp.PacketizedElementaryStream.SubPictureStreamId != null)
                                    {
                                        currentSubPictureStreamId = vsp.PacketizedElementaryStream.SubPictureStreamId.Value;
                                    }

                                    while (vsp.PacketizedElementaryStream != null &&
                                           vsp.PacketizedElementaryStream.SubPictureStreamId.HasValue &&
                                           (vsp.PacketizedElementaryStream.Length == PacketizedElementaryStreamMaximumLength ||
                                            currentSubPictureStreamId != vsp.PacketizedElementaryStream.SubPictureStreamId.Value) && position < fs.Length)
                                    {
                                        fs.Seek(position, SeekOrigin.Begin);
                                        fs.Read(buffer, 0, 0x800);
                                        vsp = new VobSubPack(buffer, p); // idx position?

                                        if (vsp.PacketizedElementaryStream != null && vsp.PacketizedElementaryStream.SubPictureStreamId.HasValue && currentSubPictureStreamId == vsp.PacketizedElementaryStream.SubPictureStreamId.Value)
                                        {
                                            VobSubPacks.Add(vsp);

                                            if (IsPrivateStream1(buffer, 0))
                                            {
                                                position += vsp.PacketizedElementaryStream.Length + 6;
                                            }
                                            else
                                            {
                                                position += 0x800;
                                            }
                                        }
                                        else
                                        {
                                            position += 0x800;
                                            fs.Seek(position, SeekOrigin.Begin);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    return;
                }
            }

            // No valid idx file found - just open like vob file
            Open(vobSubFileName);
        }
Exemplo n.º 2
0
        public void OpenSubIdx(string vobSubFileName, string idxFileName)
        {
            VobSubPacks = new List<VobSubPack>();

            if (!string.IsNullOrEmpty(idxFileName) && File.Exists(idxFileName))
            {
                var idx = new Idx(idxFileName);
                IdxPalette = idx.Palette;
                IdxLanguages = idx.Languages;
                if (idx.IdxParagraphs.Count > 0)
                {
                    var buffer = new byte[0x800]; // 2048
                    using (var fs = new FileStream(vobSubFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        foreach (var p in idx.IdxParagraphs)
                        {
                            if (p.FilePosition + 100 < fs.Length)
                            {
                                long position = p.FilePosition;
                                fs.Seek(position, SeekOrigin.Begin);
                                fs.Read(buffer, 0, 0x0800);
                                if (IsSubtitlePack(buffer) || IsPrivateStream1(buffer, 0))
                                {
                                    var vsp = new VobSubPack(buffer, p);
                                    VobSubPacks.Add(vsp);

                                    if (IsPrivateStream1(buffer, 0))
                                        position += vsp.PacketizedElementaryStream.Length + 6;
                                    else
                                        position += 0x800;

                                    int currentSubPictureStreamId = vsp.PacketizedElementaryStream.SubPictureStreamId.Value;
                                    while (vsp.PacketizedElementaryStream != null &&
                                           vsp.PacketizedElementaryStream.SubPictureStreamId.HasValue &&
                                           (vsp.PacketizedElementaryStream.Length == PacketizedElementaryStreamMaximumLength ||
                                            currentSubPictureStreamId != vsp.PacketizedElementaryStream.SubPictureStreamId.Value) && position < fs.Length)
                                    {
                                        fs.Seek(position, SeekOrigin.Begin);
                                        fs.Read(buffer, 0, 0x800);
                                        vsp = new VobSubPack(buffer, p); // idx position?

                                        if (vsp.PacketizedElementaryStream != null && vsp.PacketizedElementaryStream.SubPictureStreamId.HasValue && currentSubPictureStreamId == vsp.PacketizedElementaryStream.SubPictureStreamId.Value)
                                        {
                                            VobSubPacks.Add(vsp);

                                            if (IsPrivateStream1(buffer, 0))
                                                position += vsp.PacketizedElementaryStream.Length + 6;
                                            else
                                                position += 0x800;
                                        }
                                        else
                                        {
                                            position += 0x800;
                                            fs.Seek(position, SeekOrigin.Begin);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    return;
                }
            }

            // No valid idx file found - just open like vob file
            Open(vobSubFileName);
        }
Exemplo n.º 3
0
        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 Nikse.SubtitleEdit.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;
        }