Skip to content

Commit

Permalink
implement yt-dlp rate limit option in settings (closes #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylon committed Mar 24, 2023
1 parent 1112da2 commit b4412ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
14 changes: 14 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,19 @@ const SharedownAPI = (() => {
videoProgBar.style.width = perc > 100 ? '100%' : `${perc}%`;
}

function _setYtdlpRateLimit(unit, value, args) {
let v = parseInt(value, 10);

if (v === 0 || isNaN(v))
return;
else if (unit === 'm')
v *= 1024 * 1024;
else if (unit === 'k')
v *= 1024;

args.push('-r', v);
}

function _saveYtdlpTempFragsFolder(tmpPath, filename) {
try {
if (!_fs.existsSync(tmpPath)) {
Expand Down Expand Up @@ -1027,6 +1040,7 @@ const SharedownAPI = (() => {
args.push('-N', settings.directN.toString(), '--add-header', cookieH, '-o', outFile, vurl);
}

_setYtdlpRateLimit(settings.ytdlpRateLimitU, settings.ytdlpRateLimit, args);
videoProgBar.setAttribute('data-tmp-perc', '0');
_stoppingProcess = false;

Expand Down
21 changes: 21 additions & 0 deletions sharedown/sharedown.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ <h5 class="modal-title">Sharedown settings</h5>
</div>
</div>
</div>
<div class="row mb-3 downldr-opt yt-dlp-opt d-none">
<div class="col-12">
<div class="mb-3">
<label class="form-label">Download rate limit</label>
<div class="input-group">
<div class="input-group-text">
<div class="form-check form-check-inline">
<input name="ratelimunitradio" id="mbunitlim" checked class="form-check-input" type="radio" value="m" aria-label="Radio button to set rate limit in megabyte for following rate limit input">
<label class="form-check-label" for="mbunitlim">Mb</label>
</div>
<div class="form-check form-check-inline">
<input name="ratelimunitradio" id="kbunitlim" class="form-check-input" type="radio" value="k" aria-label="Radio button to set rate limit in kilobyte for following rate limit input">
<label class="form-check-label" for="kbunitlim">Kb</label>
</div>
</div>
<input id="ytdlprl" type="number" class="form-control" aria-label="Download rate limit input with radio button" placeholder="0 (default, unlimited)" min="0">
</div>
<div id="ytdlprlhelp" class="form-text">Exact value may not be respected, try lower if it is the case</div>
</div>
</div>
</div>
<div class="row mb-3 downldr-opt direct-opt d-none">
<div class="col-12">
<div class="mb-3">
Expand Down
12 changes: 10 additions & 2 deletions sharedown/sharedown.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const sharedownApi = window.sharedown;

const globalSettings = {
_version: 16, // internal
_version: 17, // internal
outputPath: '',
downloader: 'yt-dlp',
ytdlpTmpOut: '',
Expand All @@ -34,7 +34,9 @@ const globalSettings = {
autoSaveState: true,
logging: false,
customChromePath: '',
keepBrowserOpen: false
keepBrowserOpen: false,
ytdlpRateLimit: 0,
ytdlpRateLimitU: 'm'
};

const resources = {
Expand Down Expand Up @@ -338,6 +340,8 @@ async function loadGlobalSettings() {
ytdlpTmpOutD.value = globalSettings.ytdlpTmpOut;
resources.globalSetModal.querySelector('#shddownloader').value = globalSettings.downloader;
resources.globalSetModal.querySelector('#ytdlpn').value = globalSettings.ytdlpN;
resources.globalSetModal.querySelector('#ytdlprl').value = globalSettings.ytdlpRateLimit;
resources.globalSetModal.querySelector(`#${globalSettings.ytdlpRateLimitU}bunitlim`).checked = true;
resources.globalSetModal.querySelector('#keeptmponfail').checked = globalSettings.keepYtdlpTmpOnFail;
resources.globalSetModal.querySelector('#directn').value = globalSettings.directN;
resources.globalSetModal.querySelector('#keytar').checked = globalSettings.useKeytar;
Expand Down Expand Up @@ -372,6 +376,8 @@ async function saveGlobalSettings() {
globalSettings.retryOnFail = resources.globalSetModal.querySelector('#retryonfail').checked;
globalSettings.downloader = resources.globalSetModal.querySelector('#shddownloader').value;
globalSettings.ytdlpN = Utils.getYtdlpNVal(resources.globalSetModal.querySelector('#ytdlpn').value);
globalSettings.ytdlpRateLimit = resources.globalSetModal.querySelector('#ytdlprl').value;
globalSettings.ytdlpRateLimitU = resources.globalSetModal.querySelector('input[name="ratelimunitradio"]:checked').value;
globalSettings.keepYtdlpTmpOnFail = resources.globalSetModal.querySelector('#keeptmponfail').checked;
globalSettings.directN = Utils.getYtdlpNVal(resources.globalSetModal.querySelector('#directn').value);
globalSettings.timeout = isNaN(timeout) || timeout < 0 ? 60 : timeout;
Expand Down Expand Up @@ -415,6 +421,8 @@ function importAppSettings() {
globalSettings.retryOnFail = data.retryOnFail ?? false;
globalSettings.downloader = data.downloader ?? 'yt-dlp';
globalSettings.ytdlpN = Utils.getYtdlpNVal(data.ytdlpN ?? 5);
globalSettings.ytdlpRateLimit = data.ytdlpRateLimit ?? 0;
globalSettings.ytdlpRateLimitU = data.ytdlpRateLimitU ?? 'm';
globalSettings.keepYtdlpTmpOnFail = data.keepYtdlpTmpOnFail ?? false;
globalSettings.directN = Utils.getYtdlpNVal(data.directN ?? 5);
globalSettings.timeout = data.timeout ?? 60;
Expand Down

0 comments on commit b4412ad

Please sign in to comment.