Last active
December 8, 2024 09:11
-
-
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; | |
NSString *closestQualityLabel; | |
for (MLFormat *format in formats) { | |
int width = [format width]; | |
int height = [format height]; | |
int resolution = width > height ? height : width; | |
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