mirror of
https://github.com/91270/MeiamSubtitles.git
synced 2026-10-29 03:25:15 +08:00
修复: 统一迅雷字幕编码为 UTF-8
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
<!-- 使用旧版宿主可解析的 SDK,避免插件要求 4.9.1.90 程序集。 -->
|
||||
<PackageReference Include="MediaBrowser.Common" Version="4.8.10" />
|
||||
<PackageReference Include="MediaBrowser.Server.Core" Version="4.8.10" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
@@ -286,14 +286,14 @@ namespace Emby.MeiamSub.Thunder
|
||||
await response.Content.CopyToAsync(stream, 81920, cancellationToken);
|
||||
var data = stream.ToArray();
|
||||
stream.Dispose();
|
||||
SubtitleContentValidator.Validate(data, format, response.ContentType);
|
||||
var utf8Data = SubtitleContentValidator.ValidateAndConvertToUtf8(data, format, response.ContentType);
|
||||
|
||||
return new SubtitleResponse()
|
||||
{
|
||||
Language = downloadSub.Language,
|
||||
IsForced = downloadSub.IsForced ?? false,
|
||||
Format = format,
|
||||
Stream = new MemoryStream(data, writable: false),
|
||||
Stream = new MemoryStream(utf8Data, writable: false),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<ItemGroup>
|
||||
<!-- 使用已验证的最低兼容基线,保持同一 DLL 可运行于 Jellyfin 10.10.7、10.11.11 和 12.1。 -->
|
||||
<PackageReference Include="Jellyfin.Controller" Version="10.10.7" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -283,8 +283,11 @@ namespace Jellyfin.MeiamSub.Thunder
|
||||
var format = NormalizeFormat(downloadSub.Format)
|
||||
?? throw new InvalidDataException($"Unsupported subtitle format: {downloadSub.Format}");
|
||||
var data = await response.Content.ReadAsByteArrayAsync(cancellationToken);
|
||||
SubtitleContentValidator.Validate(data, format, response.Content.Headers.ContentType?.MediaType);
|
||||
var stream = new MemoryStream(data, writable: false);
|
||||
var utf8Data = SubtitleContentValidator.ValidateAndConvertToUtf8(
|
||||
data,
|
||||
format,
|
||||
response.Content.Headers.ContentType?.MediaType);
|
||||
var stream = new MemoryStream(utf8Data, writable: false);
|
||||
|
||||
return new SubtitleResponse()
|
||||
{
|
||||
|
||||
@@ -7,7 +7,27 @@ namespace MeiamSubtitles.Shared
|
||||
{
|
||||
internal static class SubtitleContentValidator
|
||||
{
|
||||
private static readonly Encoding StrictUtf8 = new UTF8Encoding(false, true);
|
||||
private static readonly Encoding StrictUtf16Le = new UnicodeEncoding(false, true, true);
|
||||
private static readonly Encoding StrictUtf16Be = new UnicodeEncoding(true, true, true);
|
||||
|
||||
static SubtitleContentValidator()
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
}
|
||||
|
||||
public static void Validate(byte[] data, string format, string mediaType)
|
||||
{
|
||||
DecodeAndValidate(data, format, mediaType);
|
||||
}
|
||||
|
||||
public static byte[] ValidateAndConvertToUtf8(byte[] data, string format, string mediaType)
|
||||
{
|
||||
var text = DecodeAndValidate(data, format, mediaType);
|
||||
return Encoding.UTF8.GetBytes(text);
|
||||
}
|
||||
|
||||
private static string DecodeAndValidate(byte[] data, string format, string mediaType)
|
||||
{
|
||||
if (data == null || data.Length < 8)
|
||||
{
|
||||
@@ -38,7 +58,7 @@ namespace MeiamSubtitles.Shared
|
||||
|
||||
if (IsValidSubtitle(sample, format))
|
||||
{
|
||||
return;
|
||||
return sample;
|
||||
}
|
||||
|
||||
if (sample.StartsWith("[", StringComparison.Ordinal))
|
||||
@@ -65,35 +85,52 @@ namespace MeiamSubtitles.Shared
|
||||
|
||||
private static IEnumerable<string> DecodeSamples(byte[] data)
|
||||
{
|
||||
var length = Math.Min(data.Length, 4096);
|
||||
// Decode the complete subtitle because the selected text is returned to
|
||||
// the host after validation and must not be truncated to a probe window.
|
||||
var length = data.Length;
|
||||
|
||||
if (data.Length >= 2)
|
||||
{
|
||||
if (data[0] == 0xFF && data[1] == 0xFE)
|
||||
{
|
||||
yield return DecodeSample(data, length, Encoding.Unicode);
|
||||
yield return DecodeSample(data, length, StrictUtf16Le);
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (data[0] == 0xFE && data[1] == 0xFF)
|
||||
{
|
||||
yield return DecodeSample(data, length, Encoding.BigEndianUnicode);
|
||||
yield return DecodeSample(data, length, StrictUtf16Be);
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.Length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
|
||||
{
|
||||
yield return DecodeSample(data, length, Encoding.UTF8);
|
||||
yield return DecodeSample(data, length, StrictUtf8);
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Thunder may return UTF-16 subtitles without a BOM. Try the common
|
||||
// single-byte/UTF-8 representation and both UTF-16 byte orders, then
|
||||
// accept only a candidate that contains the expected subtitle syntax.
|
||||
yield return DecodeSample(data, length, new UTF8Encoding(false, false));
|
||||
yield return DecodeSample(data, length, Encoding.Unicode);
|
||||
yield return DecodeSample(data, length, Encoding.BigEndianUnicode);
|
||||
// Thunder may return UTF-16 or GB18030 subtitles without a BOM. Decode
|
||||
// strictly so invalid UTF-8 cannot hide the actual legacy encoding.
|
||||
foreach (var encoding in new[] { StrictUtf8, StrictUtf16Le, StrictUtf16Be, GetGb18030Encoding() })
|
||||
{
|
||||
string sample;
|
||||
try
|
||||
{
|
||||
sample = DecodeSample(data, length, encoding);
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return sample;
|
||||
}
|
||||
}
|
||||
|
||||
private static Encoding GetGb18030Encoding()
|
||||
{
|
||||
return Encoding.GetEncoding(54936, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
|
||||
}
|
||||
|
||||
private static string DecodeSample(byte[] data, int length, Encoding encoding)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -32,6 +32,67 @@ public class SubtitleContentValidatorTests
|
||||
SubtitleContentValidator.Validate(data, "srt", "application/octet-stream");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvertsGb18030SrtToUtf8ForPreview()
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n";
|
||||
var data = Encoding.GetEncoding(54936).GetBytes(content);
|
||||
|
||||
var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8(
|
||||
data,
|
||||
"srt",
|
||||
"application/octet-stream");
|
||||
|
||||
Assert.Equal(content, Encoding.UTF8.GetString(normalized));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PreservesCompleteUtf8SubtitleWhenNormalizing()
|
||||
{
|
||||
var content = "1\r\n00:00:01,000 --> 00:00:20,000\r\n" + new string('中', 3000) + "\r\n";
|
||||
|
||||
var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8(
|
||||
Encoding.UTF8.GetBytes(content),
|
||||
"srt",
|
||||
"application/octet-stream");
|
||||
|
||||
Assert.Equal(content, Encoding.UTF8.GetString(normalized));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void ConvertsUtf16SrtToUtf8ForPreview(bool bigEndian)
|
||||
{
|
||||
var encoding = new UnicodeEncoding(bigEndian, true);
|
||||
var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n";
|
||||
var data = encoding.GetPreamble().Concat(encoding.GetBytes(content)).ToArray();
|
||||
|
||||
var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8(
|
||||
data,
|
||||
"srt",
|
||||
"application/octet-stream");
|
||||
|
||||
Assert.Equal(content, Encoding.UTF8.GetString(normalized));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void ConvertsUtf16WithoutBomToUtf8ForPreview(bool bigEndian)
|
||||
{
|
||||
var encoding = new UnicodeEncoding(bigEndian, false);
|
||||
var content = "1\r\n00:00:01,000 --> 00:00:02,000\r\n中文字幕\r\n";
|
||||
|
||||
var normalized = SubtitleContentValidator.ValidateAndConvertToUtf8(
|
||||
encoding.GetBytes(content),
|
||||
"srt",
|
||||
"application/octet-stream");
|
||||
|
||||
Assert.Equal(content, Encoding.UTF8.GetString(normalized));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false, false)]
|
||||
[InlineData(false, true)]
|
||||
|
||||
Reference in New Issue
Block a user