示例#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;
            }
        }
示例#2
0
        public static void Unpack(string path, string output)
        {
            Directory.CreateDirectory(output);

            using (var input = File.OpenRead(path))
            {
                var outBuffer = new MovieSubtitle();

                int index = 0;

                while (input.Position < input.Length)
                {
                    var section = StreamSection.FromStream(input);

                    switch (section.Type)
                    {
                    case SectionType.Subtitle:
                        var subtitleSection = section as SubtitleSection;

                        outBuffer.Subtitles.Add(subtitleSection);
                        break;

                    case SectionType.EndOfStream:
                        if (outBuffer.Subtitles.Count > 0)
                        {
                            var fileName   = string.Format("Subtitle_{0:D5}.xml", index);
                            var outputPath = Path.Combine(output, fileName);
                            SerializationHelper.Save(outBuffer, outputPath);
                            outBuffer.Subtitles.Clear();
                        }
                        outBuffer.Length   = input.Position - outBuffer.Position;
                        outBuffer.Position = input.Position;
                        index++;
                        break;

                    case SectionType.Raw:
                        var rawSection = section as RawSection;
                        break;
                    }
                }
            }
        }
示例#3
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;
                        }
                    }
                }
        }