Example #1
0
        private void LoadSubtitleFromMpeg2PesPackets(Subtitle subtitle, Stream stream)
        {
            long position = 0;
            stream.Position = 0;
            stream.Seek(position, SeekOrigin.Begin);
            long streamLength = stream.Length;
            var buffer = new byte[512];
            PresentationSegments = new List<DialogPresentationSegment>();
            while (position < streamLength)
            {
                stream.Seek(position, SeekOrigin.Begin);
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead < 20)
                    break;

                int size = (buffer[4] << 8) + buffer[5] + 6;
                position += size;

                if (bytesRead > 10 && VobSubParser.IsPrivateStream2(buffer, 0))
                {
                    if (buffer[6] == SegmentTypeDialogPresentation)
                    {
                        var dps = new DialogPresentationSegment(buffer);
                        PresentationSegments.Add(dps);
                        subtitle.Paragraphs.Add(new Paragraph(dps.Text.Trim(), dps.StartPtsMilliseconds, dps.EndPtsMilliseconds));
                    }
                    else if (buffer[6] == SegmentTypeDialogStyle)
                    {
                        StyleSegment = new DialogStyleSegment(buffer);
                    }
                }
            }
        }
Example #2
0
        private void LoadSubtitleFromM2Ts(Subtitle subtitle, Stream ms)
        {
            var subtitlePackets = new List<Packet>();
            const int packetLength = 188;
            bool isM2TransportStream = DetectFormat(ms);
            var packetBuffer = new byte[packetLength];
            var m2TsTimeCodeBuffer = new byte[4];
            long position = 0;
            ms.Position = 0;

            // check for Topfield .rec file
            ms.Seek(position, SeekOrigin.Begin);
            ms.Read(m2TsTimeCodeBuffer, 0, 3);
            if (m2TsTimeCodeBuffer[0] == 0x54 && m2TsTimeCodeBuffer[1] == 0x46 && m2TsTimeCodeBuffer[2] == 0x72)
                position = 3760;

            long transportStreamLength = ms.Length;
            while (position < transportStreamLength)
            {
                ms.Seek(position, SeekOrigin.Begin);
                if (isM2TransportStream)
                {
                    ms.Read(m2TsTimeCodeBuffer, 0, m2TsTimeCodeBuffer.Length);
                    var tc = (m2TsTimeCodeBuffer[0] << 24) + (m2TsTimeCodeBuffer[1] << 16) + (m2TsTimeCodeBuffer[2] << 8) + (m2TsTimeCodeBuffer[3] & Helper.B00111111);
                    // should m2ts time code be used in any way?
                    var msecs = (ulong)Math.Round((tc) / 27.0); // 27 or 90?
                    var tc2 = new TimeCode(msecs);
                    System.Diagnostics.Debug.WriteLine(tc2);
                    position += m2TsTimeCodeBuffer.Length;
                }

                ms.Read(packetBuffer, 0, packetLength);
                byte syncByte = packetBuffer[0];
                if (syncByte == Packet.SynchronizationByte)
                {
                    var packet = new Packet(packetBuffer);
                    if (packet.PacketId == TextSubtitleStreamPid)
                    {
                        subtitlePackets.Add(packet);
                    }
                    position += packetLength;
                }
                else
                {
                    position++;
                }
            }

            //TODO: merge ts packets

            PresentationSegments = new List<DialogPresentationSegment>();
            foreach (var item in subtitlePackets)
            {
                if (item.Payload != null && item.Payload.Length > 10 && VobSubParser.IsPrivateStream2(item.Payload, 0))
                {
                    if (item.Payload[6] == SegmentTypeDialogPresentation)
                    {
                        var dps = new DialogPresentationSegment(item.Payload);
                        PresentationSegments.Add(dps);
                        subtitle.Paragraphs.Add(new Paragraph(dps.Text.Trim(), dps.StartPtsMilliseconds, dps.EndPtsMilliseconds));
                    }
                    else if (item.Payload[6] == SegmentTypeDialogStyle)
                    {
                        StyleSegment = new DialogStyleSegment(item.Payload);
                    }
                }
            }

            subtitle.Renumber();
        }