Esempio n. 1
0
        private void LoadFromFile(string path)
        {
            Stream fs = File.OpenRead(path);

            List = Parser.ParseStream(fs, Encoding.Default);
            fs.Close();
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
        }
Esempio n. 5
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;
                }
            }
        }