Skip to content

Commit

Permalink
Implement #91
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrews54757 committed Mar 4, 2024
1 parent 9c72504 commit a0c74c1
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions chrome/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@
"options_general_targetspeed": {
"message": "Target download speed of predownloader"
},
"options_general_maxsize": {
"message": "Maximum size of predownloaded video"
},
"options_general_freeunused": {
"message": "Free unused channel buffers when switching quality levels"
},
Expand Down
3 changes: 3 additions & 0 deletions chrome/_locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@
"options_general_targetspeed": {
"message": "Velocidad de descarga objetivo del predescargador"
},
"options_general_maxsize": {
"message": "Tamaño máximo de video predescargado"
},
"options_general_freeunused": {
"message": "Libera los búferes de canal no utilizados al cambiar los niveles de calidad"
},
Expand Down
3 changes: 3 additions & 0 deletions chrome/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@
"options_general_targetspeed": {
"message": "プリダウンローダーのターゲット速度"
},
"options_general_maxsize": {
"message": "プリダウンローダーの最大サイズ"
},
"options_general_freeunused": {
"message": "品質レベル切り替え時に未使用のチャンネルバッファーを解放する"
},
Expand Down
3 changes: 3 additions & 0 deletions chrome/_locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@
"options_general_targetspeed": {
"message": "Целевая скорость предзагрузки"
},
"options_general_maxsize": {
"message": "Максимальный размер предзагружаемого видео"
},
"options_general_freeunused": {
"message": "Выгружать неиспользуемые буферы каналов при изменении качества видео"
},
Expand Down
9 changes: 8 additions & 1 deletion chrome/player/FastStreamClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class FastStreamClient extends EventEmitter {
this.options = {
autoPlay: false,
maxSpeed: -1,
maxSize: -1,
introCutoff: 5 * 60,
outroCutoff: 5 * 60,
bufferAhead: 120,
Expand Down Expand Up @@ -170,6 +171,7 @@ export class FastStreamClient extends EventEmitter {
this.options.freeUnusedChannels = options.freeUnusedChannels;
this.options.autoEnableBestSubtitles = options.autoEnableBestSubtitles;
this.options.maxSpeed = options.maxSpeed;
this.options.maxSize = options.maxSize;
this.options.seekStepSize = options.seekStepSize;
this.options.singleClickAction = options.singleClickAction;
this.options.doubleClickAction = options.doubleClickAction;
Expand Down Expand Up @@ -214,6 +216,8 @@ export class FastStreamClient extends EventEmitter {
this.options.toolSettings = options.toolSettings;
this.interfaceController.updateToolVisibility();
}

this.updateHasDownloadSpace();
}

updateCSSFilters() {
Expand Down Expand Up @@ -351,7 +355,10 @@ export class FastStreamClient extends EventEmitter {
this.hasDownloadSpace = false;
} else {
if (level.bitrate && this.duration) {
const storageAvailable = (this.storageAvailable * 8) * 0.6;
let storageAvailable = (this.storageAvailable * 8) * 0.6;
if (this.options.maxSize > 0) {
storageAvailable = Math.min(storageAvailable, this.options.maxSize * 8);
}
this.hasDownloadSpace = (level.bitrate * this.duration) < storageAvailable;
} else {
this.hasDownloadSpace = true;
Expand Down
1 change: 1 addition & 0 deletions chrome/player/options/defaults/DefaultOptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DefaultOptions = {
videoDaltonizerType: DaltonizerTypes.NONE,
videoDaltonizerStrength: 1,
maxSpeed: -1,
maxSize: -1,
seekStepSize: 2,
playbackRate: 1,
qualityMultiplier: 1.1,
Expand Down
5 changes: 5 additions & 0 deletions chrome/player/options/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ <h1 data-i18n="options_general_header"></h1>
<input class="number" type="text" id="maxspeed" data-i18n-label="options_general_targetspeed"></input>
</div>

<div class="option grid2" style="margin-left: 25px; margin-top: 3px">
<div class="label" data-i18n="options_general_maxsize"></div>
<input class="number" type="text" id="maxsize" data-i18n-label="options_general_maxsize"></input>
</div>

<br>

<div class="option grid1">
Expand Down
9 changes: 9 additions & 0 deletions chrome/player/options/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const keybindsList = document.getElementById('keybindslist');
const autoEnableURLSInput = document.getElementById('autoEnableURLs');
const autoSub = document.getElementById('autosub');
const maxSpeed = document.getElementById('maxspeed');
const maxSize = document.getElementById('maxsize');
const seekStepSize = document.getElementById('seekstepsize');
const playbackRate = document.getElementById('playbackrate');
const autoplayYoutube = document.getElementById('autoplayyt');
Expand Down Expand Up @@ -79,6 +80,7 @@ async function loadOptions(newOptions) {
autoSub.checked = !!Options.autoEnableBestSubtitles;
autoplayYoutube.checked = !!Options.autoplayYoutube;
maxSpeed.value = StringUtils.getSpeedString(Options.maxSpeed, true);
maxSize.value = StringUtils.getSizeString(Options.maxSize);
seekStepSize.value = Math.round(Options.seekStepSize * 100) / 100;
playbackRate.value = Options.playbackRate;
qualityMultiplier.value = Options.qualityMultiplier;
Expand Down Expand Up @@ -341,6 +343,13 @@ maxSpeed.addEventListener('change', () => {
optionChanged();
});

maxSize.addEventListener('change', () => {
// parse value, number unit
Options.maxSize = StringUtils.getSizeValue(maxSize.value);
maxSize.value = StringUtils.getSizeString(Options.maxSize);
optionChanged();
});

seekStepSize.addEventListener('change', () => {
Options.seekStepSize = parseFloat(seekStepSize.value);
optionChanged();
Expand Down
57 changes: 57 additions & 0 deletions chrome/player/utils/StringUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,63 @@ export class StringUtils {
return Math.round(value * 100) / 100 + ' ' + unit;
}

static getSizeString(size) {
if (size=== -1) {
return '∞ GB';
}

let unit = 'B';
if (size >= 1000) {
unit = 'KB';
size /= 1000;
}
if (size >= 1000) {
unit = 'MB';
size /= 1000;
}
if (size >= 1000) {
unit = 'GB';
size /= 1000;
}
if (size >= 1000) {
unit = 'TB';
size /= 1000;
}
return Math.round(size * 100) / 100 + ' ' + unit;
}

static getSizeValue(sizeStr) {
const float = parseFloat(sizeStr);
const unit = sizeStr.replace(float, '').trim();
if (
isNaN(float) ||
float < 0 ||
float === Infinity
) {
return -1;
}

// Unit can be MB, Mb, etc...
const match = unit.match(/([a-oq-zA-Z]+)/);
const unit1 = match?.[1];

let multiplier = 1;

// Convert to bytes
if (unit1) {
const sci = ['b', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'];
const split = unit1.split('');
if (sci.includes(split[0].toLowerCase())) {
multiplier *= 1000 ** sci.indexOf(split[0].toLowerCase());
} else {
// M default
multiplier *= 1000 ** 2;
}
}

return float * multiplier;
}

static parseHTTPRange(range) {
const match = range.match(/(\d+)-(\d+)?/);
if (!match) return [undefined, undefined];
Expand Down

0 comments on commit a0c74c1

Please sign in to comment.