Skip to content

Commit

Permalink
subtitleFontFile option value can be an http url
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Feb 18, 2025
1 parent 6a02294 commit 5dc79aa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/
.cxx/

# IntelliJ related
*.iml
Expand Down
9 changes: 5 additions & 4 deletions lib/fvp.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2024 Wang Bin. All rights reserved.
// Copyright 2022-2025 Wang Bin. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand All @@ -23,13 +23,13 @@ export 'src/controller.dart';
///
/// 'lowLatency': int. default is 0. reduce network stream latency. 1: for vod. 2: for live stream, may drop frames to ensure the latest content is displayed
///
/// "player": backend player properties of type Map<String, String>. See https://github.com/wang-bin/mdk-sdk/wiki/Player-APIs#void-setpropertyconst-stdstring-key-const-stdstring-value
/// "player": backend player properties of type [Map<String, String>]. See https://github.com/wang-bin/mdk-sdk/wiki/Player-APIs#void-setpropertyconst-stdstring-key-const-stdstring-value
///
/// "global": backend global options of type Map<String, Object>. See https://github.com/wang-bin/mdk-sdk/wiki/Global-Options
/// "global": backend global options of type [Map<String, Object>]. See https://github.com/wang-bin/mdk-sdk/wiki/Global-Options
///
/// "tunnel": android only, default is false. AMediacodec/MediaCodec decoder output to a SurfaceTexture surface directly without OpenGL. Maybe more efficient, but some features are not supported, e.g. HDR tone mapping, less codecs.
///
/// 'subtitleFontFile': default subtitle font file as the fallback. If not set, 'assets/subfont.ttf' will be used, you can add it in pubspec.yaml if you need it.
/// 'subtitleFontFile': default subtitle font file as the fallback, can be an http url. If not set, 'assets/subfont.ttf' will be used, you can add it in pubspec.yaml if you need it.
/// subfont.ttf can be downloaded from https://github.com/mpv-android/mpv-android/raw/master/app/src/main/assets/subfont.ttf
///
/// Example:
Expand All @@ -53,6 +53,7 @@ class VideoPlayerRegistrant {
MdkVideoPlayerPlatform.registerVideoPlayerPlatformsWith();
}
}

/*
bool isRegistered() {
return VideoPlayerPlatform.instance.runtimeType == MdkVideoPlayerPlatform;
Expand Down
29 changes: 26 additions & 3 deletions lib/src/video_player_mdk.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2024 Wang Bin. All rights reserved.
// Copyright 2022-2025 Wang Bin. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand All @@ -8,6 +8,8 @@ import 'package:flutter/widgets.dart'; //
import 'package:flutter/services.dart';
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
import 'package:logging/logging.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'fvp_platform_interface.dart';
import 'extensions.dart';
import 'media_info.dart';
Expand Down Expand Up @@ -147,6 +149,7 @@ class MdkVideoPlayerPlatform extends VideoPlayerPlatform {
_tunnel = options["tunnel"];
_playerOpts = options['player'];
_globalOpts = options['global'];
// TODO: _env => putenv
_decoders = options['video.decoders'];
_subtitleFontFile = options['subtitleFontFile'];
}
Expand Down Expand Up @@ -196,8 +199,28 @@ class MdkVideoPlayerPlatform extends VideoPlayerPlatform {
// mdk.setGlobalOptions('plugins', 'mdk-braw');
mdk.setGlobalOption("log", "all");
mdk.setGlobalOption('d3d11.sync.cpu', 1);
mdk.setGlobalOption('subtitle.fonts.file',
PlatformEx.assetUri(_subtitleFontFile ?? 'assets/subfont.ttf'));
if (_subtitleFontFile?.startsWith('http') ?? false) {
final fileName = _subtitleFontFile!.split('/').last;
getApplicationCacheDirectory().then((dir) {
final fontPath = '${dir.path}/$fileName';
_log.fine('check font path: $fontPath');
if (File(fontPath).existsSync()) {
mdk.setGlobalOption('subtitle.fonts.file', fontPath);
return;
}
_log.fine('downloading font file: $_subtitleFontFile');
http.get(Uri.parse(_subtitleFontFile!)).then((response) {
if (response.statusCode == 200) {
_log.fine('save font file: $fontPath');
File(fontPath).writeAsBytes(response.bodyBytes).then((_) {
mdk.setGlobalOption('subtitle.fonts.file', fontPath);
});
}
});
});
} else {
mdk.setGlobalOption('subtitle.fonts.file', PlatformEx.assetUri(_subtitleFontFile ?? 'assets/subfont.ttf'));
}
_globalOpts?.forEach((key, value) {
mdk.setGlobalOption(key, value);
});
Expand Down
10 changes: 6 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dependencies:
plugin_platform_interface: ^2.0.0
video_player: ^2.6.0
video_player_platform_interface: ^6.2.0
path_provider: ^2.1.2
http: ^1.3.0

dev_dependencies:
flutter_test:
Expand All @@ -46,7 +48,7 @@ flutter:
# All these are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
#implements: video_player # flutter 3.27+ can only select 1 implementation
#implements: video_player # endorsed. flutter 3.27+ can only select 1 implementation
platforms:
android:
package: com.mediadevkit.fvp
Expand All @@ -70,10 +72,10 @@ flutter:
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
# https://flutter.dev/to/asset-from-package
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
# https://flutter.dev/to/resolution-aware-images

# To add custom fonts to your plugin package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
Expand All @@ -93,4 +95,4 @@ flutter:
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages
# https://flutter.dev/to/font-from-package

0 comments on commit 5dc79aa

Please sign in to comment.