Skip to content

Instantly share code, notes, and snippets.

@PoomSmart
Last active October 23, 2025 12:56
Show Gist options
  • Select an option

  • Save PoomSmart/195e28fd8dda4722da538b0300ab0bd7 to your computer and use it in GitHub Desktop.

Select an option

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;
NSString *closestQualityLabel;
for (MLFormat *format in formats) {
int resolution = [format singleDimensionResolution];
int fps = [format FPS];
int resolutionDiff = abs(resolution - targetResolution);
int fpsDiff = abs(fps - targetFPS);
int totalDiff = resolutionDiff + fpsDiff;
if (totalDiff < minDiff) {
minDiff = totalDiff;
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