Beispiel #1
0
        static void readSubFile(string fileSpec)
        {
            var parser = new SubtitlesParser.Classes.Parsers.SrtParser();

            using (var fileStream = File.OpenRead(fileSpec))
            {
                subsOriginal = parser.ParseStream(fileStream, System.Text.Encoding.UTF8);
            }
        }
Beispiel #2
0
        public async Task <TranslationResult> TranslateAsync(TranslatorConfig cfg)
        {
            var apiToken = cfg.YandexTranslateToken;

            if (apiToken == null && cfg.YandexTranslateOAuthToken != null)
            {
                var yandexIamTokensApi = ApiFactory.CreateIamApi();
                apiToken = await yandexIamTokensApi.CreateToken(cfg.YandexTranslateOAuthToken);
            }

            if (string.IsNullOrEmpty(apiToken))
            {
                return(TranslationResult.FromError("Error: Yandex Translate api token is not provided."));
            }

            var api = ApiFactory.CreateTranslateApi(apiToken);

            var parser = new SubtitlesParser.Classes.Parsers.SrtParser();

            await using var fileStream = File.OpenRead(cfg.SourceFile);

            var items      = parser.ParseStream(fileStream, Encoding.Default);
            var allStrings = items.SelectMany(x => x.Lines).Distinct().ToList();

            var translations = new Dictionary <string, string>();

            foreach (var batch in allStrings.Batch(10))
            {
                var src = batch.ToList();
                try
                {
                    var translateResponse = await api.Translate(new TranslateRequest
                    {
                        folder_id          = cfg.YandexCloudFolderId,
                        targetLanguageCode = cfg.TargetLanguageCode,
                        texts = src
                    });

                    for (var i = 0; i < src.Count; i++)
                    {
                        var dst = translateResponse.Translations[i].Text;
                        dst = FixTags(dst);
                        translations.Add(src[i], dst);
                    }
                }
                catch (Exception exc)
                {
                    return(TranslationResult.FromError(exc.ToString()));
                }
            }

            var translation = SubtitlesToString(items, translations);

            return(TranslationResult.Ok(translation));
        }
        private IEnumerable <SubtitleRow> ParseStream(Stream subtitleStream)
        {
            var parser   = new SubtitlesParser.Classes.Parsers.SrtParser();
            var encoding = DetectEncoding(subtitleStream);

            List <SubtitleItem> items = parser.ParseStream(subtitleStream, encoding);

            IEnumerable <SubtitleRow> rows = new List <SubtitleRow>();

            foreach (SubtitleItem item in items)
            {
                (rows as List <SubtitleRow>).Add(item.ToSubtitleRow());
            }
            ;

            return(rows);
        }
