From edf0eaa7dd96e41512af45c573851655a3bc19b2 Mon Sep 17 00:00:00 2001 From: appdevelpo <56633229+appdevelpo@users.noreply.github.com> Date: Sun, 14 Apr 2024 00:05:05 +0800 Subject: [PATCH] add external playing option for linux -mpv( with header and subtitle support) -vlc --- lib/controllers/detail_controller.dart | 3 +- lib/utils/external_player.dart | 48 ++++++++++++++++++--- lib/views/pages/settings/settings_page.dart | 7 +++ linux/CMakeLists.txt | 2 +- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/controllers/detail_controller.dart b/lib/controllers/detail_controller.dart index f862c070..bc530cb0 100644 --- a/lib/controllers/detail_controller.dart +++ b/lib/controllers/detail_controller.dart @@ -382,7 +382,8 @@ class DetailPageController extends GetxController { await launchMobileExternalPlayer(watchData.url, player); return; } - await launchDesktopExternalPlayer(watchData.url, player); + await launchDesktopExternalPlayer(watchData.url, player, + watchData.headers ?? {}, watchData.subtitles ?? []); return; } catch (e) { showPlatformSnackbar( diff --git a/lib/utils/external_player.dart b/lib/utils/external_player.dart index 45c5b609..0d7190e8 100644 --- a/lib/utils/external_player.dart +++ b/lib/utils/external_player.dart @@ -1,6 +1,9 @@ import 'dart:io'; import 'package:android_intent_plus/android_intent.dart'; +import 'package:miru_app/models/index.dart'; +import 'package:miru_app/utils/request.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:url_launcher/url_launcher_string.dart'; Future launchMobileExternalPlayer(String playUrl, String player) async { @@ -19,14 +22,49 @@ Future launchMobileExternalPlayer(String playUrl, String player) async { } // desktop -Future launchDesktopExternalPlayer(String playUrl, String player) async { +Future launchDesktopExternalPlayer( + String playUrl, + String player, + Map headers, + List subs) async { + final subLink = subs.map((e) => e.url).toList(); + //windows + if (Platform.isWindows) { + switch (player) { + case "vlc": + const vlc = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe'; + await Process.run(vlc, [playUrl]); + break; + case "potplayer": + await _launchExternalPlayer("potplayer://$playUrl"); + break; + } + } + //linux switch (player) { case "vlc": - const vlc = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe'; - await Process.run(vlc, [playUrl]); + await Process.run("vlc", [playUrl]); break; - case "potplayer": - await _launchExternalPlayer("potplayer://$playUrl"); + case "mpv": + final sub = []; + var directory = await getTemporaryDirectory(); + for (final s in subs) { + final fileName = Uri.parse(s.url).pathSegments.last; + final response = await dio.get(s.url); + final targetPath = '${directory.path}/$fileName'; + await File(targetPath).writeAsString(response.data); + sub.add(targetPath); + } + + final uA = headers.remove("User-Agent"); + final headerFields = + headers.entries.map((e) => '${e.key}: ${e.value}').join(', '); + await Process.run("mpv", [ + playUrl, + '--user-agent=$uA', + if (headerFields.isNotEmpty) '--http-header-fields="$headerFields"', + if (subLink.isNotEmpty) '--sub-files=${sub.join(":")}', + ]); break; } } diff --git a/lib/views/pages/settings/settings_page.dart b/lib/views/pages/settings/settings_page.dart index b30056e7..ef6b58de 100644 --- a/lib/views/pages/settings/settings_page.dart +++ b/lib/views/pages/settings/settings_page.dart @@ -235,6 +235,13 @@ class _SettingsPageState extends State { "Other": "other", }; } + if (Platform.isLinux) { + return { + "settings.external-player-builtin".i18n: "built-in", + "VLC": "vlc", + "mpv": "mpv", + }; + } return { "settings.external-player-builtin".i18n: "built-in", "VLC": "vlc", diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index 8109795a..0b449674 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -90,7 +90,7 @@ set_target_properties(${BINARY_NAME} # Generated plugin build rules, which manage building the plugins and adding # them to the application. include(flutter/generated_plugins.cmake) - +target_link_libraries(${BINARY_NAME} PRIVATE ${MIMALLOC_LIB}) # === Installation === # By default, "installing" just makes a relocatable bundle in the build