Skip to content

Instantly share code, notes, and snippets.

@A1ex-N
Last active April 28, 2023 01:49
Show Gist options
  • Save A1ex-N/9521f90d91025cafc5fdc4c31164a821 to your computer and use it in GitHub Desktop.
Save A1ex-N/9521f90d91025cafc5fdc4c31164a821 to your computer and use it in GitHub Desktop.
tampermonkey script to calculate video bitrate on rarbg.to
// ==UserScript==
// @name rarbg.to bitrate calculator
// @namespace http://tampermonkey.net/
// @version 0.2
// @description does a rough estimation of video bitrate based on filesize and runtime, and appends it to the "Size" element
// @author https://github.com/A1ex-N
// @match https://rarbg.to/torrent/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=rarbg.to
// @grant none
// ==/UserScript==
function findElements() {
const infoTableSelector = '.lista-rounded > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > div:nth-child(1) > table:nth-child(1) > tbody:nth-child(1)';
const torrentDescriptionTable = document.querySelector(infoTableSelector).children;
let elements = {
sizeElement: {
headerEl: null,
textEl: null,
strippedText: null, // size text without MB/GB
// if isGigabytes is false we assume it's megabytes, even though it could be KB or even TB
isGigabytes: false,
},
runtimeElement: {
headerEl: null,
textEl: null,
},
};
for (const tableRow of torrentDescriptionTable) {
if (tableRow.children.length != 0) {
let headerElement = tableRow.children[0];
let textElement = tableRow.children[1];
switch (headerElement.innerText) {
case "Size:":
elements.sizeElement.headerEl = headerElement;
elements.sizeElement.textEl = textElement;
elements.sizeElement.strippedText = textElement.innerText.replace(RegExp(" (MB)|(GB)"), "");
if (textElement.innerText.includes("GB")) { elements.sizeElement.isGigabytes = true }
break;
case "IMDB Runtime:":
elements.runtimeElement.headerEl = headerElement;
elements.runtimeElement.textEl = textElement;
break;
}
}
}
return elements;
}
(function () {
'use strict';
let elements = findElements();
let rawSize = elements.sizeElement.strippedText;
var sizeInMegabytes;
if (elements.sizeElement.isGigabytes) {
sizeInMegabytes = parseFloat(rawSize) * 1024;
} else {
sizeInMegabytes = parseFloat(rawSize);
}
if (elements.runtimeElement.headerEl === null) {
console.error("[rarbg.to bitrate calculator] Cannot find runtime, therefore the bitrate cannot be calculated");
elements.sizeElement.textEl.innerText += " [Unable to calculate bitrate]";
return;
}
let runtimeInMinutes = elements.runtimeElement.textEl.innerText;
let runtimeInSeconds = parseInt(runtimeInMinutes * 60);
let bitrateInMegabytes = (sizeInMegabytes / runtimeInSeconds).toFixed(2);
let bitrateInMegabits = bitrateInMegabytes * 8;
elements.sizeElement.textEl.innerText += ` [Bitrate: ~${bitrateInMegabits}Mb/s (probably a bit less than reported)]`;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment