diff options
| -rw-r--r-- | CONTRIBUTORS.md | 1 | ||||
| -rw-r--r-- | MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs | 8 | ||||
| -rw-r--r-- | tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleEncoderTests.cs | 40 |
3 files changed, 49 insertions, 0 deletions
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 2bd0aeba6a..ac10184a09 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -250,6 +250,7 @@ - [TheDreadPirate](https://github.com/thedreaddpirate) - [Daniel Aquino](https://github.com/chino) - [klizas](https://github.com/klizas) + - [p27ZuH6CNAdzr](https://github.com/p27ZuH6CNAdzr) # Emby Contributors diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index fba644b74a..27a987fefe 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -188,6 +188,14 @@ namespace MediaBrowser.MediaEncoding.Subtitles _logger.LogDebug("charset {CharSet} detected for {Path}", detected.EncodingName, fileInfo.Path); + // If encoding is not supported by .NET (e.g., ISO-8859-16), return stream as-is + // The charset name will be passed to ffmpeg via -sub_charenc if needed + if (detected.Encoding is null) + { + _logger.LogWarning("Encoding {Encoding} is not supported by .NET for {Path}, returning stream as-is", detected.EncodingName, fileInfo.Path); + return stream; + } + await using (stream.ConfigureAwait(false)) { using var reader = new StreamReader(stream, detected.Encoding); diff --git a/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleEncoderTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleEncoderTests.cs index 48850b2f67..13779851a9 100644 --- a/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleEncoderTests.cs +++ b/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleEncoderTests.cs @@ -269,6 +269,46 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests } } + [Fact] + public async Task GetSubtitleStream_UnsupportedEncoding_DoesNotThrow() + { + // Regression test for issue #2200 + // When the charset detector identifies an encoding not supported by .NET (e.g., ISO-8859-16), + // GetSubtitleStream should handle it gracefully instead of throwing ArgumentNullException + var cancellationToken = TestContext.Current.CancellationToken; + var srt = BuildGreekSrt(); + var path = Path.GetTempFileName(); + try + { + // Write as ISO-8859-7 (Greek) to create a non-UTF-8 file + await File.WriteAllTextAsync(path, srt, Encoding.GetEncoding("iso-8859-7"), cancellationToken); + + var fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }); + var subtitleEncoder = fixture.Create<SubtitleEncoder>(); + + var fileInfo = new SubtitleEncoder.SubtitleInfo + { + Path = path, + Protocol = MediaProtocol.File, + Format = "srt", + IsExternal = true + }; + + // This should not throw even if the detected encoding was unsupported + using var stream = await subtitleEncoder.GetSubtitleStream(fileInfo, cancellationToken); + Assert.NotNull(stream); + + // Verify we can read from the stream without error + using var reader = new StreamReader(stream); + var text = await reader.ReadToEndAsync(cancellationToken); + Assert.NotEmpty(text); + } + finally + { + File.Delete(path); + } + } + private static SubtitleEncoder CreateEncoder() { var fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }); |
