Skip to content

Commit

Permalink
chore: improve tags extraction and fallback
Browse files Browse the repository at this point in the history
ref: #480
  • Loading branch information
MSOB7YY committed Feb 6, 2025
1 parent 73ca3eb commit 58c4568
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/class/faudiomodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class FArtwork {
}

class FTags {
bool get isValid =>
title?.isNotEmpty == true || //
album?.isNotEmpty == true ||
artist?.isNotEmpty == true ||
albumArtist?.isNotEmpty == true;

/// Used for bulk extractions.
final String path;
final FArtwork artwork;
Expand Down
20 changes: 20 additions & 0 deletions lib/class/media_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,40 @@ class MIStream {
class MIStreamTags {
final String? handlerName;
final String? language;
final String? title;
final String? artist;
final String? album;
final String? albumArtist;
final String? track;

const MIStreamTags({
this.handlerName,
this.language,
this.title,
this.artist,
this.album,
this.albumArtist,
this.track,
});

factory MIStreamTags.fromMap(Map<dynamic, dynamic> map) => MIStreamTags(
handlerName: map.getOrUpperCase("handler_name"),
language: map.getOrUpperCase("language"),
title: map["Title"] ?? map.getOrUpperCase("title"),
artist: map["Artist"] ?? map.getOrUpperCase("artist"),
album: map["Album"] ?? map.getOrUpperCase("album"),
albumArtist: map["AlbumArtist"] ?? map.getOrUpperCase("albumArtist") ?? map.getOrUpperCase("album_artist"),
track: map["Track"] ?? map.getOrUpperCase("track"),
);

Map<dynamic, dynamic> toMap() => {
"handler_name": handlerName,
"language": language,
"title": title,
"artist": artist,
"album": album,
"album_artist": albumArtist,
"track": track,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class _TagsExtractorAndroid extends TagsExtractor {
}
}

if (trackInfo == null || trackInfo.hasError) {
if (trackInfo == null || trackInfo.hasError || !trackInfo.tags.isValid) {
final ffmpegInfo = await _ffmpegQueue.add(() => ffmpegController.extractMetadata(trackPath).timeout(const Duration(seconds: 5)).catchError((_) => null));

if (ffmpegInfo != null && isVideo) {
Expand All @@ -113,7 +113,8 @@ class _TagsExtractorAndroid extends TagsExtractor {
videoController.addLocalVideoFileInfoToCacheMap(trackPath, ffmpegInfo, stats);
} catch (_) {}
}
trackInfo = ffmpegInfo == null ? FAudioModel.dummy(trackPath, artwork) : ffmpegInfo.toFAudioModel(artwork: artwork);
final newTrackInfo = ffmpegInfo == null ? FAudioModel.dummy(trackPath, artwork) : ffmpegInfo.toFAudioModel(artwork: artwork);
trackInfo = newTrackInfo.merge(trackInfo);
}

return trackInfo;
Expand Down
60 changes: 55 additions & 5 deletions lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,56 @@ extension CacheGetterVideo on VideoStream {
}
}

extension FAudioModelExtensions on FAudioModel {
FAudioModel merge(FAudioModel? original) {
if (original == null) return this;
return FAudioModel(
tags: FTags(
path: original.tags.path.isNotEmpty ? original.tags.path : this.tags.path,
artwork: original.tags.artwork.hasArtwork ? original.tags.artwork : this.tags.artwork,
title: original.tags.title ?? this.tags.title,
album: original.tags.album ?? this.tags.album,
albumArtist: original.tags.albumArtist ?? this.tags.albumArtist,
artist: original.tags.artist ?? this.tags.artist,
composer: original.tags.composer ?? this.tags.composer,
genre: original.tags.genre ?? this.tags.genre,
trackNumber: original.tags.trackNumber ?? this.tags.trackNumber,
trackTotal: original.tags.trackTotal ?? this.tags.trackTotal,
discNumber: original.tags.discNumber ?? this.tags.discNumber,
discTotal: original.tags.discTotal ?? this.tags.discTotal,
lyrics: original.tags.lyrics ?? this.tags.lyrics,
comment: original.tags.comment ?? this.tags.comment,
description: original.tags.description ?? this.tags.description,
synopsis: original.tags.synopsis ?? this.tags.synopsis,
year: original.tags.year ?? this.tags.year,
language: original.tags.language ?? this.tags.language,
lyricist: original.tags.lyricist ?? this.tags.lyricist,
djmixer: original.tags.djmixer ?? this.tags.djmixer,
mixer: original.tags.mixer ?? this.tags.mixer,
mood: original.tags.mood ?? this.tags.mood,
rating: original.tags.rating ?? this.tags.rating,
remixer: original.tags.remixer ?? this.tags.remixer,
tags: original.tags.tags ?? this.tags.tags,
tempo: original.tags.tempo ?? this.tags.tempo,
country: original.tags.country ?? this.tags.country,
recordLabel: original.tags.recordLabel ?? this.tags.recordLabel,
ratingPercentage: original.tags.ratingPercentage ?? this.tags.ratingPercentage,
gainData: original.tags.gainData ?? this.tags.gainData,
),
durationMS: original.durationMS ?? this.durationMS,
bitRate: original.bitRate ?? this.bitRate,
channels: original.channels ?? this.channels,
encodingType: original.encodingType ?? this.encodingType,
format: original.format ?? this.format,
sampleRate: original.sampleRate ?? this.sampleRate,
isVariableBitRate: original.isVariableBitRate ?? this.isVariableBitRate,
isLoseless: original.isLoseless ?? this.isLoseless,
hasError: original.hasError,
errorsMap: original.errorsMap,
);
}
}

extension MediaInfoToFAudioModel on MediaInfo {
FAudioModel toFAudioModel({required FArtwork? artwork}) {
final infoFull = this;
Expand All @@ -260,13 +310,13 @@ extension MediaInfoToFAudioModel on MediaInfo {
tags: FTags(
path: infoFull.path,
artwork: artwork ?? FArtwork(),
title: info?.title,
album: info?.album,
albumArtist: info?.albumArtist,
artist: info?.artist,
title: info?.title ?? audioStream?.tags?.title,
album: info?.album ?? audioStream?.tags?.album,
albumArtist: info?.albumArtist ?? audioStream?.tags?.albumArtist,
artist: info?.artist ?? audioStream?.tags?.artist,
composer: info?.composer,
genre: info?.genre,
trackNumber: trackNumberTotal?.first ?? info?.track,
trackNumber: trackNumberTotal?.first ?? info?.track ?? audioStream?.tags?.track,
trackTotal: info?.trackTotal ?? (trackNumberTotal?.length == 2 ? trackNumberTotal?.last : null),
discNumber: discNumberTotal?.first ?? info?.disc,
discTotal: info?.discTotal ?? (discNumberTotal?.length == 2 ? discNumberTotal?.last : null),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 4.9.71-beta+250202178
version: 4.9.73-beta+250204354

environment:
sdk: ">=3.6.0 <4.0.0"
Expand Down

0 comments on commit 58c4568

Please sign in to comment.