Skip to content

Instantly share code, notes, and snippets.

@PoomSmart
Last active April 28, 2024 08:31
Show Gist options
  • Save PoomSmart/195e28fd8dda4722da538b0300ab0bd7 to your computer and use it in GitHub Desktop.
Save PoomSmart/195e28fd8dda4722da538b0300ab0bd7 to your computer and use it in GitHub Desktop.
Sets the default video quality for videos on iOS YouTube.
#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