Beispiel #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;
            }
        }
Beispiel #2
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;
                        }
                    }
                }
        }