Skip to content

Commit

Permalink
Improve srt parsing, workaround PopScope bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb committed Apr 21, 2024
1 parent 5090a3a commit b615cca
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 65 deletions.
59 changes: 35 additions & 24 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -246,34 +247,44 @@ class _AppState extends State<App> with WidgetsBindingObserver {
return false;
}
}
BuildContext? dialogContext;
bool? dialog = await showDialog(
context: context,
builder: (context) => PopScope(
canPop: true,
child: AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to exit channel?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: const Text('No'),
),
TextButton(
onPressed: () {
// canPop = true;
Navigator.of(context).pop(true);
Navigator.of(context).pop(true);
},
child: const Text('Yes'),
),
],
),
),
useRootNavigator: true,
builder: (context) {
dialogContext = context;
return PopScope(
canPop: true,
child: AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to exit channel?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: const Text('No'),
),
TextButton(
onPressed: () {
// canPop = true;
Navigator.of(context).pop(true);
},
child: const Text('Yes'),
),
],
),
);
},
);
SystemChrome.restoreSystemUIOverlays();
return dialog ?? false;
final result = dialog ?? false;
if (result) {
// todo bug with stayed dialog in release mode, idk
if (kReleaseMode) Navigator.of(context).pop();
if (dialogContext != null) Navigator.of(dialogContext!).pop();
}
return result;
}

