feat: localized audio track names

This commit is contained in:
ThetaDev 2023-03-19 03:06:29 +01:00
parent 366c39d4c6
commit 87a88e4df7
6 changed files with 76 additions and 30 deletions

View file

@ -13,6 +13,7 @@ import androidx.preference.PreferenceManager;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.stream.AudioStream;
import org.schabi.newpipe.extractor.stream.AudioTrackType;
import org.schabi.newpipe.extractor.stream.DeliveryMethod;
import org.schabi.newpipe.extractor.stream.Stream;
import org.schabi.newpipe.extractor.stream.VideoStream;
@ -244,8 +245,14 @@ public final class ListHelper {
final Comparator<AudioStream> trackCmp =
getAudioTrackComparator(preferredLanguageOrEnglish, preferDescriptiveAudio);
// Filter unknown audio tracks if there are multiple tracks
java.util.stream.Stream<AudioStream> cs = collectedStreams.values().stream();
if (collectedStreams.size() > 1) {
cs = cs.filter(s -> s.getAudioTrackId() != null);
}
// Sort collected streams
return collectedStreams.values().stream().sorted(trackCmp).collect(Collectors.toList());
return cs.sorted(trackCmp).collect(Collectors.toList());
}
/*//////////////////////////////////////////////////////////////////////////
@ -672,7 +679,7 @@ public final class ListHelper {
return Comparator.comparing(AudioStream::getAudioLocale, (o1, o2) -> Boolean.compare(
o1 == null || !o1.getISO3Language().equals(preferredLanguage),
o2 == null || !o2.getISO3Language().equals(preferredLanguage))
).thenComparing(AudioStream::isDescriptive, (o1, o2) ->
).thenComparing(s -> s.getAudioTrackType() == AudioTrackType.DESCRIPTIVE, (o1, o2) ->
Boolean.compare(o1 ^ preferDescriptiveAudio, o2 ^ preferDescriptiveAudio)
).thenComparing(AudioStream::getAudioTrackName, (o1, o2) -> {
if (o1 != null) {

View file

@ -21,6 +21,7 @@ import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.stream.AudioStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
@ -261,6 +262,47 @@ public final class Localization {
}
}
/**
* Get the localized name of an audio track.
* <p>Example:</p>
* <p>English (original)</p>
* <p>English (descriptive)</p>
* <p>Spanish (dubbed)</p>
*
* @param context used to get app language
* @param track a {@link AudioStream} of the track
* @return localized track name
*/
public static String audioTrackName(final Context context, final AudioStream track) {
String res;
if (track.getAudioLocale() != null) {
res = track.getAudioLocale().getDisplayLanguage(getAppLocale(context));
} else if (track.getAudioTrackName() != null) {
res = track.getAudioTrackName();
} else {
res = context.getString(R.string.unknown_audio_track);
}
if (track.getAudioTrackType() != null) {
res += " (";
switch (track.getAudioTrackType()) {
case ORIGINAL:
res += context.getString(R.string.track_type_original);
break;
case DUBBED:
res += context.getString(R.string.track_type_dubbed);
break;
case DESCRIPTIVE:
res += context.getString(R.string.track_type_descriptive);
break;
}
res += ")";
}
return res;
}
/*//////////////////////////////////////////////////////////////////////////
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/