Example #1
0
        // Parse one Subtitle from an SBV file.
        // Returns null if there is no subtitle to read.
        public static SBVSubtitle ParseSingle(TextReader reader)
        {
            // Read the next non-empty line as a timestamp
            string timestampLine;

            do
            {
                timestampLine = reader.ReadLine();
                if (timestampLine == null)
                {
                    return(null);
                }
            } while(string.IsNullOrWhiteSpace(timestampLine));

            // Parse the timestamp
            var parts = timestampLine.Split(',');

            if (parts.Length != 2)
            {
                throw new FormatException("Invalid timestamp.");
            }
            TimeSpan start, end;

            if (!ParseTimestamp(parts[0], out start))
            {
                throw new FormatException($"Invalid timestamp '{parts[0]}'.");
            }
            if (!ParseTimestamp(parts[1], out end))
            {
                throw new FormatException($"Invalid timestamp '{parts[1]}'.");
            }
            var subtitle = new SBVSubtitle();

            subtitle.Start = start;
            subtitle.End   = end;

            // Collect the content
            var contentBuilder = new StringBuilder();

            for (;;)
            {
                var line = reader.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    break;
                }
                contentBuilder.AppendLine(line);
            }
            subtitle.Content = contentBuilder.ToString();

            return(subtitle);
        }
Example #2
0
        static async Task Main(string[] args)
        {
            // Read settings
            var options = new Options(args);
            var error   = options.Errors();

            if (error != null)
            {
                Console.WriteLine(error);
                return;
            }

            // Translate subtitles
            using (var inputStream = File.OpenRead(options.InputFile))
                using (var inputReader = new StreamReader(inputStream))
                    using (var outputStream = options.OutputFile != null ? File.OpenWrite(options.OutputFile) : null)
                        using (var outputWriter = outputStream != null ? new StreamWriter(outputStream) : null)
                            using (var translator = new TranslationService(
                                       options.TranslateId,
                                       options.TranslateSecret,
                                       options.TranslateEndpoint)){
                                foreach (var subtitle in SBVSubtitle.Parse(inputReader))
                                {
                                    subtitle.Content = await translator.Translate(subtitle.Content, options.InputLanguage, options.OutputLanguage);

                                    if (outputWriter != null)
                                    {
                                        outputWriter.WriteLine(subtitle);
                                    }
                                    else
                                    {
                                        Console.WriteLine(subtitle);
                                    }
                                }
                            }
        }