Future<AddVideo?> _addUrlDialog(BuildContext context) async {
Expand Down
59 changes: 57 additions & 2 deletions lib/models/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,11 @@ class PlayerModel extends ChangeNotifier {
return RawCaptionFile([]);
}
if (response.statusCode == 200) {
final data = utf8.decode(response.bodyBytes);
var data = utf8.decode(response.bodyBytes);
if (url.endsWith('.srt')) {
return SubRipCaptionFile(data);
data = parseSrt(data);
return WebVTTCaptionFile(data);
// return SubRipCaptionFile(data);
} else if (url.endsWith('.vtt')) {
return WebVTTCaptionFile(data);
} else {
Expand All @@ -310,6 +312,59 @@ class PlayerModel extends ChangeNotifier {
}
}

static String parseSrt(String text) {
final subs = <Map<String, String>>[];
final lines = text.replaceAll('\r\n', '\n').split('\n');
final blocks = getSrtBlocks(lines);
final badTimeReg = RegExp(r'(,[\d]+)');
for (final lines in blocks) {
if (lines.length < 3) continue;
final textLines = lines.getRange(2, lines.length).toList();
final time = lines[1].replaceAllMapped(badTimeReg, (match) {
final ms = match.group(1)!;
return ms.length < 4 ? ms.padRight(4, '0') : ms;
});
subs.add({
'counter': lines[0],
'time': time.replaceAll(',', '.'),
'text': textLines.join('\n').trim(),
});
}
var data = 'WEBVTT\n\n';
for (final sub in subs) {
data += '${sub['counter']}\n';
data += '${sub['time']}\n';
data += '${sub['text']}\n\n';
}
return data;
}

static List<List<String>> getSrtBlocks(List<String> lines) {
final blocks = <List<String>>[];
final isNumLineReg = RegExp(r'^(\d+)$');
// [id, time, firstTextLine, ... lastTextLine]
var block = <String>[];
for (var i = 0; i < lines.length; i++) {
final line = lines[i];
if (blocks.isEmpty && line.isEmpty) continue;
final prevLine = i > 0 ? lines[i - 1] : '';
final nextLine = i < lines.length - 1 ? lines[i + 1] : '';
// block id line
if (prevLine.isEmpty &&
isNumLineReg.hasMatch(line) &&
nextLine.contains('-->')) {
// push previously collected block and start new one
if (block.isNotEmpty) {
blocks.add(block);
block = [];
}
}
block.add(line);
}
if (block.isNotEmpty) blocks.add(block);
return blocks;
}

static Future<ClosedCaptionFile> getYoutubeSubtitles(String url) async {
final yt = youtube.YoutubeExplode();
try {
Expand Down
48 changes: 20 additions & 28 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: "direct main"
description:
name: app_links
sha256: fd7fc1569870b4b0d90d17a9f36661a6ff92400fecb6e4adab4abe0f0488bb5f
sha256: "42dc15aecf2618ace4ffb74a2e58a50e45cd1b9f2c17c8f0cafe4c297f08c815"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
version: "4.0.1"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -85,10 +85,10 @@ packages:
dependency: "direct main"
description:
name: device_info_plus
sha256: "77f757b789ff68e4eaf9c56d1752309bd9f7ad557cb105b938a7f8eb89e59110"
sha256: eead12d1a1ed83d8283ab4c2f3fca23ac4082f29f25f29dff0f758f57d06ec91
url: "https://pub.dev"
source: hosted
version: "9.1.2"
version: "10.1.0"
device_info_plus_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -176,14 +176,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
name: js
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
url: "https://pub.dev"
source: hosted
version: "0.7.1"
json_annotation:
dependency: transitive
description:
Expand Down Expand Up @@ -268,10 +260,10 @@ packages:
dependency: "direct main"
description:
name: package_info_plus
sha256: "7e76fad405b3e4016cd39d08f455a4eb5199723cf594cd1b8916d47140d93017"
sha256: cb44f49b6e690fa766f023d5b22cac6b9affe741dd792b6ac7ad4fabe0d7b097
url: "https://pub.dev"
source: hosted
version: "4.2.0"
version: "6.0.0"
package_info_plus_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -356,10 +348,10 @@ packages:
dependency: "direct main"
description:
name: shared_preferences
sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02"
sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev"
source: hosted
version: "2.2.2"
version: "2.2.3"
shared_preferences_android:
dependency: transitive
description:
Expand Down Expand Up @@ -545,10 +537,10 @@ packages:
dependency: "direct main"
description:
name: video_player
sha256: afc65f4b8bcb2c188f64a591f84fb471f4f2e19fc607c65fd8d2f8fedb3dec23
sha256: db6a72d8f4fd155d0189845678f55ad2fd54b02c10dcafd11c068dbb631286c0
url: "https://pub.dev"
source: hosted
version: "2.8.3"
version: "2.8.6"
video_player_android:
dependency: transitive
description:
Expand Down Expand Up @@ -593,18 +585,18 @@ packages:
dependency: "direct main"
description:
name: wakelock_plus
sha256: "26ebc8b5e0037c15e2a1b661dcec8a475cb7205befcce8a33f545ae8c86b367c"
sha256: c8b7cc80f045533b40a0e6c9109905494e3cf32c0fbd5c62616998e0de44003f
url: "https://pub.dev"
source: hosted
version: "1.1.6"
version: "1.2.4"
wakelock_plus_platform_interface:
dependency: transitive
description:
name: wakelock_plus_platform_interface
sha256: "40fabed5da06caff0796dc638e1f07ee395fb18801fbff3255a2372db2d80385"
sha256: "422d1cdbb448079a8a62a5a770b69baa489f8f7ca21aef47800c726d404f9d16"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.2.1"
web:
dependency: transitive
description:
Expand All @@ -617,18 +609,18 @@ packages:
dependency: "direct main"
description:
name: web_socket_channel
sha256: "1d8e795e2a8b3730c41b8a98a2dff2e0fb57ae6f0764a1c46ec5915387d257b2"
sha256: "58c6666b342a38816b2e7e50ed0f1e261959630becd4c879c4f26bfa14aa5a42"
url: "https://pub.dev"
source: hosted
version: "2.4.4"
version: "2.4.5"
win32:
dependency: transitive
description:
name: win32
sha256: "8cb58b45c47dcb42ab3651533626161d6b67a2921917d8d429791f76972b3480"
sha256: "0a989dc7ca2bb51eac91e8fd00851297cfffd641aa7538b165c62637ca0eaa4a"
url: "https://pub.dev"
source: hosted
version: "5.3.0"
version: "5.4.0"
win32_registry:
dependency: transitive
description:
Expand Down Expand Up @@ -657,10 +649,10 @@ packages:
dependency: "direct main"
description:
name: youtube_explode_dart
sha256: "77a55747579c76b5d071bca3941cfca141207f064b3f0322994573cb4a0c2831"
sha256: "12d32dffd8c85927eb46f7cf7a9dfce690edfe82134c08a90529c51eba58a85c"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.2.0"
sdks:
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.19.0"
22 changes: 11 additions & 11 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+14
version: 1.0.0+16

environment:
sdk: ">=3.0.0 <4.0.0"
sdk: ">=3.3.0 <4.0.0"

dependencies:
flutter:
Expand All @@ -28,29 +28,29 @@ dependencies:

provider: ^6.0.5

shared_preferences: ^2.0.18
shared_preferences: ^2.2.3

video_player: ^2.6.0
video_player: ^2.8.6

perfect_volume_control: ^1.0.5

web_socket_channel: ^2.4.4
web_socket_channel: ^2.4.5

http: ^1.2.1

youtube_explode_dart: ^2.0.1
youtube_explode_dart: ^2.2.0

url_launcher: ^6.1.10
url_launcher: ^6.2.5

wakelock_plus: ^1.1.1
wakelock_plus: ^1.2.4

native_device_orientation: ^2.0.2

device_info_plus: ^9.0.3
device_info_plus: ^10.1.0

package_info_plus: ^4.2.0
package_info_plus: ^6.0.0

app_links: ^4.0.0
app_links: ^4.0.1

ota_update: ^6.0.0

Expand Down

0 comments on commit b615cca

Please sign in to comment.