From fe150243848d2606c3b2d1096be94a980aeb8adf Mon Sep 17 00:00:00 2001 From: My-Responsitories <107370289+My-Responsitories@users.noreply.github.com> Date: Tue, 9 Jun 2026 02:06:55 +0000 Subject: [PATCH] opt: subtitle & video screenshot (#2364) --- lib/http/video.dart | 2 +- lib/pages/video/controller.dart | 17 +------ lib/pages/video/reply_new/view.dart | 14 ++++-- lib/plugin/pl_player/controller.dart | 70 ++++++++++++++-------------- pubspec.lock | 28 +++++------ pubspec.yaml | 14 +++--- 6 files changed, 68 insertions(+), 77 deletions(-) diff --git a/lib/http/video.dart b/lib/http/video.dart index de33b21d2..3d10063a1 100644 --- a/lib/http/video.dart +++ b/lib/http/video.dart @@ -851,7 +851,7 @@ abstract final class VideoHttp { ..writeAll( list.map( (item) => - '${item?['sid'] ?? 0}\n${_subtitleTimecode(item['from'])} --> ${_subtitleTimecode(item['to'])}\n${item['content'].trim()}', + '${_subtitleTimecode(item['from'])} --> ${_subtitleTimecode(item['to'])}\n${item['content'].trim()}', ), '\n\n', ); diff --git a/lib/pages/video/controller.dart b/lib/pages/video/controller.dart index d4d1014ab..b3e30dff2 100644 --- a/lib/pages/video/controller.dart +++ b/lib/pages/video/controller.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'dart:math' show min; import 'dart:ui'; @@ -54,12 +53,10 @@ import 'package:PiliPlus/services/download/download_service.dart'; import 'package:PiliPlus/utils/accounts.dart'; import 'package:PiliPlus/utils/connectivity_utils.dart'; import 'package:PiliPlus/utils/extension/context_ext.dart'; -import 'package:PiliPlus/utils/extension/file_ext.dart'; import 'package:PiliPlus/utils/extension/iterable_ext.dart'; import 'package:PiliPlus/utils/extension/num_ext.dart'; import 'package:PiliPlus/utils/extension/size_ext.dart'; import 'package:PiliPlus/utils/page_utils.dart'; -import 'package:PiliPlus/utils/path_utils.dart'; import 'package:PiliPlus/utils/platform_utils.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/storage_pref.dart'; @@ -75,7 +72,6 @@ import 'package:flutter_volume_controller/flutter_volume_controller.dart'; import 'package:get/get.dart'; import 'package:hive_ce/hive.dart'; import 'package:media_kit/media_kit.dart' hide Subtitle; -import 'package:path/path.dart' as path; class VideoDetailController extends GetxController with GetTickerProviderStateMixin, BlockMixin { @@ -1081,19 +1077,8 @@ class VideoDetailController extends GetxController final sub = subtitles[index - 1]; String subUri = subtitle.id; - File? file; if (subtitle.isData) { - subUri = path.join(tmpDirPath, '${cid.value}-${sub.lan}.vtt'); - file = File(subUri); - if (!file.existsSync()) { - await file.writeAsString(subtitle.id); - if (plPlayerController.videoPlayerController?.disposed == false) { - plPlayerController.videoPlayerController!.release.add(file.tryDel); - } else { - file.tryDel(); - return; - } - } + subUri = 'memory://$subUri'; } await plPlayerController.videoPlayerController?.setSubtitleTrack( SubtitleTrack(subUri, sub.lanDoc, sub.lan, uri: true), diff --git a/lib/pages/video/reply_new/view.dart b/lib/pages/video/reply_new/view.dart index 5bc51ab87..42d8d28b7 100644 --- a/lib/pages/video/reply_new/view.dart +++ b/lib/pages/video/reply_new/view.dart @@ -350,12 +350,16 @@ class _ReplyPageState extends CommonRichTextPubPageState { final res = await plPlayerController .plPlayerController .videoPlayerController - ?.screenshot(format: .png); + ?.screenshot(); if (res != null) { - final path = - '$tmpDirPath/${Utils.generateRandomString(8)}.png'; - await File(path).writeAsBytes(res); - imageList.add(FilePicModel(path: path)); + final png = await res.toByteData(format: .png); + if (png != null) { + final path = + '$tmpDirPath/${Utils.generateRandomString(8)}.png'; + await File(path).writeAsBytes(png.buffer.asUint8List()); + imageList.add(FilePicModel(path: path)); + } + res.dispose(); } else { debugPrint('null screenshot'); } diff --git a/lib/plugin/pl_player/controller.dart b/lib/plugin/pl_player/controller.dart index 729fbaaee..85f2f29ef 100644 --- a/lib/plugin/pl_player/controller.dart +++ b/lib/plugin/pl_player/controller.dart @@ -1724,50 +1724,52 @@ class PlPlayerController with BlockConfigMixin { videoShot = await VideoHttp.videoshot(bvid: bvid, cid: cid!); } - void takeScreenshot() { + Future takeScreenshot() async { SmartDialog.showToast('截图中'); - videoPlayerController?.screenshot(format: .png).then((value) { - if (value != null) { - SmartDialog.showToast('点击弹窗保存截图'); - showDialog( - context: Get.context!, - builder: (context) => GestureDetector( - onTap: () { - Get.back(); + final image = await videoPlayerController?.screenshot(); + if (image != null) { + SmartDialog.showToast('点击弹窗保存截图'); + showDialog( + context: Get.context!, + builder: (context) => GestureDetector( + onTap: () async { + final bytes = await image.toByteData(format: .png); + if (bytes != null) { ImageUtils.saveByteImg( - bytes: value, + bytes: bytes.buffer.asUint8List(), fileName: 'screenshot_${ImageUtils.time}', ); - }, - child: Align( - alignment: Alignment.centerRight, - child: Padding( - padding: const EdgeInsets.only(right: 12), - child: ConstrainedBox( - constraints: BoxConstraints( - maxWidth: min(DeviceUtils.size.width / 3, 350), + } + Get.back(); + }, + child: Align( + alignment: Alignment.centerRight, + child: Padding( + padding: const EdgeInsets.only(right: 12), + child: ConstrainedBox( + constraints: BoxConstraints( + maxWidth: min(MediaQuery.widthOf(context) / 3, 350), + ), + child: DecoratedBox( + decoration: BoxDecoration( + border: Border.all( + width: 5, + color: ThemeUtils.theme.colorScheme.surface, + ), ), - child: DecoratedBox( - decoration: BoxDecoration( - border: Border.all( - width: 5, - color: ThemeUtils.theme.colorScheme.surface, - ), - ), - child: Padding( - padding: const EdgeInsets.all(5), - child: Image.memory(value), - ), + child: Padding( + padding: const EdgeInsets.all(5), + child: RawImage(image: image), ), ), ), ), ), - ); - } else { - SmartDialog.showToast('截图失败'); - } - }); + ), + ).whenComplete(image.dispose); + } else { + SmartDialog.showToast('截图失败'); + } } void onPopInvokedWithResult(bool didPop, Object? result) { diff --git a/pubspec.lock b/pubspec.lock index c7ddd837d..b94f3cd1f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1115,8 +1115,8 @@ packages: description: path: media_kit ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.1.11" media_kit_libs_android_video: @@ -1124,8 +1124,8 @@ packages: description: path: "libs/android/media_kit_libs_android_video" ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.3.7" media_kit_libs_ios_video: @@ -1133,8 +1133,8 @@ packages: description: path: "libs/ios/media_kit_libs_ios_video" ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.1.4" media_kit_libs_linux: @@ -1158,8 +1158,8 @@ packages: description: path: "libs/universal/media_kit_libs_video" ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.0.5" media_kit_libs_windows_video: @@ -1167,8 +1167,8 @@ packages: description: path: "libs/windows/media_kit_libs_windows_video" ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.0.10" media_kit_native_event_loop: @@ -1176,8 +1176,8 @@ packages: description: path: media_kit_native_event_loop ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.0.9" media_kit_video: @@ -1185,8 +1185,8 @@ packages: description: path: media_kit_video ref: "version_1.2.5" - resolved-ref: "4660bd0743d5643d49093af70f8bcd404f787f9c" - url: "https://github.com/bggRGjQaUbCoE/media-kit.git" + resolved-ref: e6c3481025959a23c259aa6072a28cebaa1c0fcc + url: "https://github.com/My-Responsitories/media-kit.git" source: git version: "1.2.5" menu_base: diff --git a/pubspec.yaml b/pubspec.yaml index 475be5e3e..9e66d5eb8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -187,37 +187,37 @@ dependency_overrides: flutter_volume_controller: ^2.0.0 media_kit: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: media_kit ref: version_1.2.5 media_kit_libs_android_video: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: libs/android/media_kit_libs_android_video ref: version_1.2.5 media_kit_libs_ios_video: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: libs/ios/media_kit_libs_ios_video ref: version_1.2.5 media_kit_libs_video: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: libs/universal/media_kit_libs_video ref: version_1.2.5 media_kit_libs_windows_video: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: libs/windows/media_kit_libs_windows_video ref: version_1.2.5 media_kit_native_event_loop: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: media_kit_native_event_loop ref: version_1.2.5 media_kit_video: git: - url: https://github.com/bggRGjQaUbCoE/media-kit.git + url: https://github.com/My-Responsitories/media-kit.git path: media_kit_video ref: version_1.2.5 screen_brightness_android: