Пример #1
0
 public static void ToStream(Stream writer, RawSection packer)
 {
     writer.WriteBE(packer.LanguageID);
     writer.WriteBE(packer.StreamID);
     writer.WriteBE(packer.Data.Length + 8);
     writer.Write(packer.Data);
 }
Пример #2
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;
            }
        }
Пример #3
0
        public static new RawSection FromStream(Stream reader)
        {
            var section = new RawSection();

            section.LanguageID = reader.ReadUInt16BE();
            section.StreamID   = reader.ReadUInt16BE();
            section.Length     = reader.ReadUInt32BE();
            section.Data       = new byte[section.Length - 8];
            reader.Read(section.Data, 0, section.Data.Length);

            return(section);
        }
Пример #4
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));
            }
        }
Пример #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;
                        }
                    }
                }
        }