Last active
April 28, 2024 08:31
-
-
Save PoomSmart/195e28fd8dda4722da538b0300ab0bd7 to your computer and use it in GitHub Desktop.
Sets the default video quality for videos on iOS YouTube.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <YouTubeHeader/MLAVPlayer.h> | |
#import <YouTubeHeader/MLHAMPlayerItem.h> | |
#import <YouTubeHeader/MLQuickMenuVideoQualitySettingFormatConstraint.h> | |
int targetResolution = 1440; | |
int targetFPS = 60; | |
static NSString *getClosestQualityLabel(NSArray <MLFormat *> *formats) { | |
int minDiff = INT_MAX; | |
int selectedFPS = 0; | |
NSString *closestQualityLabel; | |
for (MLFormat *format in formats) { | |
int height = [format height]; | |
int fps = [format FPS]; | |
int diff = abs(height - targetResolution); | |
if (diff < minDiff || (diff == minDiff && abs(fps - targetFPS) < abs(selectedFPS - targetFPS))) { | |
minDiff = diff; | |
selectedFPS = fps; | |
closestQualityLabel = [format qualityLabel]; | |
} | |
} | |
return closestQualityLabel; | |
} | |
static MLQuickMenuVideoQualitySettingFormatConstraint *getConstraint(NSString *qualityLabel) { | |
MLQuickMenuVideoQualitySettingFormatConstraint *constraint; | |
@try { | |
constraint = [[%c(MLQuickMenuVideoQualitySettingFormatConstraint) alloc] initWithVideoQualitySetting:3 formatSelectionReason:2 qualityLabel:qualityLabel]; | |
} @catch (id ex) { | |
constraint = [[%c(MLQuickMenuVideoQualitySettingFormatConstraint) alloc] initWithVideoQualitySetting:3 formatSelectionReason:2 qualityLabel:qualityLabel resolutionCap:0]; | |
} | |
return constraint; | |
} | |
%hook MLHAMPlayerItem | |
- (void)onSelectableVideoFormats:(NSArray <MLFormat *> *)formats { | |
%orig; | |
NSString *qualityLabel = getClosestQualityLabel(formats); | |
MLQuickMenuVideoQualitySettingFormatConstraint *constraint = getConstraint(qualityLabel); | |
self.videoFormatConstraint = constraint; | |
} | |
%end | |
%hook MLAVPlayer | |
- (void)streamSelectorHasSelectableVideoFormats:(NSArray <MLFormat *> *)formats { | |
%orig; | |
NSString *qualityLabel = getClosestQualityLabel(formats); | |
MLQuickMenuVideoQualitySettingFormatConstraint *constraint = getConstraint(qualityLabel); | |
self.videoFormatConstraint = constraint; | |
} | |
%end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment