Skip to content

Commit

Permalink
fix: ass subtitle remove emoji. close #55 #57
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Oct 20, 2024
1 parent 75cdfef commit e269527
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Jellyfin.Plugin.Danmu.Test/ToAssTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public void TestToAss()
{
var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?><i><chatserver>chat.bilibili.com</chatserver><chatid>13113033</chatid><mission>0</mission><maxlimit>3000</maxlimit><state>0</state><real_name>0</real_name><source>k-v</source>
<d p=""253.09700,1,30,16777215,1665229999,0,e890f28,1158792239837718272,11"">杨笑汝有受到羽海野千花影响啦</d>
<d p=""225.85500,1,25,16777215,1665229198,0,e890f28,1158785518893433856,11"">风间笑死我了</d>
<d p=""225.85500,1,25,16777215,1665229198,0,e890f28,1158785518893433856,11"">风间🎉笑死我了</d>
<d p=""225.85500,1,25,16777215,1665229198,0,e890f28,1158785518893433856,11"">🥳🥳😂</d>
<d p=""213.22500,1,25,16777215,1665229172,0,e890f28,1158785301788090624,11"">有搬运车的</d>
<d p=""253.71600,1,25,16777215,1663566786,0,4cd9e142,1144840193279505664,11"">杨笑汝最喜欢的就是野海羽千花!</d>
<d p=""634.75000,1,25,16777215,1663517171,0,43c9cec0,1144424000059966464,11"">给女儿买衣服哈哈哈哈哈</d>
Expand All @@ -24,6 +25,7 @@ public void TestToAss()
<d p=""55.38000,1,25,16777215,1660413677,0,9c28a5a9,1118390004910248704,11"">这个op看得我好迷茫</d></i>
";

Danmaku2Ass.Bilibili.GetInstance().SetCustomFilter(true);
var ass = Danmaku2Ass.Bilibili.GetInstance().ToASS(Encoding.UTF8.GetBytes(xml), new Config());
Console.WriteLine(ass);
Assert.IsNotNull(ass);
Expand Down
5 changes: 5 additions & 0 deletions Jellyfin.Plugin.Danmu/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class PluginConfiguration : BasePluginConfiguration
/// </summary>
public string AssSpeed { get; set; } = string.Empty;

/// <summary>
/// 删除 emoji 表情。
/// </summary>
public bool AssRemoveEmoji { get; set; } = true;


/// <summary>
/// 检测弹幕数和视频剧集数需要一致才自动下载弹幕.
Expand Down
14 changes: 13 additions & 1 deletion Jellyfin.Plugin.Danmu/Core/Danmaku2Ass/Bilibili.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class Bilibili
{
{ "top_filter", false },
{ "bottom_filter", false },
{ "scroll_filter", false }
{ "scroll_filter", false },
{ "custom_filter", false }
};

private readonly Dictionary<int, string> mapping = new Dictionary<int, string>
Expand Down Expand Up @@ -93,6 +94,17 @@ public Bilibili SetScrollFilter(bool isFilter)
return this;
}

/// <summary>
/// 是否启用自定义过滤处理
/// </summary>
/// <param name="isFilter"></param>
/// <returns></returns>
public Bilibili SetCustomFilter(bool isFilter)
{
config["custom_filter"] = isFilter;
return this;
}

public void Create(long avid, long cid, Config subtitleConfig, string assFile)
{
//// 弹幕转换
Expand Down
18 changes: 15 additions & 3 deletions Jellyfin.Plugin.Danmu/Core/Danmaku2Ass/Filter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Danmaku2Ass
{
Expand Down Expand Up @@ -79,10 +80,21 @@ public override List<Danmaku> DoFilter(List<Danmaku> danmakus)
/// </summary>
public class CustomFilter : Filter
{
private Regex regEmoj = new Regex(@"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])", RegexOptions.Compiled);
public override List<Danmaku> DoFilter(List<Danmaku> danmakus)
{
// TODO
return base.DoFilter(danmakus);
{
// 过滤 emoji 和非法字符
List<Danmaku> keep = new List<Danmaku>();
foreach (var danmaku in danmakus)
{
danmaku.Content = this.regEmoj.Replace(danmaku.Content, string.Empty).Trim();
if (string.IsNullOrWhiteSpace(danmaku.Content))
{
continue;
}
keep.Add(danmaku);
}
return keep;
}
}
}
19 changes: 9 additions & 10 deletions Jellyfin.Plugin.Danmu/Core/Danmaku2Ass/Producer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public void LoadFilter()
{
Filters.Add("scroll_filter", new ScrollFilter());
}
//if (Config["custom_filter"])
//{
// Filters.Add("custom_filter", new CustomFilter());
//}
if (Config["custom_filter"])
{
Filters.Add("custom_filter", new CustomFilter());
}

}

Expand All @@ -52,18 +52,17 @@ public void ApplyFilter()
{ "top_filter", 0},
{ "bottom_filter", 0},
{ "scroll_filter", 0},
//{ "custom_filter",0}
{ "custom_filter", 0}
};

List<Danmaku> danmakus = Danmakus;
//string[] orders = { "top_filter", "bottom_filter", "scroll_filter", "custom_filter" };
string[] orders = { "top_filter", "bottom_filter", "scroll_filter" };
string[] orders = { "top_filter", "bottom_filter", "scroll_filter", "custom_filter" };
foreach (string name in orders)
{
Filter filter;
if (!this.Filters.TryGetValue(name, out filter))
{
continue;
if (!this.Filters.TryGetValue(name, out filter))
{
continue;
}

int count = danmakus.Count;
Expand Down
1 change: 1 addition & 0 deletions Jellyfin.Plugin.Danmu/Core/Danmaku2Ass/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Danmaku2Ass
{
Expand Down
4 changes: 4 additions & 0 deletions Jellyfin.Plugin.Danmu/LibraryManagerEventsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,10 @@ private async Task SaveDanmu(BaseItem item, byte[] bytes)
{
assConfig.TuneDuration = this.Config.AssSpeed.Trim().ToInt() - 8;
}
if (this.Config.AssRemoveEmoji)
{
Danmaku2Ass.Bilibili.GetInstance().SetCustomFilter(true);
}

var assPath = Path.Combine(item.ContainingFolderPath, item.FileNameWithoutExtension + ".danmu.ass");
Danmaku2Ass.Bilibili.GetInstance().Create(bytes, assConfig, assPath);
Expand Down

0 comments on commit e269527

Please sign in to comment.