From cdb79ef78a54e57121e5627ea03dbaaa6c52b1ba Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:15:36 +0200 Subject: [PATCH 1/3] Make isLimitingDataUsage method of ListHelper package-private and fix some typos in the class --- .../java/org/schabi/newpipe/util/ListHelper.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index 5918ece25..90e344fae 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -46,10 +46,10 @@ public final class ListHelper { List.of(MediaFormat.MP3, MediaFormat.M4A, MediaFormat.WEBMA); // Use a Set for better performance private static final Set HIGH_RESOLUTION_LIST = Set.of("1440p", "2160p"); - // Audio track types in order of priotity. 0=lowest, n=highest + // Audio track types in order of priority. 0=lowest, n=highest private static final List AUDIO_TRACK_TYPE_RANKING = List.of(AudioTrackType.DESCRIPTIVE, AudioTrackType.DUBBED, AudioTrackType.ORIGINAL); - // Audio track types in order of priotity when descriptive audio is preferred. + // Audio track types in order of priority when descriptive audio is preferred. private static final List AUDIO_TRACK_TYPE_RANKING_DESCRIPTIVE = List.of(AudioTrackType.ORIGINAL, AudioTrackType.DUBBED, AudioTrackType.DESCRIPTIVE); @@ -689,7 +689,7 @@ public final class ListHelper { } } - private static boolean isLimitingDataUsage(final Context context) { + static boolean isLimitingDataUsage(@NonNull final Context context) { return getResolutionLimit(context) != null; } @@ -731,7 +731,7 @@ public final class ListHelper { /** * Get a {@link Comparator} to compare {@link AudioStream}s by their format and bitrate. * - *

The prefered stream will be ordered last.

+ *

The preferred stream will be ordered last.

* * @param context app context * @return Comparator @@ -746,7 +746,7 @@ public final class ListHelper { /** * Get a {@link Comparator} to compare {@link AudioStream}s by their format and bitrate. * - *

The prefered stream will be ordered last.

+ *

The preferred stream will be ordered last.

* * @param defaultFormat the default format to look for * @param limitDataUsage choose low bitrate audio stream @@ -788,7 +788,7 @@ public final class ListHelper { *
  • Language is English
  • * * - *

    The prefered track will be ordered last.

    + *

    The preferred track will be ordered last.

    * * @param context App context * @return Comparator @@ -825,7 +825,7 @@ public final class ListHelper { *
  • Language is English
  • * * - *

    The prefered track will be ordered last.

    + *

    The preferred track will be ordered last.

    * * @param preferredLanguage Preferred audio stream language * @param preferOriginalAudio Get the original audio track regardless of its language From 77bbbc88f8e1cefca0a4d2ec9b1a6a0c6a592637 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:23:00 +0200 Subject: [PATCH 2/3] Use ListHelper to get secondary audio streams for video-only streams Instead of searching for the first audio stream matching a compatible media format, this change makes SecondaryStreamHelper.getAudioStreamFor use methods isLimitingDataUsage, getAudioFormatComparator and getAudioIndexByHighestRank of ListHelper to get an audio stream which can be muxed into a video-only stream, if available. This allows users to download videos with the highest audio quality available if no resolution limit on mobile data usage has been set. The order of formats used to search a compatible audio stream has been kept. --- .../newpipe/download/DownloadDialog.java | 4 +- .../newpipe/util/SecondaryStreamHelper.java | 46 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 9e9909e85..2e0a421da 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -267,8 +267,8 @@ public class DownloadDialog extends DialogFragment if (!videoStreams.get(i).isVideoOnly()) { continue; } - final AudioStream audioStream = SecondaryStreamHelper - .getAudioStreamFor(audioStreams.getStreamsList(), videoStreams.get(i)); + final AudioStream audioStream = SecondaryStreamHelper.getAudioStreamFor( + context, audioStreams.getStreamsList(), videoStreams.get(i)); if (audioStream != null) { secondaryStreams.append(i, new SecondaryStreamHelper<>(audioStreams, audioStream)); diff --git a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java index 9415135cf..42a6ca361 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java @@ -1,5 +1,7 @@ package org.schabi.newpipe.util; +import android.content.Context; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -9,6 +11,7 @@ import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.util.StreamItemAdapter.StreamInfoWrapper; +import java.util.Comparator; import java.util.List; public class SecondaryStreamHelper { @@ -27,12 +30,14 @@ public class SecondaryStreamHelper { /** * Find the correct audio stream for the desired video stream. * + * @param context Android context * @param audioStreams list of audio streams * @param videoStream desired video ONLY stream * @return selected audio stream or null if a candidate was not found */ @Nullable - public static AudioStream getAudioStreamFor(@NonNull final List audioStreams, + public static AudioStream getAudioStreamFor(@NonNull final Context context, + @NonNull final List audioStreams, @NonNull final VideoStream videoStream) { final MediaFormat mediaFormat = videoStream.getFormat(); if (mediaFormat == null) { @@ -41,33 +46,36 @@ public class SecondaryStreamHelper { switch (mediaFormat) { case WEBM: - case MPEG_4:// ¿is mpeg-4 DASH? + case MPEG_4: // Is MPEG-4 DASH? break; default: return null; } - final boolean m4v = (mediaFormat == MediaFormat.MPEG_4); + final boolean m4v = mediaFormat == MediaFormat.MPEG_4; + final boolean isLimitingDataUsage = ListHelper.isLimitingDataUsage(context); - for (final AudioStream audio : audioStreams) { - if (audio.getFormat() == (m4v ? MediaFormat.M4A : MediaFormat.WEBMA)) { - return audio; + Comparator comparator = ListHelper.getAudioFormatComparator( + m4v ? MediaFormat.M4A : MediaFormat.WEBMA, isLimitingDataUsage); + int preferredAudioStreamIndex = ListHelper.getAudioIndexByHighestRank( + audioStreams, comparator); + + if (preferredAudioStreamIndex == -1) { + if (m4v) { + return null; + } + + comparator = ListHelper.getAudioFormatComparator( + MediaFormat.WEBMA_OPUS, isLimitingDataUsage); + preferredAudioStreamIndex = ListHelper.getAudioIndexByHighestRank( + audioStreams, comparator); + + if (preferredAudioStreamIndex == -1) { + return null; } } - if (m4v) { - return null; - } - - // retry, but this time in reverse order - for (int i = audioStreams.size() - 1; i >= 0; i--) { - final AudioStream audio = audioStreams.get(i); - if (audio.getFormat() == MediaFormat.WEBMA_OPUS) { - return audio; - } - } - - return null; + return audioStreams.get(preferredAudioStreamIndex); } public T getStream() { From ce30108efc917ab7edb0b90ac8b44d5efbb7180b Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 7 Dec 2023 16:40:32 +0100 Subject: [PATCH 3/3] Improve javadoc for `getAudioStreamFor` --- .../org/schabi/newpipe/util/SecondaryStreamHelper.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java index 42a6ca361..75d9a3892 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java @@ -28,12 +28,15 @@ public class SecondaryStreamHelper { } /** - * Find the correct audio stream for the desired video stream. + * Finds an audio stream compatible with the provided video-only stream, so that the two streams + * can be combined in a single file by the downloader. If there are multiple available audio + * streams, chooses either the highest or the lowest quality one based on + * {@link ListHelper#isLimitingDataUsage(Context)}. * * @param context Android context * @param audioStreams list of audio streams - * @param videoStream desired video ONLY stream - * @return selected audio stream or null if a candidate was not found + * @param videoStream desired video-ONLY stream + * @return the selected audio stream or null if a candidate was not found */ @Nullable public static AudioStream getAudioStreamFor(@NonNull final Context context,