Beispiel #4
0
        private void NewFile_Click(object sender, EventArgs e)
        {
            bool continueNew = true;

            if (List.Count > 0)
            {
                DialogResult res;// = MessageBox.Show("Save changes to " + SaveLocation + "?", "Subtitle Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                if (SaveLocation == "")
                {
                    res = MessageBox.Show("Save changes to current file?", "Subtitle Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                }
                else
                {
                    res = MessageBox.Show("Save changes to " + SaveLocation + "?", "Subtitle Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                }

                if (res == DialogResult.Cancel)
                {
                    continueNew = false;
                }
                else if (res == DialogResult.Yes)
                {
                    if (SaveLocation == "")
                    {
                        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                        saveFileDialog1.Filter = "srt files (*.srt)|*.srt|All files (*.*)|*.*";
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            SaveLocation = saveFileDialog1.FileName;
                        }
                    }
                    SaveToFile(SaveLocation);
                }
            }
            if (continueNew)
            {
                ClearAllCurrent();
                treeView.Nodes.Clear();
                Parser       = new SubtitlesParser.Classes.Parsers.SrtParser();
                List         = new List <SubtitlesParser.Classes.SubtitleItem>();
                SaveLocation = "";
                CurrentNode  = 1;
            }
        }
Beispiel #5
0
        private static void Proceed(Options o)
        {
            var stream1 = new FileStream(o.InputFiles.ElementAt(0), FileMode.Open, FileAccess.Read);
            var stream2 = new FileStream(o.InputFiles.ElementAt(1), FileMode.Open, FileAccess.Read);

            var file1 = new SubtitlesParser.Classes.Parsers.SrtParser().ParseStream(stream1, Encoding.UTF8).Select(x => new { SubtitleItem = x, Type = "c1" });
            var file2 = new SubtitlesParser.Classes.Parsers.SrtParser().ParseStream(stream2, Encoding.UTF8).Select(x => new { SubtitleItem = x, Type = "c2" });

            var txt = file1.Concat(file2).OrderBy(x => x.SubtitleItem.StartTime).ToList();


            var lineItems = string.Join("<br/>", txt.Select(x =>
            {
                var htmlDoc = new HtmlDocument();

                htmlDoc.LoadHtml(string.Join(" ", x.SubtitleItem.Lines));
                var innerText = Regex.Replace(htmlDoc.DocumentNode.InnerText, @"\t|\n|\r", "");

                innerText = string.Join(" ", innerText.Split(" ").Where(x => x.Trim().Length > 0));

                var words = innerText.Split(new string[] { " ", ".", ",", ":", "!", "?" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.ToLower()).Distinct();

                var pron = x.Type != "c1" ? "" :
                           string.Join("\r\n", words
                                       .Select(t => GetPronounce(t))
                                       .Where(t => !string.IsNullOrWhiteSpace(t.us))
                                       .Select(t => {
                    var id = Guid.NewGuid().ToString("N").Substring(0, 10);
                    return(@$ "<v onclick='p(" "{t.usvoice.Substring(45, t.usvoice.Length - 45)}" ")'>{t.word.ToLower()} : {t.us}</v>");
                })
                                       );

                return($"<x class='{x.Type}'> {innerText}</x>" + pron);
            }));

            File.WriteAllText(o.OutputFile, CreateBodyContent(lineItems));
        }
Beispiel #6
0
        void SetUI()
        {
            if (FeaturedVideos == null || FeaturedVideos.Count == 0)
            {
                return;
            }

            //Choose the video with least play count
            FeaturedVideos.OrderBy(v => v.PlayCount);

            foreach (var video in FeaturedVideos)
            {
                string full_path = VideoFolderPath + video.Filename;

                FileInfo vfi = new FileInfo(full_path);
                if (vfi.Exists && vfi.Extension.ToLower() == ".mp4")
                {
                    VideoPlayer.Source = new Uri(full_path, UriKind.Absolute);

                    #region Load Subtitle
                    if (SrtParser == null)
                    {
                        SrtParser = new SubtitlesParser.Classes.Parsers.SrtParser();
                    }

                    string   subtitle_path = VideoFolderPath + video.SubtitleFilename;
                    FileInfo sfi           = new FileInfo(subtitle_path);
                    if (sfi.Exists && sfi.Extension.ToLower() == ".srt")
                    {
                        try
                        {
                            using (FileStream fs = File.OpenRead(subtitle_path))
                            {
                                SubtitleItems = SrtParser.ParseStream(fs, Encoding.Default);

                                if (SubtitleItems != null && SubtitleItems.Count > 0)
                                {
                                    if (SubtitleTimer == null)
                                    {
                                        SubtitleTimer          = new System.Timers.Timer();
                                        SubtitleTimer.Interval = 100;
                                        SubtitleTimer.Elapsed += SubtitleTimer_Elapsed;
                                    }

                                    SubtitleTimer.Start();
                                }
                            }
                        }
                        catch { }
                    }
                    #endregion

                    video.IncrementPlayCount();
                    txtVideoTitle.Text      = video.Title;
                    VideoPlayer.MediaEnded += VideoPlayer_MediaEnded;
                    VideoPlayer.Play();

                    break;
                }
            }
        }