mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-07-14 13:20:17 +08:00
57
lib/utils/subtitle_utils.dart
Normal file
57
lib/utils/subtitle_utils.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:PiliPlus/models/common/enum_with_label.dart';
|
||||
import 'package:collection/collection.dart' show IterableExtension;
|
||||
|
||||
enum SubtitleFormat implements EnumWithLabel {
|
||||
json('JSON'),
|
||||
vtt('WEBVTT'),
|
||||
srt('SRT');
|
||||
|
||||
@override
|
||||
final String label;
|
||||
const SubtitleFormat(this.label);
|
||||
}
|
||||
|
||||
abstract final class SubtitleUtils {
|
||||
static String _vttTimecode(num seconds) {
|
||||
final int h = seconds ~/ 3600;
|
||||
seconds %= 3600;
|
||||
final int m = seconds ~/ 60;
|
||||
seconds %= 60;
|
||||
final String sms = seconds.toStringAsFixed(3).padLeft(6, '0');
|
||||
return "${h.toString().padLeft(2, '0')}:${m.toString().padLeft(2, '0')}:$sms";
|
||||
}
|
||||
|
||||
static String json2Vtt(List list) {
|
||||
final sb = StringBuffer('WEBVTT\n\n')
|
||||
..writeAll(
|
||||
list.map(
|
||||
(item) =>
|
||||
'${_vttTimecode(item['from'])} --> ${_vttTimecode(item['to'])}\n${item['content'].trim()}',
|
||||
),
|
||||
'\n\n',
|
||||
);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String _srtTimecode(num seconds) {
|
||||
final int h = seconds ~/ 3600;
|
||||
seconds %= 3600;
|
||||
final int m = seconds ~/ 60;
|
||||
seconds %= 60;
|
||||
final int s = seconds.toInt();
|
||||
final int ms = ((seconds - s) * 1000).round();
|
||||
return '${h.toString().padLeft(2, '0')}:${m.toString().padLeft(2, '0')}:${s.toString().padLeft(2, '0')},${ms.toString().padLeft(3, '0')}';
|
||||
}
|
||||
|
||||
static String json2Srt(List list) {
|
||||
final sb = StringBuffer()
|
||||
..writeAll(
|
||||
list.mapIndexed(
|
||||
(i, e) =>
|
||||
'${i + 1}\n${_srtTimecode(e['from'])} --> ${_srtTimecode(e['to'])}\n${e['content'].trim()}',
|
||||
),
|
||||
'\n\n',
|
||||
);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user