A cross platform Audiobookshelf client for Android and iOS
Dart
Make BookOrbit narrated books download in seconds instead of crawling, stop them from jumping back to the start, keep read along on the book's own narration instead of transcribing, use the chapters you edited, and make library scans work on BookOrbit
Barnabas0bb34ae61a9f
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex b09d82ce..4af30ec7 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -1691,6 +1691,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {Future<void> _storeNewerServerPosition(String key, Map<String, dynamic> mp) async {if (mp['isFinished'] == true) return;+ // BookOrbit's list carries only a percent for some books, with a+ // currentTime of 0 that is no place at all: storing it sent the next+ // start back to the beginning.+ if (mp['positionUnknown'] == true) return;final serverTime = (mp['currentTime'] as num?)?.toDouble();final serverUpd = (mp['lastUpdate'] as num?)?.toInt() ?? 0;if (serverTime == null || serverUpd <= 0) return;diff --git a/lib/services/api_service.dart b/lib/services/api_service.dartindex 084693bf..1991a8ac 100644--- a/lib/services/api_service.dart+++ b/lib/services/api_service.dart@@ -424,6 +424,13 @@ class ApiService {/// Headers for a download of [url] built by [buildDownloadFileUrl].Map<String, String> downloadHeadersFor(String url) => mediaHeaders;+ /// Where a narrated EPUB downloads whole from, and the headers it needs,+ /// for the download that takes its audio out on the phone.+ Future<({String url, Map<String, String> headers})> narratedEpubDownload(String itemId, String ino) async {+ final url = buildDownloadFileUrl(itemId, ino);+ return (url: url, headers: downloadHeadersFor(url));+ }+/// Called once before a book's files are queued for download.Future<void> prepareDownload(String itemId) async {}@@ -4606,6 +4613,8 @@ class ApiService {);if (r.statusCode != 200) {debugPrint('[API] updateChapters failed: ${r.statusCode}');+ } else {+ debugPrint('[API] updateChapters $itemId: ${chapters.length} chapters saved');}return r.statusCode == 200;} catch (e) {diff --git a/lib/services/bookorbit_api_service.dart b/lib/services/bookorbit_api_service.dartindex 5397d3c2..a2262d7e 100644--- a/lib/services/bookorbit_api_service.dart+++ b/lib/services/bookorbit_api_service.dart@@ -791,6 +791,7 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {case 'me':return _routeMe(method, s, q, body);case 'libraries':+ if (method == 'POST' && n == 4 && at(3) == 'scan') return _json(await _scanLibrary(at(2)));if (method != 'GET') return null;if (n == 2) {final dock = await _dockLibrary();@@ -896,6 +897,7 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {await _boPatch('/books/${at(2)}/metadata', {'audioMetadata': {'chapters': chapters},});+ debugPrint('[BookOrbit] chapters for ${at(2)} saved: ${chapters?.length ?? 0}');await _afterEdit(at(2), chaptersChanged: true);return _json({'success': true, 'updated': true});}@@ -3503,6 +3505,17 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {return buildEbookUrl(itemId, ino);}+ /// BookOrbit streams the EPUB itself from disk, while the audio files+ /// inside it are read whole into the server's memory on every request,+ /// which crawls on a book with one big audio file. Its download tickets+ /// cover audio files only, so the EPUB goes with a freshly refreshed+ /// sign-in.+ @override+ Future<({String url, Map<String, String> headers})> narratedEpubDownload(String itemId, String ino) async {+ await _ensureFreshAccessToken();+ return (url: '$_v1/books/files/$ino/download', headers: mediaHeaders);+ }+@overrideMap<String, String> downloadHeadersFor(String url) {if (url.startsWith('$_v1/watch-downloads/media-overlay/files/')) {@@ -3722,7 +3735,7 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {final real = await _pool(listening.map((id) => () async {try {final manifest = await _manifest(id);- if (manifest == null) return null;+ if (manifest == null) return await _narrationProgress(id, byId[id]);final state = await _playbackState(id);if (state == null) return null;final card = byId[id];@@ -3745,6 +3758,43 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {return byId.values.toList();}+ /// The progress of a book read aloud from its EPUB: its place is kept on+ /// the EPUB file, not as an audiobook. Null for any other book.+ Future<Map<String, dynamic>?> _narrationProgress(String id, Map<String, dynamic>? card) async {+ final n = await _narration(id);+ if (n == null) return null;+ final spot = await _narrationPosition(id, n.fileId);+ if (spot == null) return null;+ final duration = n.overlay.duration;+ final finished = card?['isFinished'] == true;+ return {+ ...?card,+ 'id': card?['id'] ?? 'bo-$id',+ 'libraryItemId': id,+ 'mediaItemId': id,+ 'mediaItemType': 'book',+ 'episodeId': null,+ 'currentTime': spot.seconds,+ 'duration': duration,+ 'progress': finished ? 1.0 : (duration > 0 ? (spot.seconds / duration).clamp(0.0, 1.0) : 0.0),+ 'isFinished': finished,+ 'lastUpdate': spot.updatedAt ?? card?['lastUpdate'] ?? 0,+ }..remove('positionUnknown');+ }++ /// A library scan as Audiobookshelf asks for one: BookOrbit's scanner for a+ /// library, or its watcher's rescan for the Book Dock.+ Future<Map<String, dynamic>> _scanLibrary(String libId) async {+ if (libId == ApiService.bookDockLibraryId) {+ await _boPost('/book-dock/rescan');+ debugPrint('[BookOrbit] Book Dock rescan started');+ } else {+ await _boPost('/scanner/libraries/$libId/scan');+ debugPrint('[BookOrbit] library $libId scan started');+ }+ return {'success': true};+ }+/// Save a book-absolute position. Returns the HTTP status that best/// describes the outcome for Audiobookshelf callers.Future<int> _putPosition(String bookId, double absoluteSeconds, {DateTime? capturedAt}) async {@@ -3996,6 +4046,9 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {final media = BookOrbitMapper.item(detail: detail)['media'] as Map<String, dynamic>;final fileId = int.tryParse('${(media['narration'] as Map?)?['fileId']}');if (fileId == null) return null;+ // The same narrated file as last time has the same timings: reading+ // them again took seconds on a long book, and held up every start.+ if (cached != null && cached.fileId == fileId) return cached;final playlist = await _boGet('/epub/$bookId/media-overlay', {'fileId': '$fileId'}) as Map<String, dynamic>;final parsed = MediaOverlay.fromBookOrbit(playlist);if (parsed == null || parsed.isEmpty) return null;@@ -4141,7 +4194,11 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {final item = BookOrbitMapper.item(detail: detail);final media = item['media'] as Map<String, dynamic>;final metadata = media['metadata'] as Map<String, dynamic>;- final chapters = overlay.chapters();+ // Chapters saved with the chapter editor win over the narration's own+ // sections, when they fit its length.+ final edited = BookOrbitMapper.savedChapters(detail, overlay.duration);+ final useEdited = edited.isNotEmpty && (edited.last['start'] as double) < overlay.duration;+ final chapters = useEdited ? edited : overlay.chapters();final tracks = overlay.audioTracks((t) => _narrationTrackPath(bookId, n.fileId, t));media['chapters'] = chapters;media['tracks'] = tracks;@@ -4154,7 +4211,8 @@ class BookOrbitApiService extends ApiService implements BookOrbitTokenSource {..fileId = n.fileId..startPercent = overlay.duration > 0 ? (current / overlay.duration * 100).clamp(0.0, 100.0) : null;debugPrint('[BookOrbit] narration session for $bookId: ${tracks.length} tracks, '- '${overlay.clips.length} sentences, ${overlay.duration.toStringAsFixed(0)}s, from ${current.toStringAsFixed(0)}s');+ '${overlay.clips.length} sentences, ${overlay.duration.toStringAsFixed(0)}s, from ${current.toStringAsFixed(0)}s, '+ '${chapters.length} chapters ${useEdited ? 'saved with the book' : 'from the narration'}');return {'id': sessionId,'userId': '',diff --git a/lib/services/bookorbit_mapper.dart b/lib/services/bookorbit_mapper.dartindex d5f6c505..bf65ff8f 100644--- a/lib/services/bookorbit_mapper.dart+++ b/lib/services/bookorbit_mapper.dart@@ -1139,6 +1139,22 @@ class BookOrbitMapper {bookOrbitProviderIdFields['${e.key}']!: '${e.value}'.trim(),};+ /// Chapters saved on a book's audio details, where the chapter editor+ /// writes them, laid out to [duration]. Empty when none are saved.+ static List<Map<String, dynamic>> savedChapters(Map<String, dynamic> detail, double duration) {+ final raw = _maps((detail['audioMetadata'] as Map?)?['chapters']);+ double start(int i) => ((raw[i]['startMs'] as num?) ?? 0) / 1000.0;+ return [+ for (var i = 0; i < raw.length; i++)+ {+ 'id': i,+ 'start': start(i),+ 'end': i + 1 < raw.length ? start(i + 1) : duration,+ 'title': raw[i]['title'] ?? 'Chapter ${i + 1}',+ },+ ];+ }+/// Audiobookshelf chapters ({start, end, title} in seconds) as BookOrbit's/// audio chapters. Null clears them.static List<Map<String, dynamic>>? chapters(List<dynamic> abs) {diff --git a/lib/services/download_service.dart b/lib/services/download_service.dartindex a23e15b1..0b38af55 100644--- a/lib/services/download_service.dart+++ b/lib/services/download_service.dart@@ -153,9 +153,12 @@ class _PendingBook {/// (e.g. "Author/Title") on completion. Null for internal / iOS downloads.final String? safTreeUri;final String? safSubfolder;- /// An Audiobookshelf EPUB that reads itself aloud: the one task downloads- /// the whole EPUB, and its audio is taken out when it lands.+ /// An EPUB that reads itself aloud: the one task downloads the whole EPUB,+ /// and its audio is taken out when it lands.final bool narration;+ /// The narration keeps the server's own timings (BookOrbit's), saved on+ /// this phone when the download started, rather than the file's.+ final bool serverTimings;/// True once the user cancels, so terminal handling cleans up instead of/// surfacing an error.@@ -200,6 +203,7 @@ class _PendingBook {this.safTreeUri,this.safSubfolder,this.narration = false,+ this.serverTimings = false,}) {// Start the no-progress watchdog clock at creation (fresh or restored).lastUpdate = DateTime.now();@@ -230,6 +234,7 @@ class _PendingBook {if (safTreeUri != null) 'safTreeUri': safTreeUri,if (safSubfolder != null) 'safSubfolder': safSubfolder,if (narration) 'narration': true,+ if (serverTimings) 'serverTimings': true,};factory _PendingBook.fromJson(Map<String, dynamic> j) => _PendingBook(@@ -251,6 +256,7 @@ class _PendingBook {safTreeUri: j['safTreeUri'] as String?,safSubfolder: j['safSubfolder'] as String?,narration: j['narration'] == true,+ serverTimings: j['serverTimings'] == true,);}@@ -775,7 +781,7 @@ class DownloadService extends ChangeNotifier {media['duration'] = duration;media['chapters'] = chapters;media['tracks'] = audioTracks;- media['narration'] = {'duration': duration, 'local': true};+ media['narration'] = {...?(media['narration'] as Map?), 'duration': duration, 'local': true};final stored = {...item, 'media': media};final cover = localCoverPath ??(api == null ? null : await _cacheCover(api, itemId, api.getCoverUrl(itemId, width: 800)));@@ -854,7 +860,7 @@ class DownloadService extends ChangeNotifier {media['duration'] = duration;media['chapters'] = chapters;media['tracks'] = audioTracks;- media['narration'] = {'duration': duration, 'local': true};+ media['narration'] = {...?(media['narration'] as Map?), 'duration': duration, 'local': true};}_downloads[itemId] = DownloadInfo(itemId: itemId,@@ -1909,7 +1915,7 @@ class DownloadService extends ChangeNotifier {);notifyListeners();- if (episodeId == null && !api.isBookOrbit && NarratedBooks.contains(itemId)) {+ if (episodeId == null && await _isNarratedEpubBook(api, itemId)) {await _executeNarrationDownload(api: api, itemId: itemId, title: title, author: author, coverUrl: coverUrl, libraryId: libraryId);return;@@ -2168,9 +2174,22 @@ class DownloadService extends ChangeNotifier {}}- /// An Audiobookshelf EPUB that reads itself aloud downloads whole, as one- /// background task with the usual notifications, so it carries on with the- /// app in the background or closed. Its audio is taken out once it lands+ /// Whether [itemId] is a book whose audio is its EPUB's narration: known on+ /// this phone for Audiobookshelf, said by the item itself on BookOrbit.+ Future<bool> _isNarratedEpubBook(ApiService api, String itemId) async {+ if (!api.isBookOrbit) return NarratedBooks.contains(itemId);+ try {+ final item = await api.getLibraryItem(itemId);+ return (item?['media'] as Map?)?['narration'] != null;+ } catch (e) {+ debugPrint('[Download] could not tell whether $itemId is narrated: $e');+ return false;+ }+ }++ /// An EPUB that reads itself aloud downloads whole, as one background task+ /// with the usual notifications, so it carries on with the app in the+ /// background or closed. Its audio is taken out once it lands/// ([_finishNarration]).Future<void> _executeNarrationDownload({required ApiService api,@@ -2233,8 +2252,21 @@ class DownloadService extends ChangeNotifier {final fileMeta = ebookFile['metadata'] as Map?;final filename = _sanitizePath(fileMeta?['filename'] as String? ?? 'book.epub');final size = (fileMeta?['size'] as num?)?.toInt();- final url = api.buildDownloadFileUrl(itemId, ino);+ final target = await api.narratedEpubDownload(itemId, ino);+ final url = target.url;final wifiOnly = await PlayerSettings.getWifiOnlyDownloads();+ // BookOrbit streams and keeps places by its own timings, so the+ // download keeps them too, saved now while the server is reachable.+ var serverTimings = false;+ if (api.isBookOrbit) {+ final timings = await NarrationService.instance.audioNarration(api, itemId);+ if (timings != null) {+ await NarrationService.instance.keepTimings(itemId, timings);+ serverTimings = true;+ } else {+ debugPrint('[Download] BookOrbit gave no timings for $itemId - the EPUB\'s own will be used');+ }+ }_pending[itemId] = _PendingBook(itemId: itemId,@@ -2249,6 +2281,7 @@ class DownloadService extends ChangeNotifier {expectedPaths: ['${bookDir.path}/$filename'],slimSessionJson: jsonEncode({'libraryItem': slimItem}),narration: true,+ serverTimings: serverTimings,);await _persistPending();@@ -2267,7 +2300,7 @@ class DownloadService extends ChangeNotifier {task = DownloadTask(taskId: _taskId(itemId, 0),url: url,- headers: api.downloadHeadersFor(url),+ headers: target.headers,filename: filename,baseDirectory: BaseDirectory.root,directory: bookDir.path,@@ -2282,7 +2315,7 @@ class DownloadService extends ChangeNotifier {task = DownloadTask(taskId: _taskId(itemId, 0),url: url,- headers: api.downloadHeadersFor(url),+ headers: target.headers,filename: filename,baseDirectory: BaseDirectory.applicationDocuments,directory: relDir!,@@ -2303,7 +2336,8 @@ class DownloadService extends ChangeNotifier {);final ok = await FileDownloader().enqueue(task);debugPrint('[Download] narration EPUB for "$title" ($itemId) enqueued ok=$ok: $filename, '- '${size ?? '?'} bytes, dir=${bookDir.path}, '+ '${size ?? '?'} bytes, from ${Uri.parse(url).path}, dir=${bookDir.path}, '+ '${serverTimings ? "BookOrbit's timings kept, " : ''}''audio goes to ${useSaf ? 'the custom folder' : 'app storage'} once it lands');if (!ok) throw Exception('Failed to enqueue the EPUB');} catch (e) {@@ -2331,8 +2365,8 @@ class DownloadService extends ChangeNotifier {var ok = false;try {if (item != null) {- ok = await NarrationService.instance- .keepFromEpub(item, itemId, epubPath, p.bookDir, localCoverPath: p.localCoverPath);+ ok = await NarrationService.instance.keepFromEpub(item, itemId, epubPath, p.bookDir,+ localCoverPath: p.localCoverPath, serverTimings: p.serverTimings);} else {debugPrint('[Download] narration for $itemId has no saved item to keep it with');}diff --git a/lib/services/narration_service.dart b/lib/services/narration_service.dartindex 8a5bb407..67fe734c 100644--- a/lib/services/narration_service.dart+++ b/lib/services/narration_service.dart@@ -469,6 +469,10 @@ class NarrationService {/// The narration laid on the audio files' real lengths, saved, and kept as/// the book's download.+ ///+ /// [measure] and [markNarrated] are for Audiobookshelf. BookOrbit's places+ /// go by its own timings, kept as they are, and it already knows which+ /// books are narrated; [savedChapters] keeps the chapters saved with it.Future<void> _keep(ApiService? api,Map<String, dynamic> item,@@ -477,18 +481,34 @@ class NarrationService {String dir,List<String> paths, {String? localCoverPath,+ bool measure = true,+ bool markNarrated = true,+ bool savedChapters = false,}) async {- final measured = <double?>[];- for (final p in paths) {- measured.add(await audioFileDuration(p));+ var timed = o;+ if (measure) {+ final measured = <double?>[];+ for (final p in paths) {+ measured.add(await audioFileDuration(p));+ }+ timed = o.withTrackDurations(measured);+ debugPrint('[Narration] $itemId measured ${measured.where((m) => m != null).length} of '+ '${paths.length} files: ${o.duration.toStringAsFixed(0)}s from the timings, '+ '${timed.duration.toStringAsFixed(0)}s from the audio');+ if (measured.every((m) => m != null)) await ScopedPrefs.setBool(_timedKey(itemId), true);}- final timed = o.withTrackDurations(measured);- debugPrint('[Narration] $itemId measured ${measured.where((m) => m != null).length} of '- '${paths.length} files: ${o.duration.toStringAsFixed(0)}s from the timings, '- '${timed.duration.toStringAsFixed(0)}s from the audio');- await _remember(itemId, timed);- await markAudiobook(itemId, timed);- if (measured.every((m) => m != null)) await ScopedPrefs.setBool(_timedKey(itemId), true);+ if (measure) {+ await _remember(itemId, timed);+ } else {+ await keepTimings(itemId, timed);+ }+ if (markNarrated) await markAudiobook(itemId, timed);+ // Chapters saved with the book win over the narration's sections when+ // they fit, the same as when it streams.+ final saved = savedChapters+ ? [for (final c in (item['media'] as Map?)?['chapters'] as List? ?? const []) Map<String, dynamic>.from(c as Map)]+ : const <Map<String, dynamic>>[];+ final useSaved = saved.isNotEmpty && ((saved.last['start'] as num?) ?? 0) < timed.duration;await DownloadService().registerNarration(api: api,localCoverPath: localCoverPath,@@ -497,7 +517,7 @@ class NarrationService {dirPath: dir,paths: paths,duration: timed.duration,- chapters: timed.chapters(),+ chapters: useSaved ? saved : timed.chapters(),audioTracks: timed.audioTracks((t) => t.href),);}@@ -512,15 +532,30 @@ class NarrationService {String epubPath,String dir, {String? localCoverPath,+ bool serverTimings = false,}) async {final started = DateTime.now();- final o = await compute(_parseEpub, epubPath);- if (o == null || o.isEmpty) {+ final parsed = await compute(_parseEpub, epubPath);+ if (parsed == null || parsed.isEmpty) {debugPrint('[Narration] the downloaded EPUB for $itemId holds no narration');- await _remember(itemId, null);+ if (!serverTimings) await _remember(itemId, null);return false;}- final hrefs = o.tracks.map((t) => t.href).toList();+ // BookOrbit's timings, saved when the download started, are what its+ // streaming, read along and saved places go by; the file's own are the+ // fallback.+ final kept = serverTimings ? await localOverlay(itemId) : null;+ if (serverTimings && kept == null) {+ debugPrint('[Narration] BookOrbit timings for $itemId are gone - using the EPUB\'s own');+ }+ final o = kept ?? parsed;+ // Each track's file inside the zip, found by the path the EPUB gives it.+ final hrefs = [+ for (final t in o.tracks)+ parsed.tracks+ .firstWhere((p) => MediaOverlay.sameDoc(p.href, t.href), orElse: () => t)+ .href,+ ];final names = _trackNames(o.audioTracks((t) => t.href));final paths = await compute(_extract, (epub: epubPath, dir: dir, hrefs: hrefs, names: names));debugPrint('[Narration] took ${paths.length} audio files out of the downloaded EPUB for $itemId '@@ -546,10 +581,20 @@ class NarrationService {} catch (e) {debugPrint('[Narration] the downloaded EPUB for $itemId was not deleted: $e');}- await _keep(null, item, itemId, o, dir, paths, localCoverPath: localCoverPath);+ await _keep(null, item, itemId, o, dir, paths,+ localCoverPath: localCoverPath,+ measure: kept == null,+ markNarrated: !serverTimings,+ savedChapters: serverTimings);return true;}+ /// Keeps [o] as [itemId]'s narration on this phone (memory and disk).+ Future<void> keepTimings(String itemId, MediaOverlay o) async {+ _overlays[itemId] = o;+ await _save(itemId, o);+ }+/// Plays the narration, from the sentence [from] when given (read along/// starting at the page in view, "play from here"). An Audiobookshelf book/// plays it once downloaded: false until then.diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dartindex 8ace4a14..97d672bb 100644--- a/lib/widgets/ebook_reader_view.dart+++ b/lib/widgets/ebook_reader_view.dart@@ -298,6 +298,11 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObsbool get _useNarration =>_narration != null && (_narrationFromServer || !_hasAudio || _audioIsNarration);+ // The narration and audio lookups started on open: read along waits for+ // them, since which kind it is depends on their answers.+ Future<void>? _narrationLookup;+ Future<void>? _audioMetaLookup;+// Reader settingsint _fontSize = 16;double _lineHeight = 1.4;@@ -408,7 +413,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs// behind the sliding-in reader, which makes opening from a home card janky.PlayerSettings.settingsChanged.addListener(_loadSkipSettings);_loadSkipSettings();- _loadAudioMeta();+ _audioMetaLookup = _loadAudioMeta();_volumeNav.attach();PlayerSettings.getTranscriptionEnabled().then((on) {if (mounted && on) setState(() => _transcriptionOn = true);@@ -1133,7 +1138,8 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObsfinal serverSpot = _otherAccount ? null : _pullServerPosition(api);final file = await NarrationService.instance.readerFile(api, widget.itemId, widget.ebookFile, widget.title);- unawaited(_loadNarration(api, file));+ _narrationLookup = _loadNarration(api, file);+ unawaited(_narrationLookup);if (_otherAccount) {await _restoreOtherAccountSpot();} else {@@ -3996,10 +4002,21 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs_stopReadAlong();return;}+ // Following the book's own narration or transcribing depends on what the+ // book is, so a tap in the first moments waits for that answer.+ final lookups = [+ if (_narrationLookup != null) _narrationLookup!,+ if (_audioMetaLookup != null) _audioMetaLookup!,+ ];+ if (lookups.isNotEmpty) {+ await Future.wait(lookups).timeout(const Duration(seconds: 8), onTimeout: () => const []);+ if (!mounted) return;+ }if (_useNarration) {await _startNarrationReadAlong();return;}+ debugPrint('[ReadAlong] ${widget.itemId} is not narrated here - transcribing to follow along');final player = AudioPlayerService();if (player.currentItemId != widget.itemId ||player.currentEpisodeId != null) {