feat: 新增字幕功能,倍速移至底部

This commit is contained in:
orz12
2024-02-27 13:53:22 +08:00
parent 11c2a69e61
commit 81da31c91b
4 changed files with 306 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:developer';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hive/hive.dart';
import '../common/constants.dart';
import '../models/common/reply_type.dart';
@@ -155,7 +156,8 @@ class VideoHttp {
}
// 免登录查看1080p
if ((userInfoCache.get('userInfoCache') == null || MineController.anonymity) &&
if ((userInfoCache.get('userInfoCache') == null ||
MineController.anonymity) &&
setting.get(SettingBoxKey.p1080, defaultValue: true)) {
data['try_look'] = 1;
}
@@ -476,4 +478,110 @@ class VideoHttp {
return {'status': false, 'data': []};
}
}
static Future subtitlesJson(
{String? aid, String? bvid, required int cid}) async {
assert(aid != null || bvid != null);
var res = await Request().get(Api.subtitleUrl, data: {
if (aid != null) 'aid': aid,
if (bvid != null) 'bvid': bvid,
'cid': cid,
});
if (res.data['code'] == 0) {
dynamic data = res.data['data'];
List subtitlesJson = data['subtitle']['subtitles'];
/*
[
{
"id": 1430455228267894300,
"lan": "ai-zh",
"lan_doc": "中文(自动生成)",
"is_lock": false,
"subtitle_url": "//aisubtitle.hdslb.com/bfs/ai_subtitle/prod/15508958271448462983dacf99a49f40ccdf91a4df8d925e2b58?auth_key=1708941835-aaa0e44844594386ad356795733983a2-0-89af73c6aad5a1fca43b02113fa9d485",
"type": 1,
"id_str": "1430455228267894272",
"ai_type": 0,
"ai_status": 2
}
]
*/
return {
'status': true,
'data': subtitlesJson,
};
} else {
return {'status': false, 'data': [], 'msg': res.data['message']};
}
}
static Future vttSubtitles(List subtitlesJson) async {
if (subtitlesJson.isEmpty) {
return [];
}
List<Map<String, String>> subtitlesVtt = [];
String subtitleTimecode(double seconds) {
int h = (seconds / 3600).floor();
int m = ((seconds % 3600) / 60).floor();
int s = (seconds % 60).floor();
int ms = ((seconds * 1000) % 1000).floor();
if (h == 0) {
return "${m.toString().padLeft(2, '0')}:${s.toString().padLeft(2, '0')}.${ms.toString().padLeft(3, '0')}";
}
return "${h.toString().padLeft(2, '0')}:${m.toString().padLeft(2, '0')}:${s.toString().padLeft(2, '0')}.${ms.toString().padLeft(3, '0')}";
}
for (var i in subtitlesJson) {
var res =
await Request().get("https://${i['subtitle_url'].split('//')[1]}");
/*
{
"font_size": 0.4,
"font_color": "#FFFFFF",
"background_alpha": 0.5,
"background_color": "#9C27B0",
"Stroke": "none",
"type": "AIsubtitle",
"lang": "zh",
"version": "v1.6.0.4",
"body": [
{
"from": 0.5,
"to": 1.58,
"sid": 1,
"location": 2,
"content": "很多人可能不知道",
"music": 0.0
},
……,
{
"from": 558.629,
"to": 560.22,
"sid": 280,
"location": 2,
"content": "我们下期再见",
"music": 0.0
}
]
}
*/
if (res.data != null) {
String vttData = "WEBVTT\n\n";
for (var item in res.data['body']) {
vttData += "${item['sid']}\n";
vttData +=
"${subtitleTimecode(item['from'])} --> ${subtitleTimecode(item['to'])}\n";
vttData += "${item['content']}\n\n";
}
subtitlesVtt.add({
'language': i['lan'],
'title': i['lan_doc'],
'text': vttData,
});
} else {
SmartDialog.showToast("字幕${i['lan_doc']}加载失败, ${res.data['message']}");
}
}
return subtitlesVtt;
}
}