Пример #1
0
        public static ISubtitle GetSubtitleFromFile(string fileName)
        {
            var subtitleCreator = new SubtitleCreator();

            var subtitlePath   = SubtitleFolderPath + fileName;
            var subtitleStream = File.OpenRead(subtitlePath);
            var subtitle       = subtitleCreator.CreateFromFile(subtitleStream);

            return(subtitle);
        }
Пример #2
0
        public void Subtitle_ToString_MatchesSource()
        {
            var subtitle        = SubtitleContainer.GetSubtitleFromFile(SubtitleContainer.ValidSubtitleName);
            var subtitleCreator = new SubtitleCreator();

            var subtitleString        = subtitle.ToString();
            var reconstructedSubtitle = subtitleCreator.CreateFromString(subtitleString);

            for (var i = 0; i < subtitle.Items.Count; i++)
            {
                Assert.Equal(subtitle.Items[i].ToString(), reconstructedSubtitle.Items[i].ToString());
            }
        }
Пример #3
0
        public void PerformAction()
        {
            var subtitleCreator = new SubtitleCreator();
            var subtitle        = subtitleCreator.CreateFromFile(File.OpenRead(_sourceFile));

            if (_timeOffset != 0)
            {
                Console.WriteLine("Offsetting your subtitle by " + _timeOffset + " seconds");

                var timeOffsetAction = new SubtitleTimeOffsetAction(TimeSpan.FromSeconds(_timeOffset), subtitle);
                timeOffsetAction.PerformAction();
            }

            if (!string.IsNullOrEmpty(_language))
            {
                var culture          = new CultureInfo(_language);
                var translatorAction = new SubtitleTranslatorAction(subtitle, culture);

                var reset = new ManualResetEvent(false);

                Console.WriteLine("Translating your subtitle to " + culture.EnglishName);
                Console.WriteLine("Please wait...");

                translatorAction.PerformAction(
                    () =>
                {
                    SaveSubtitleToOutput(subtitle, _destination);
                    reset.Set();
                });

                reset.WaitOne();
            }
            else
            {
                SaveSubtitleToOutput(subtitle, _destination);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            var htmlDir  = ConfigurationManager.AppSettings["HtmlDir"];
            var mediaDir = ConfigurationManager.AppSettings["MediaDir"];

            Console.WriteLine($"HmlDir: {htmlDir}");
            Console.WriteLine($"MediaDir: {mediaDir}");

            var trasncriptPath  = $"{mediaDir}Transcript\\";
            var translationPath = $"{mediaDir}Translation\\";

            Directory.CreateDirectory(trasncriptPath);
            Directory.CreateDirectory(translationPath);

            var htmlFiles = Directory.GetFiles(htmlDir, "*.*", SearchOption.AllDirectories)
                            .Where(f => f.ToLower().EndsWith(".html") || f.ToLower().EndsWith(".htm"))
                            .ToList();

            var failedFiles = new List <FailedFile>();

            foreach (var htmlFile in htmlFiles)
            {
                try
                {
                    Console.WriteLine("Procesing {0}", htmlFile);

                    var html = File.ReadAllText(htmlFile);

                    var captionsPattern = new Regex(@"var CAPTIONS = \[(.*?)\];", RegexOptions.Multiline);
                    var mediaIdPattern  = new Regex(@"var MEDIA_ID = (.*?);");
                    var fileNamePattern = new Regex(@"var FILE_NAME = '(.*?)';");

                    Match captionsMatch = captionsPattern.Match(html);
                    Match mediIdMatch   = mediaIdPattern.Match(html);
                    Match fileNameMatch = fileNamePattern.Match(html);

                    var captionsJson     = JArray.Parse($"[{captionsMatch.Groups[1].Value}]");
                    var mediaId          = mediIdMatch.Groups[1].Value;
                    var oldVideoFileName = $"{mediaId}_{fileNameMatch.Groups[1].Value}";

                    var newVideoFileName = oldVideoFileName;

                    var newVideoFileNameWithoutExt = Path.GetFileNameWithoutExtension(newVideoFileName);

                    // cehck if video exitx
                    if (!File.Exists($"{mediaDir}{oldVideoFileName}"))
                    {
                        failedFiles.Add(new FailedFile
                        {
                            FileName = htmlFile,
                            Error    = "Video does not exists"
                        });
                        continue;
                    }

                    using (
                        var subWriterTranscript =
                            new SubtitleCreator(File.CreateText($"{trasncriptPath}{newVideoFileNameWithoutExt}.srt")))
                        using (
                            var subWriterTranscriptLocal =
                                new SubtitleCreator(File.CreateText($"{mediaDir}{newVideoFileNameWithoutExt}.fr.srt")))
                            using (
                                var subWriterTranslation =
                                    new SubtitleCreator(File.CreateText($"{translationPath}{newVideoFileNameWithoutExt}.srt")))
                                using (
                                    var subWriterTranslationLocal =
                                        new SubtitleCreator(File.CreateText($"{mediaDir}{newVideoFileNameWithoutExt}.en.srt")))
                                {
                                    foreach (var caption in captionsJson)
                                    {
                                        //transcript, translation, time_in, time_out

                                        var transcript  = (string)caption["transcript"];
                                        var translation = (string)caption["translation"];
                                        var timeIn      = (double)caption["time_in"];
                                        var timeOut     = (double)caption["time_out"];

                                        subWriterTranscript.AddSubtitle(timeIn, timeOut, transcript);
                                        subWriterTranscriptLocal.AddSubtitle(timeIn, timeOut, transcript);

                                        subWriterTranslation.AddSubtitle(timeIn, timeOut, translation);
                                        subWriterTranslationLocal.AddSubtitle(timeIn, timeOut, translation);
                                    }
                                }
                }
                catch (Exception ex)
                {
                    failedFiles.Add(new FailedFile
                    {
                        FileName = htmlFile,
                        Error    = ex.Message
                    });
                }
            }
            if (failedFiles.Any())
            {
                Console.WriteLine("Files with error:");
                foreach (var failedFile in failedFiles)
                {
                    Console.WriteLine("File: {0}", failedFile.FileName);
                    Console.WriteLine("Error: {0}", failedFile.Error);
                }
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }