コード例 #1
0
ファイル: SubtitleManager.cs プロジェクト: ximliu/jellyfin
        public async Task DownloadSubtitles(
            Video video,
            LibraryOptions libraryOptions,
            string subtitleId,
            CancellationToken cancellationToken)
        {
            var parts    = subtitleId.Split('_', 2);
            var provider = GetProvider(parts[0]);

            try
            {
                var response = await GetRemoteSubtitles(subtitleId, cancellationToken).ConfigureAwait(false);

                await TrySaveSubtitle(video, libraryOptions, response).ConfigureAwait(false);
            }
            catch (RateLimitExceededException)
            {
                throw;
            }
            catch (Exception ex)
            {
                SubtitleDownloadFailure?.Invoke(this, new SubtitleDownloadFailureEventArgs
                {
                    Item      = video,
                    Exception = ex,
                    Provider  = provider.Name
                });

                throw;
            }
        }
コード例 #2
0
        public async Task DownloadSubtitles(Video video,
                                            LibraryOptions libraryOptions,
                                            string subtitleId,
                                            CancellationToken cancellationToken)
        {
            var parts    = subtitleId.Split(new[] { '_' }, 2);
            var provider = GetProvider(parts.First());

            var saveInMediaFolder = libraryOptions.SaveSubtitlesWithMedia;

            try
            {
                var response = await GetRemoteSubtitles(subtitleId, cancellationToken).ConfigureAwait(false);

                using (var stream = response.Stream)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await stream.CopyToAsync(memoryStream).ConfigureAwait(false);

                        memoryStream.Position = 0;

                        var savePaths    = new List <string>();
                        var saveFileName = Path.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLower();

                        if (response.IsForced)
                        {
                            saveFileName += ".forced";
                        }

                        saveFileName += "." + response.Format.ToLower();

                        if (saveInMediaFolder)
                        {
                            savePaths.Add(Path.Combine(video.ContainingFolderPath, saveFileName));
                        }

                        savePaths.Add(Path.Combine(video.GetInternalMetadataPath(), saveFileName));

                        await TrySaveToFiles(memoryStream, savePaths).ConfigureAwait(false);
                    }
                }
            }
            catch (RateLimitExceededException)
            {
                throw;
            }
            catch (Exception ex)
            {
                SubtitleDownloadFailure?.Invoke(this, new SubtitleDownloadFailureEventArgs
                {
                    Item      = video,
                    Exception = ex,
                    Provider  = provider.Name
                });

                throw;
            }
        }