feat(YouTube): Add Force original audio
patch (#4122)
This commit is contained in:

committed by
GitHub

parent
1e1863446a
commit
f4aa440608
@ -0,0 +1,41 @@
|
||||
package app.revanced.extension.youtube.patches;
|
||||
|
||||
import app.revanced.extension.shared.Logger;
|
||||
import app.revanced.extension.youtube.settings.Settings;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class ForceOriginalAudioPatch {
|
||||
|
||||
private static final String DEFAULT_AUDIO_TRACKS_IDENTIFIER = "original";
|
||||
|
||||
/**
|
||||
* Injection point.
|
||||
*/
|
||||
public static boolean isDefaultAudioStream(boolean isDefault, String audioTrackId, String audioTrackDisplayName) {
|
||||
try {
|
||||
if (!Settings.FORCE_ORIGINAL_AUDIO.get()) {
|
||||
return isDefault;
|
||||
}
|
||||
|
||||
if (audioTrackDisplayName.isEmpty()) {
|
||||
// Older app targets can have empty audio tracks and these might be placeholders.
|
||||
// The real audio tracks are called after these.
|
||||
return isDefault;
|
||||
}
|
||||
|
||||
Logger.printDebug(() -> "default: " + String.format("%-5s", isDefault) + " id: "
|
||||
+ String.format("%-8s", audioTrackId) + " name:" + audioTrackDisplayName);
|
||||
|
||||
final boolean isOriginal = audioTrackDisplayName.contains(DEFAULT_AUDIO_TRACKS_IDENTIFIER);
|
||||
if (isOriginal) {
|
||||
Logger.printDebug(() -> "Using audio: " + audioTrackId);
|
||||
}
|
||||
|
||||
return isOriginal;
|
||||
} catch (Exception ex) {
|
||||
Logger.printException(() -> "isDefaultAudioStream failure", ex);
|
||||
}
|
||||
|
||||
return isDefault;
|
||||
}
|
||||
}
|
@ -52,6 +52,8 @@ public class Settings extends BaseSettings {
|
||||
public static final FloatSetting PLAYBACK_SPEED_DEFAULT = new FloatSetting("revanced_playback_speed_default", -2.0f);
|
||||
public static final StringSetting CUSTOM_PLAYBACK_SPEEDS = new StringSetting("revanced_custom_playback_speeds",
|
||||
"0.25\n0.5\n0.75\n0.9\n0.95\n1.0\n1.05\n1.1\n1.25\n1.5\n1.75\n2.0\n3.0\n4.0\n5.0", true);
|
||||
// Audio
|
||||
public static final BooleanSetting FORCE_ORIGINAL_AUDIO = new BooleanSetting("revanced_force_original_audio", FALSE);
|
||||
|
||||
// Ads
|
||||
public static final BooleanSetting HIDE_BUTTONED_ADS = new BooleanSetting("revanced_hide_buttoned_ads", TRUE);
|
||||
|
@ -48,35 +48,44 @@ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment {
|
||||
|
||||
/**
|
||||
* Sorts a preference list by menu entries, but preserves the first value as the first entry.
|
||||
*
|
||||
* @noinspection SameParameterValue
|
||||
*/
|
||||
private static void sortListPreferenceByValues(ListPreference listPreference) {
|
||||
private static void sortListPreferenceByValues(ListPreference listPreference, int firstEntriesToPreserve) {
|
||||
CharSequence[] entries = listPreference.getEntries();
|
||||
CharSequence[] entryValues = listPreference.getEntryValues();
|
||||
final int entrySize = entries.length;
|
||||
|
||||
if (entrySize != entryValues.length) {
|
||||
// Xml array declaration has a missing/extra entry.
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
// Ensure the first entry remains the first after sorting.
|
||||
CharSequence firstEntry = entries[0];
|
||||
CharSequence firstEntryValue = entryValues[0];
|
||||
List<Pair<String, String>> firstPairs = new ArrayList<>(firstEntriesToPreserve);
|
||||
List<Pair<String, String>> pairsToSort = new ArrayList<>(entrySize);
|
||||
|
||||
List<Pair<String, String>> entryPairs = new ArrayList<>(entrySize);
|
||||
for (int i = 1; i < entrySize; i++) {
|
||||
entryPairs.add(new Pair<>(entries[i].toString(), entryValues[i].toString()));
|
||||
for (int i = 0; i < entrySize; i++) {
|
||||
Pair<String, String> pair = new Pair<>(entries[i].toString(), entryValues[i].toString());
|
||||
if (i < firstEntriesToPreserve) {
|
||||
firstPairs.add(pair);
|
||||
} else {
|
||||
pairsToSort.add(pair);
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(entryPairs, (pair1, pair2) -> pair1.first.compareToIgnoreCase(pair2.first));
|
||||
Collections.sort(pairsToSort, (pair1, pair2) -> pair1.first.compareToIgnoreCase(pair2.first));
|
||||
|
||||
CharSequence[] sortedEntries = new CharSequence[entrySize];
|
||||
CharSequence[] sortedEntryValues = new CharSequence[entrySize];
|
||||
|
||||
sortedEntries[0] = firstEntry;
|
||||
sortedEntryValues[0] = firstEntryValue;
|
||||
int i = 0;
|
||||
for (Pair<String, String> pair : firstPairs) {
|
||||
sortedEntries[i] = pair.first;
|
||||
sortedEntryValues[i] = pair.second;
|
||||
i++;
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
for (Pair<String, String> pair : entryPairs) {
|
||||
for (Pair<String, String> pair : pairsToSort) {
|
||||
sortedEntries[i] = pair.first;
|
||||
sortedEntryValues[i] = pair.second;
|
||||
i++;
|
||||
@ -102,7 +111,7 @@ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment {
|
||||
|
||||
preference = findPreference(Settings.SPOOF_VIDEO_STREAMS_LANGUAGE.key);
|
||||
if (preference instanceof ListPreference languagePreference) {
|
||||
sortListPreferenceByValues(languagePreference);
|
||||
sortListPreferenceByValues(languagePreference, 1);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.printException(() -> "initialize failure", ex);
|
||||
|
Reference in New Issue
Block a user