Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: add danmaku deduplication (#1778)

玲羽cfaf66b13f8e
diff --git a/lib/pages/player/player_controller.dart b/lib/pages/player/player_controller.dart
index 1229273f..ced83887 100644
--- a/lib/pages/player/player_controller.dart
+++ b/lib/pages/player/player_controller.dart
@@ -756,7 +756,12 @@ abstract class _PlayerController with Store {
}
void addDanmakus(List<Danmaku> danmakus) {
- for (var element in danmakus) {
+ final bool danmakuDeduplicationEnable = setting.get(SettingBoxKey.danmakuDeduplication, defaultValue: false);
+
+ // 如果启用了弹幕去重功能则处理5秒内相邻重复类似的弹幕进行合并
+ final List<Danmaku> listToAdd = danmakuDeduplicationEnable ? Utils.mergeDuplicateDanmakus(danmakus, timeWindowSeconds: 5) : danmakus;
+
+ for (var element in listToAdd) {
var danmakuList =
danDanmakus[element.time.toInt()] ?? List.empty(growable: true);
danmakuList.add(element);
diff --git a/lib/pages/settings/danmaku/danmaku_settings.dart b/lib/pages/settings/danmaku/danmaku_settings.dart
index d5c15ae2..cc810f36 100644
--- a/lib/pages/settings/danmaku/danmaku_settings.dart
+++ b/lib/pages/settings/danmaku/danmaku_settings.dart
@@ -30,6 +30,7 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
late bool danmakuScroll;
late bool danmakuColor;
late bool danmakuMassive;
+ late bool danmakuDeduplication;
late bool danmakuBiliBiliSource;
late bool danmakuGamerSource;
late bool danmakuDanDanSource;
@@ -60,6 +61,8 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
danmakuColor = setting.get(SettingBoxKey.danmakuColor, defaultValue: true);
danmakuMassive =
setting.get(SettingBoxKey.danmakuMassive, defaultValue: false);
+ danmakuDeduplication =
+ setting.get(SettingBoxKey.danmakuDeduplication, defaultValue: false);
danmakuBiliBiliSource =
setting.get(SettingBoxKey.danmakuBiliBiliSource, defaultValue: true);
danmakuGamerSource =
@@ -271,6 +274,17 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
description: Text('弹幕过多时进行叠加绘制', style: TextStyle(fontFamily: fontFamily)),
initialValue: danmakuMassive,
),
+ SettingsTile.switchTile(
+ onToggle: (value) async {
+ danmakuDeduplication = value ?? !danmakuDeduplication;
+ await setting.put(
+ SettingBoxKey.danmakuDeduplication, danmakuDeduplication);
+ setState(() {});
+ },
+ title: Text('弹幕去重', style: TextStyle(fontFamily: fontFamily)),
+ description: Text('相同内容弹幕过多时合并为一条弹幕', style: TextStyle(fontFamily: fontFamily)),
+ initialValue: danmakuDeduplication,
+ ),
],
),
SettingsSection(
diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart
index c70bf7cb..d7bad8ce 100644
--- a/lib/utils/storage.dart
+++ b/lib/utils/storage.dart
@@ -282,6 +282,7 @@ class SettingBoxKey {
danmakuScroll = 'danmakuScroll',
danmakuBottom = 'danmakuBottom',
danmakuMassive = 'danmakuMassive',
+ danmakuDeduplication = 'danmakuDeduplication',
danmakuArea = 'danmakuArea',
danmakuColor = 'danmakuColor',
danmakuDuration = 'danmakuDuration',
diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart
index f5d5bea9..a80fe38e 100644
--- a/lib/utils/utils.dart
+++ b/lib/utils/utils.dart
@@ -8,6 +8,7 @@ import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
+import 'package:kazumi/modules/danmaku/danmaku_module.dart';
import 'package:kazumi/request/api.dart';
import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/utils/logger.dart';
@@ -302,6 +303,119 @@ class Utils {
return color;
}
+ static List<Danmaku> mergeDuplicateDanmakus(
+ List<Danmaku> danmakus, {
+ double timeWindowSeconds = 0,
+ }) {
+ final Map<String, List<Danmaku>> grouped = {};
+
+ // 弹幕规范化处理
+ // 去首尾空格并小写,全角转半角,去掉所有空白、标点符号,连续重复内容压缩,保留日语字符
+ for (var d in danmakus) {
+ String text = d.message;
+
+ text = text.trim().toLowerCase();
+
+ final buffer = StringBuffer();
+ for (int i = 0; i < text.length; i++) {
+ int code = text.codeUnitAt(i);
+ if (code == 0x3000) {
+ buffer.writeCharCode(0x20);
+ } else if (code >= 0xFF01 && code <= 0xFF5E) {
+ buffer.writeCharCode(code - 0xFEE0);
+ } else {
+ buffer.writeCharCode(code);
+ }
+ }
+ text = buffer.toString();
+
+ text = text.replaceAll(RegExp(r'\s+'), '');
+
+ text = text.replaceAll(RegExp(
+ r'[^\w\u4e00-\u9fff\u3040-\u309F\u30A0-\u30FF\u31F0-\u31FF\uFF65-\uFF9F]',
+ unicode: true,
+ ),'');
+
+ text = text.replaceAllMapped(RegExp(r'(.)\1{2,}'), (match) {
+ final char = match.group(1)!;
+ return char * 3;
+ });
+
+ grouped.putIfAbsent(text, () => []);
+ grouped[text]!.add(d);
+ }
+
+ final List<Danmaku> result = [];
+
+ grouped.forEach((normalized, list) {
+ if (list.isEmpty) return;
+
+ if (timeWindowSeconds <= 0) {
+ if (list.length == 1) {
+ result.add(list.first);
+ } else {
+ result.add(
+ Danmaku(
+ message: '${list.first.message} x${list.length}',
+ time: list.first.time, // 默认取第一条
+ type: 5,
+ color: Utils.generateDanmakuColor(0xFFFFFF), // 白色弹幕
+ source: list.first.source,
+ ),
+ );
+ }
+ return;
+ }
+
+ list.sort((a, b) => a.time.compareTo(b.time));
+
+ List<Danmaku> currentGroup = [];
+ for (var item in list) {
+ if (currentGroup.isEmpty) {
+ currentGroup.add(item);
+ continue;
+ }
+ final last = currentGroup.last;
+ if ((item.time - last.time) <= timeWindowSeconds) {
+ currentGroup.add(item);
+ } else {
+ if (currentGroup.length == 1) {
+ result.add(currentGroup.first);
+ } else {
+ result.add(
+ Danmaku(
+ message: '${currentGroup.first.message} x${currentGroup.length}',
+ time: currentGroup.first.time,
+ type: 5,
+ color: Utils.generateDanmakuColor(0xFFFFFF),
+ source: currentGroup.first.source,
+ ),
+ );
+ }
+ currentGroup = [item];
+ }
+ }
+
+ if (currentGroup.isNotEmpty) {
+ if (currentGroup.length == 1) {
+ result.add(currentGroup.first);
+ } else {
+ result.add(
+ Danmaku(
+ message: '${currentGroup.first.message} x${currentGroup.length}',
+ time: currentGroup.first.time,
+ type: 5,
+ color: Utils.generateDanmakuColor(0xFFFFFF),
+ source: currentGroup.first.source,
+ ),
+ );
+ }
+ }
+ });
+
+ return result;
+ }
+
static int extractEpisodeNumber(String input) {
RegExp regExp = RegExp(r'第?(\d+)[话集]?');
Match? match = regExp.firstMatch(input);