Ejemplo n.º 1
0
        public static void ToStream(Stream writer, StreamSection packet)
        {
            switch (packet.Type)
            {
            case SectionType.Subtitle: SubtitleSection.ToStream(writer, (SubtitleSection)packet); break;

            case SectionType.EndOfStream: EndOfStreamSection.ToStream(writer, (EndOfStreamSection)packet); break;

            case SectionType.Raw: RawSection.ToStream(writer, (RawSection)packet); break;
            }
        }
Ejemplo n.º 2
0
        public static void ToStream(Stream writer, EndOfStreamSection packet)
        {
            writer.WriteBE(packet.LanguageID);
            writer.WriteBE(packet.StreamID);

            writer.WriteBE(packet.Length);

            writer.WriteBE(packet.Value);
            writer.Write(0u);

            writer.WriteOffset(0x800);
        }
Ejemplo n.º 3
0
        public static StreamSection FromStream(Stream reader)
        {
            var id = reader.ReadUInt32BE() & 0xff;

            reader.Seek(-4, SeekOrigin.Current);

            switch (id)
            {
            case 0x04: return(SubtitleSection.FromStream(reader));

            case 0xF0: return(EndOfStreamSection.FromStream(reader));

            default: return(RawSection.FromStream(reader));
            }
        }
Ejemplo n.º 4
0
        public static new EndOfStreamSection FromStream(Stream reader)
        {
            var packet = new EndOfStreamSection();

            packet.LanguageID = reader.ReadUInt16BE();
            packet.StreamID   = reader.ReadUInt16BE();
            packet.Length     = reader.ReadUInt32BE();

            packet.Value = reader.ReadUInt32BE();
            uint dummy = reader.ReadUInt32BE();

            Debug.Assert(dummy == 0);

            reader.Offset(0x800);

            return(packet);
        }
Ejemplo n.º 5
0
        public static void ApplyTranslation(string originalPath, string outputPath)
        {
            var index            = 0;
            var subtitleLocation = originalPath.Remove(originalPath.LastIndexOf('.'));

            var set    = LoadSubtitle(index, subtitleLocation);
            var buffer = new Dictionary <long, byte[]>();

            using (var output = File.Create(outputPath))
                using (var input = File.OpenRead(originalPath))
                {
                    while (input.Position < input.Length)
                    {
                        var section = StreamSection.FromStream(input);

                        switch (section.Type)
                        {
                        case SectionType.Raw:
                            var rawSection = section as RawSection;

                            using (var ms = new MemoryStream())
                            {
                                RawSection.ToStream(ms, rawSection);

                                AppendToBuffer(buffer, rawSection.First, ms.ToArray());
                            }
                            break;

                        case SectionType.EndOfStream:

                            if (buffer.Count > 0)
                            {
                                var source = buffer.ToArray().OrderBy(x => x.Key);

                                foreach (var item in source)
                                {
                                    output.Write(item.Value);
                                }

                                buffer.Clear();
                            }


                            EndOfStreamSection.ToStream(output, section as EndOfStreamSection);
                            index++;

                            set = LoadSubtitle(index, subtitleLocation);

                            break;

                        case SectionType.Subtitle:
                            var subtitleSection = section as SubtitleSection;

                            SubtitleSection loaded;
                            using (var ms = new MemoryStream())
                            {
                                if (set != null && set.TryGetValue(GetKey(subtitleSection), out loaded))
                                {
                                    if (loaded.Texts.Where(x => x.TranslatedText != null).Count() > 0)
                                    {
                                        var copy = loaded.Texts.ToArray();

                                        for (int i = 0; i < copy.Length; i++)
                                        {
                                            loaded.Texts.Clear();
                                            loaded.Texts.Add(copy[i]);
                                            loaded.BaseTime = copy[i].Start;

                                            SubtitleSection.ToStreamTranslated(ms, loaded);
                                            AppendToBuffer(buffer, loaded.BaseTime, ms.ToArray());
                                        }
                                    }
                                    else
                                    {
                                        SubtitleSection.ToStream(ms, subtitleSection);
                                        AppendToBuffer(buffer, subtitleSection.BaseTime, ms.ToArray());
                                    }
                                }
                                else
                                {
                                    SubtitleSection.ToStream(ms, subtitleSection);
                                    AppendToBuffer(buffer, subtitleSection.BaseTime, ms.ToArray());
                                }
                            }
                            break;
                        }
                    }
                }
        }