Commit 03f2dd22 authored by sebaro's avatar sebaro

Fix YouTube, Add VIKI HD videos

parent fee19149
......@@ -3,6 +3,10 @@
/* SaveTube Changelog */
#2019.06.21
- fixes YouTube
- adds VIKI HD videos (DASH and HLS) and subtitles
#2019.06.05
- fixes YouTube
- fixes Veoh
......
......@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "SaveTube",
"version": "2019.6.5",
"version": "2019.6.21",
"description": "Download videos from video sharing web sites.",
......
// ==UserScript==
// @name SaveTube
// @version 2019.06.05
// @version 2019.06.21
// @description Download videos from video sharing web sites.
// @author sebaro
// @namespace http://sebaro.pro/savetube
......@@ -418,31 +418,15 @@ function getMyVideo() {
else {
if (saver['videoList'][saver['videoSave']] == 'DASH') {
if (saver['videoSave'].indexOf('MP4') != -1) {
var vdoV = saver['videoList'][saver['videoSave'].replace(/MP4/, 'Video MP4')];
if (saver['videoList']['High Bitrate Audio MP4']) {
var vdoA = saver['videoList']['High Bitrate Audio MP4'];
}
else if (saver['videoList']['Medium Bitrate Audio MP4']) {
var vdoA = saver['videoList']['Medium Bitrate Audio MP4'];
}
else {
var vdoA = saver['videoList']['Low Bitrate Audio MP4'];
}
var vdoV = saver['videoList'][saver['videoSave'].replace('MP4', 'Video MP4')];
var vdoA = saver['videoList'][saver['videoSave'].replace('MP4', 'Audio MP4')] || saver['videoList']['High Bitrate Audio MP4'] || saver['videoList']['Medium Bitrate Audio MP4'];
}
else {
var vdoV = saver['videoList'][saver['videoSave'].replace(/WebM/, 'Video WebM')];
if (saver['videoList']['High Bitrate Audio Opus']) {
var vdoA = saver['videoList']['High Bitrate Audio Opus'];
}
else if (saver['videoList']['Medium Bitrate Audio Opus']) {
var vdoA = saver['videoList']['Medium Bitrate Audio Opus'];
}
else {
var vdoA = saver['videoList']['Low Bitrate Audio Opus'];
}
var vdoV = saver['videoList'][saver['videoSave'].replace('WebM', 'Video WebM')];
var vdoA = saver['videoList']['High Bitrate Audio Opus'] || saver['videoList']['Medium Bitrate Audio Opus'] || saver['videoList']['Low Bitrate Audio Opus'];
}
var vdoT = (vdoTle) ? vdoTle + vdoDef : page.site + vdoDef;
vdoURL = 'savetube:' + vdoT + '=SAVETUBE=' + vdoV + '=SAVETUBE=' + vdoA;
vdoURL = 'savetube:' + vdoT + 'SEPARATOR' + vdoV + 'SEPARATOR' + vdoA;
page.win.location.href = vdoURL;
}
else {
......@@ -563,13 +547,8 @@ function SaveTube() {
if (page.url.indexOf('youtube.com/watch') != -1) {
/* Redirect Categories */
if (page.url.indexOf('gaming.youtube.com') != -1) {
page.win.location.href = page.url.replace('gaming', 'www');
}
/* Video Availability */
if (getMyContent(page.url, '"simpleText":"(This video is (private|unavailable|not available).)"', false)) return;
if (getMyContent(page.url, '"playabilityStatus":\\{"status":"(ERROR|UNPLAYABLE)"', false)) return;
var ytVideoUnavailable = getMyElement('', 'div', 'id', 'player-unavailable', -1, false);
if (ytVideoUnavailable) {
if (ytVideoUnavailable.className.indexOf('hid') == -1) {
......@@ -588,7 +567,7 @@ function SaveTube() {
var ytSignFuncName, ytSignFuncBody, ytSwapFuncName, ytSwapFuncBody, ytFuncMatch;
ytScriptSrc = ytScriptSrc.replace(/(\r\n|\n|\r)/gm, '');
ytSignFuncName = ytScriptSrc.match(/"signature"\s*,\s*([^\)]*?)\(/);
if (!ytSignFuncName) ytSignFuncName = ytScriptSrc.match(/d.set\(b,(?:encodeURIComponent\()?.*?([a-zA-Z0-9$]+)\(/);
if (!ytSignFuncName) ytSignFuncName = ytScriptSrc.match(/c&&.\.set\(b,(?:encodeURIComponent\()?.*?([$a-zA-Z0-9]+)\(/);
ytSignFuncName = (ytSignFuncName) ? ytSignFuncName[1] : null;
if (ytSignFuncName) {
ytFuncMatch = ytSignFuncName.replace(/\$/, '\\$') + '\\s*=\\s*function\\s*' + '\\s*\\(\\w+\\)\\s*\\{(.*?)\\}';
......@@ -852,7 +831,7 @@ function SaveTube() {
/* Get Videos */
var ytVideoList = {};
if (ytVideosContent) {
if (ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) {
if (ytVideosContent.match(/^s=/) || ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) {
var ytScriptURL = getMyContent(page.url, '"js":\\s*"(.*?)"', true);
if (!ytScriptURL) ytScriptURL = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"js":\\s*"(.*?)"', true);
if (ytScriptURL) {
......@@ -890,6 +869,10 @@ function SaveTube() {
else if (page.url.indexOf('dailymotion.com/video') != -1) {
/* Video Availability */
if (getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"error":\\{"title":"(.*?)"', false)) return;
if (getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"error_title":"(.*?)"', false)) return;
/* Get Video Title */
var dmVideoTitle = getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"title":"(.*?)"', false);
if (dmVideoTitle) {
......@@ -978,7 +961,8 @@ function SaveTube() {
/* Get Videos */
if (viVideosContent) {
var viVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4', '360p': 'Low Definition MP4', '270p': 'Very Low Definition MP4'};
var viVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4',
'360p': 'Low Definition MP4', '270p': 'Very Low Definition MP4'};
var viVideoList = {};
var viVideoFound = false;
var viVideo, myVideoCode;
......@@ -1164,47 +1148,43 @@ function SaveTube() {
/* Get Videos Content */
var vkVideosContent;
if (vkVideoID) {
/*
A JavaScript implementation of the SHA family of hashes, as
defined in FIPS PUB 180-4 and FIPS PUB 202, as well as the corresponding
HMAC implementation as defined in FIPS PUB 198a
Copyright Brian Turek 2008-2017
/* SHA-1
Copyright 2008-2018 Brian Turek, 1998-2009 Paul Johnston & Contributors
Distributed under the BSD License
See http://caligatio.github.com/jsSHA/ for more information
Several functions taken from Paul Johnston
See https://caligatio.github.com/jsSHA/ for more information
*/
'use strict';(function(G){function r(d,b,c){var h=0,a=[],f=0,g,m,k,e,l,p,q,t,w=!1,n=[],u=[],v,r=!1;c=c||{};g=c.encoding||"UTF8";v=c.numRounds||1;if(v!==parseInt(v,10)||1>v)throw Error("numRounds must a integer >= 1");if("SHA-1"===d)l=512,p=z,q=H,e=160,t=function(a){return a.slice()};else throw Error("Chosen SHA variant is not supported");k=A(b,g);m=x(d);this.setHMACKey=function(a,f,b){var c;if(!0===w)throw Error("HMAC key already set");if(!0===r)throw Error("Cannot set HMAC key after calling update");
g=(b||{}).encoding||"UTF8";f=A(f,g)(a);a=f.binLen;f=f.value;c=l>>>3;b=c/4-1;if(c<a/8){for(f=q(f,a,0,x(d),e);f.length<=b;)f.push(0);f[b]&=4294967040}else if(c>a/8){for(;f.length<=b;)f.push(0);f[b]&=4294967040}for(a=0;a<=b;a+=1)n[a]=f[a]^909522486,u[a]=f[a]^1549556828;m=p(n,m);h=l;w=!0};this.update=function(b){var e,g,c,d=0,q=l>>>5;e=k(b,a,f);b=e.binLen;g=e.value;e=b>>>5;for(c=0;c<e;c+=q)d+l<=b&&(m=p(g.slice(c,c+q),m),d+=l);h+=d;a=g.slice(d>>>5);f=b%l;r=!0};this.getHash=function(b,g){var c,k,l,p;if(!0===
w)throw Error("Cannot call getHash after setting HMAC key");l=B(g);switch(b){case "HEX":c=function(a){return C(a,e,l)};break;case "B64":c=function(a){return D(a,e,l)};break;case "BYTES":c=function(a){return E(a,e)};break;case "ARRAYBUFFER":try{k=new ArrayBuffer(0)}catch(I){throw Error("ARRAYBUFFER not supported by this environment");}c=function(a){return F(a,e)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER");}p=q(a.slice(),f,h,t(m),e);for(k=1;k<v;k+=1)p=q(p,e,0,x(d),e);
return c(p)};this.getHMAC=function(b,g){var c,k,n,r;if(!1===w)throw Error("Cannot call getHMAC without first setting HMAC key");n=B(g);switch(b){case "HEX":c=function(a){return C(a,e,n)};break;case "B64":c=function(a){return D(a,e,n)};break;case "BYTES":c=function(a){return E(a,e)};break;case "ARRAYBUFFER":try{c=new ArrayBuffer(0)}catch(I){throw Error("ARRAYBUFFER not supported by this environment");}c=function(a){return F(a,e)};break;default:throw Error("outputFormat must be HEX, B64, BYTES, or ARRAYBUFFER");
}k=q(a.slice(),f,h,t(m),e);r=p(u,x(d));r=q(k,e,l,r,e);return c(r)}}function C(d,b,c){var h="";b/=8;var a,f;for(a=0;a<b;a+=1)f=d[a>>>2]>>>8*(3+a%4*-1),h+="0123456789abcdef".charAt(f>>>4&15)+"0123456789abcdef".charAt(f&15);return c.outputUpper?h.toUpperCase():h}function D(d,b,c){var h="",a=b/8,f,g,m;for(f=0;f<a;f+=3)for(g=f+1<a?d[f+1>>>2]:0,m=f+2<a?d[f+2>>>2]:0,m=(d[f>>>2]>>>8*(3+f%4*-1)&255)<<16|(g>>>8*(3+(f+1)%4*-1)&255)<<8|m>>>8*(3+(f+2)%4*-1)&255,g=0;4>g;g+=1)8*f+6*g<=b?h+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(m>>>
6*(3-g)&63):h+=c.b64Pad;return h}function E(d,b){var c="",h=b/8,a,f;for(a=0;a<h;a+=1)f=d[a>>>2]>>>8*(3+a%4*-1)&255,c+=String.fromCharCode(f);return c}function F(d,b){var c=b/8,h,a=new ArrayBuffer(c),f;f=new Uint8Array(a);for(h=0;h<c;h+=1)f[h]=d[h>>>2]>>>8*(3+h%4*-1)&255;return a}function B(d){var b={outputUpper:!1,b64Pad:"=",shakeLen:-1};d=d||{};b.outputUpper=d.outputUpper||!1;!0===d.hasOwnProperty("b64Pad")&&(b.b64Pad=d.b64Pad);if("boolean"!==typeof b.outputUpper)throw Error("Invalid outputUpper formatting option");
if("string"!==typeof b.b64Pad)throw Error("Invalid b64Pad formatting option");return b}function A(d,b){var c;switch(b){case "UTF8":case "UTF16BE":case "UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE");}switch(d){case "HEX":c=function(b,a,f){var g=b.length,c,d,e,l,p;if(0!==g%2)throw Error("String of HEX type must be in byte increments");a=a||[0];f=f||0;p=f>>>3;for(c=0;c<g;c+=2){d=parseInt(b.substr(c,2),16);if(isNaN(d))throw Error("String of HEX type contains invalid characters");
l=(c>>>1)+p;for(e=l>>>2;a.length<=e;)a.push(0);a[e]|=d<<8*(3+l%4*-1)}return{value:a,binLen:4*g+f}};break;case "TEXT":c=function(c,a,f){var g,d,k=0,e,l,p,q,t,n;a=a||[0];f=f||0;p=f>>>3;if("UTF8"===b)for(n=3,e=0;e<c.length;e+=1)for(g=c.charCodeAt(e),d=[],128>g?d.push(g):2048>g?(d.push(192|g>>>6),d.push(128|g&63)):55296>g||57344<=g?d.push(224|g>>>12,128|g>>>6&63,128|g&63):(e+=1,g=65536+((g&1023)<<10|c.charCodeAt(e)&1023),d.push(240|g>>>18,128|g>>>12&63,128|g>>>6&63,128|g&63)),l=0;l<d.length;l+=1){t=k+
p;for(q=t>>>2;a.length<=q;)a.push(0);a[q]|=d[l]<<8*(n+t%4*-1);k+=1}else if("UTF16BE"===b||"UTF16LE"===b)for(n=2,d="UTF16LE"===b&&!0||"UTF16LE"!==b&&!1,e=0;e<c.length;e+=1){g=c.charCodeAt(e);!0===d&&(l=g&255,g=l<<8|g>>>8);t=k+p;for(q=t>>>2;a.length<=q;)a.push(0);a[q]|=g<<8*(n+t%4*-1);k+=2}return{value:a,binLen:8*k+f}};break;case "B64":c=function(b,a,f){var c=0,d,k,e,l,p,q,n;if(-1===b.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");k=b.indexOf("=");b=b.replace(/\=/g,
"");if(-1!==k&&k<b.length)throw Error("Invalid '=' found in base-64 string");a=a||[0];f=f||0;q=f>>>3;for(k=0;k<b.length;k+=4){p=b.substr(k,4);for(e=l=0;e<p.length;e+=1)d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(p[e]),l|=d<<18-6*e;for(e=0;e<p.length-1;e+=1){n=c+q;for(d=n>>>2;a.length<=d;)a.push(0);a[d]|=(l>>>16-8*e&255)<<8*(3+n%4*-1);c+=1}}return{value:a,binLen:8*c+f}};break;case "BYTES":c=function(b,a,c){var d,m,k,e,l;a=a||[0];c=c||0;k=c>>>3;for(m=0;m<b.length;m+=
1)d=b.charCodeAt(m),l=m+k,e=l>>>2,a.length<=e&&a.push(0),a[e]|=d<<8*(3+l%4*-1);return{value:a,binLen:8*b.length+c}};break;case "ARRAYBUFFER":try{c=new ArrayBuffer(0)}catch(h){throw Error("ARRAYBUFFER not supported by this environment");}c=function(b,a,c){var d,m,k,e,l;a=a||[0];c=c||0;m=c>>>3;l=new Uint8Array(b);for(d=0;d<b.byteLength;d+=1)e=d+m,k=e>>>2,a.length<=k&&a.push(0),a[k]|=l[d]<<8*(3+e%4*-1);return{value:a,binLen:8*b.byteLength+c}};break;default:throw Error("format must be HEX, TEXT, B64, BYTES, or ARRAYBUFFER");
}return c}function n(d,b){return d<<b|d>>>32-b}function u(d,b){var c=(d&65535)+(b&65535);return((d>>>16)+(b>>>16)+(c>>>16)&65535)<<16|c&65535}function y(d,b,c,h,a){var f=(d&65535)+(b&65535)+(c&65535)+(h&65535)+(a&65535);return((d>>>16)+(b>>>16)+(c>>>16)+(h>>>16)+(a>>>16)+(f>>>16)&65535)<<16|f&65535}function x(d){var b=[];if("SHA-1"===d)b=[1732584193,4023233417,2562383102,271733878,3285377520];else throw Error("No SHA variants supported");return b}function z(d,b){var c=[],h,a,f,g,m,k,e;h=b[0];a=b[1];
f=b[2];g=b[3];m=b[4];for(e=0;80>e;e+=1)c[e]=16>e?d[e]:n(c[e-3]^c[e-8]^c[e-14]^c[e-16],1),k=20>e?y(n(h,5),a&f^~a&g,m,1518500249,c[e]):40>e?y(n(h,5),a^f^g,m,1859775393,c[e]):60>e?y(n(h,5),a&f^a&g^f&g,m,2400959708,c[e]):y(n(h,5),a^f^g,m,3395469782,c[e]),m=g,g=f,f=n(a,30),a=h,h=k;b[0]=u(h,b[0]);b[1]=u(a,b[1]);b[2]=u(f,b[2]);b[3]=u(g,b[3]);b[4]=u(m,b[4]);return b}function H(d,b,c,h){var a;for(a=(b+65>>>9<<4)+15;d.length<=a;)d.push(0);d[b>>>5]|=128<<24-b%32;b+=c;d[a]=b&4294967295;d[a-1]=b/4294967296|0;
b=d.length;for(a=0;a<b;a+=16)h=z(d.slice(a,a+16),h);return h}"function"===typeof define&&define.amd?define(function(){return r}):"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(module.exports=r),exports=r):G.jsSHA=r})(this);
var vkTimestamp = parseInt(Date.now() / 1000);
var vkQuery = "/v5/videos/" + vkVideoID + "/streams.json?app=100005a&t=" + vkTimestamp + "&site=www.viki.com"
var vkToken = "MM_d*yP@`&1@]@!AVrXf_o-HVEnoTnm$O-ti4[G~$JDI/Dc-&piU&z&5.;:}95\=Iad";
var shaObj = new jsSHA("SHA-1", "TEXT");
shaObj.setHMACKey(vkToken, "TEXT");
shaObj.update(vkQuery);
var vkSig = shaObj.getHMAC("HEX");
var vkSource = "https://api.viki.io" + vkQuery + "&sig=" + vkSig;
vkVideosContent = getMyContent(vkSource, 'TEXT', false);
var SHA1FuncBody;
var SHA1Key = 'sha1js';
try {
if (localStorage.getItem(SHA1Key)) {
SHA1FuncBody = localStorage.getItem(SHA1Key);
}
else throw false;
}
catch(e) {
SHA1FuncBody = getMyContent('https://raw.githack.com/Caligatio/jsSHA/master/src/sha1.js', 'TEXT', false);
localStorage.setItem(SHA1Key, SHA1FuncBody);
}
var SHA1Func = new Function('a', SHA1FuncBody);
var SHA1 = new SHA1Func();
if (SHA1.jsSHA) {
var shaObj = new SHA1.jsSHA("SHA-1", "TEXT");
var vkTimestamp = parseInt(Date.now() / 1000);
var vkQuery = "/v5/videos/" + vkVideoID + "/streams.json?app=100005a&t=" + vkTimestamp + "&site=www.viki.com"
var vkToken = "MM_d*yP@`&1@]@!AVrXf_o-HVEnoTnm$O-ti4[G~$JDI/Dc-&piU&z&5.;:}95\=Iad";
shaObj.setHMACKey(vkToken, "TEXT");
shaObj.update(vkQuery);
var vkSig = shaObj.getHMAC("HEX");
var vkSource = "https://api.viki.io" + vkQuery + "&sig=" + vkSig;
vkVideosContent = getMyContent(vkSource, 'TEXT', false);
}
}
/* Get Videos */
if (vkVideosContent) {
var vkVideoList = {};
var vkVideoFormats = {'720p': 'High Definition MP4', '480p': 'Standard Definition MP4', '360p': 'Low Definition MP4', '240p': 'Very Low Definition MP4'};
var vkVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4',
'360p': 'Low Definition MP4', '240p': 'Very Low Definition MP4'};
var vkVideoFound = false;
var vkVideoParser, vkVideoParse, vkVideo, myVideoCode;
for (var vkVideoCode in vkVideoFormats) {
......@@ -1218,13 +1198,108 @@ function SaveTube() {
}
}
// DASH/HLS/Subtitles
vkVideosContent = getMyContent(page.url.replace('/videos/', '/player5_fragment/'), 'TEXT', false);
if (vkVideosContent) {
vkVideoEncDASH = vkVideosContent.match(/dash\+xml".*?stream=(.*?)"/);
vkVideoEncDASH = (vkVideoEncDASH) ? vkVideoEncDASH[1] : null;
vkVideoEncHLS = vkVideosContent.match(/x-mpegURL".*?stream=(.*?)"/);
vkVideoEncHLS = (vkVideoEncHLS) ? vkVideoEncHLS[1] : null;
if (vkVideoEncDASH || vkVideoEncHLS) {
vkVideoEncKey = vkVideosContent.match(/chabi:\s*'(.*?)'/);
vkVideoEncKey = (vkVideoEncKey) ? vkVideoEncKey[1] : null;
vkVideoEncIV = vkVideosContent.match(/ecta:\s*'(.*?)'/);
vkVideoEncIV = (vkVideoEncIV) ? vkVideoEncIV[1] : null;
if (vkVideoEncKey && vkVideoEncIV) {
/* AES
Copyright 2015-2018 Richard Moore
MIT License.
See https://github.com/ricmoo/aes-js/ for more information
*/
var AESFuncBody;
var AESKey = 'aesjs';
try {
if (localStorage.getItem(AESKey)) {
AESFuncBody = localStorage.getItem(AESKey);
}
else throw false;
}
catch(e) {
AESFuncBody = getMyContent('https://raw.githack.com/ricmoo/aes-js/master/index.js', 'TEXT', false);
localStorage.setItem(AESKey, AESFuncBody);
}
var AESFunc = new Function('a', AESFuncBody);
var AES = new AESFunc();
var AESKey = AES.aesjs.utils.utf8.toBytes(vkVideoEncKey);
var AESIV = AES.aesjs.utils.utf8.toBytes(vkVideoEncIV);
var encryptedBytes, decryptedBytes;
// HLS
encryptedBytes = AES.aesjs.utils.hex.toBytes(vkVideoEncHLS);
AESCBC = new AES.aesjs.ModeOfOperation.cbc(AESKey, AESIV);
decryptedBytes = AESCBC.decrypt(encryptedBytes);
var vkHLSManifest = AES.aesjs.utils.utf8.fromBytes(decryptedBytes);
if (vkHLSManifest) {
if (!vkVideoFound) vkVideoFound = true;
vkVideoList['Any Definition HLS'] = vkHLSManifest;
}
// DASH
encryptedBytes = AES.aesjs.utils.hex.toBytes(vkVideoEncDASH);
AESCBC = new AES.aesjs.ModeOfOperation.cbc(AESKey, AESIV);
decryptedBytes = AESCBC.decrypt(encryptedBytes);
var vkDASHManifest = AES.aesjs.utils.utf8.fromBytes(decryptedBytes);
if (vkDASHManifest) {
var vkDASHDomain = vkDASHManifest.split('/').splice(0, 5).join('/');
var vkDASHContent = getMyContent(vkDASHManifest, 'TEXT', false);
if (vkDASHContent) {
var vkDASHVideo;
var vkDASHVideos = vkDASHContent.match(new RegExp('<BaseURL>.*?</BaseURL>', 'g'));
if (vkDASHVideos) {
for (var i = 0; i < vkDASHVideos.length; i++) {
vkDASHVideo = vkDASHDomain + '/' + vkDASHVideos[i].replace('<BaseURL>', '').replace('</BaseURL>', '');
for (var vkVideoCode in vkVideoFormats) {
if (vkDASHVideo.indexOf(vkVideoCode) != -1) {
myVideoCode = vkVideoFormats[vkVideoCode];
if (vkDASHVideo.indexOf('track1') != -1) {
if (!vkVideoFound) vkVideoFound = true;
if (!vkVideoList[myVideoCode]) {
vkVideoList[myVideoCode.replace('MP4', 'Video MP4')] = vkDASHVideo;
}
}
if (vkDASHVideo.indexOf('track2') != -1) {
if (!vkVideoList[myVideoCode]) {
vkVideoList[myVideoCode.replace('MP4', 'Audio MP4')] = vkDASHVideo;
}
}
}
}
}
}
for (var vkVideoCode in vkVideoFormats) {
myVideoCode = vkVideoFormats[vkVideoCode];
if (!vkVideoList[myVideoCode]) {
if (vkVideoList[myVideoCode.replace('MP4', 'Video MP4')] && vkVideoList[myVideoCode.replace('MP4', 'Audio MP4')]) {
vkVideoList[myVideoCode] = 'DASH';
}
}
}
}
}
}
}
// Subtitles
var vkSubtitles = vkVideosContent.match(/"srclang":"en".*?"src":"(.*?)"/);
vkSubtitles = (vkSubtitles) ? vkSubtitles[1] : null;
if (vkSubtitles) vkVideoList['EN Subtitles WebVTT'] = vkSubtitles;
}
/* Create Saver */
if (vkVideoFound) {
var vkDefaultVideo = 'Low Definition MP4';
saver = {'videoList': vkVideoList, 'videoSave': vkDefaultVideo, 'videoTitle': vkVideoTitle};
feature['container'] = false;
feature['dash'] = true;
option['definition'] = 'LD';
option['definitions'] = ['High Definition', 'Standard Definition', 'Low Definition', 'Very Low Definition'];
option['definitions'] = ['Full High Definition', 'High Definition', 'Standard Definition', 'Low Definition', 'Very Low Definition'];
option['containers'] = ['MP4'];
createMySaver();
}
......@@ -1283,7 +1358,8 @@ function SaveTube() {
/* Get Videos */
var imdbVideoList = {};
if (imdbVideosContent) {
var imdbVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4', '360p': 'Low Definition MP4', 'SD': 'Low Definition MP4', '240p': 'Very Low Definition MP4'};
var imdbVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4',
'360p': 'Low Definition MP4', 'SD': 'Low Definition MP4', '240p': 'Very Low Definition MP4'};
var imdbVideoFound = false;
var imdbVideoParser, imdbVideoParse, myVideoCode, imdbVideo;
for (var imdbVideoCode in imdbVideoFormats) {
......
#! /bin/bash
d="/home/Videos/YouTube"
t="$(echo "$1" | awk -F '=SAVETUBE=' '{print $1}' | sed -e "s/savetube://" | sed -e "s/%20/ /g" -e "s/ +$//" -e "s/^ +//")"
v="$(echo "$1" | awk -F '=SAVETUBE=' '{print $2}')"
a="$(echo "$1" | awk -F '=SAVETUBE=' '{print $3}')"
f="$(echo "$v" | grep -q "video/mp4" && echo "mp4" || echo "webm")"
t="$(echo "$1" | awk -F 'SEPARATOR' '{print $1}' | sed -e "s/savetube://" | sed -e "s/%20/ /g" -e "s/ +$//" -e "s/^ +//")"
v="$(echo "$1" | awk -F 'SEPARATOR' '{print $2}')"
a="$(echo "$1" | awk -F 'SEPARATOR' '{print $3}')"
f="$(echo "$v" | grep -q "video/mp4\|\.mp4" && echo "mp4" || echo "webm")"
ffmpeg -i "$v" -i "$a" -c:v copy -c:a copy "$d/$t.$f"
// ==UserScript==
// @name SaveTube
// @version 2019.06.05
// @version 2019.06.21
// @description Download videos from video sharing web sites.
// @author sebaro
// @namespace http://sebaro.pro/savetube
......@@ -418,31 +418,15 @@ function getMyVideo() {
else {
if (saver['videoList'][saver['videoSave']] == 'DASH') {
if (saver['videoSave'].indexOf('MP4') != -1) {
var vdoV = saver['videoList'][saver['videoSave'].replace(/MP4/, 'Video MP4')];
if (saver['videoList']['High Bitrate Audio MP4']) {
var vdoA = saver['videoList']['High Bitrate Audio MP4'];
}
else if (saver['videoList']['Medium Bitrate Audio MP4']) {
var vdoA = saver['videoList']['Medium Bitrate Audio MP4'];
}
else {
var vdoA = saver['videoList']['Low Bitrate Audio MP4'];
}
var vdoV = saver['videoList'][saver['videoSave'].replace('MP4', 'Video MP4')];
var vdoA = saver['videoList'][saver['videoSave'].replace('MP4', 'Audio MP4')] || saver['videoList']['High Bitrate Audio MP4'] || saver['videoList']['Medium Bitrate Audio MP4'];
}
else {
var vdoV = saver['videoList'][saver['videoSave'].replace(/WebM/, 'Video WebM')];
if (saver['videoList']['High Bitrate Audio Opus']) {
var vdoA = saver['videoList']['High Bitrate Audio Opus'];
}
else if (saver['videoList']['Medium Bitrate Audio Opus']) {
var vdoA = saver['videoList']['Medium Bitrate Audio Opus'];
}
else {
var vdoA = saver['videoList']['Low Bitrate Audio Opus'];
}
var vdoV = saver['videoList'][saver['videoSave'].replace('WebM', 'Video WebM')];
var vdoA = saver['videoList']['High Bitrate Audio Opus'] || saver['videoList']['Medium Bitrate Audio Opus'] || saver['videoList']['Low Bitrate Audio Opus'];
}
var vdoT = (vdoTle) ? vdoTle + vdoDef : page.site + vdoDef;
vdoURL = 'savetube:' + vdoT + '=SAVETUBE=' + vdoV + '=SAVETUBE=' + vdoA;
vdoURL = 'savetube:' + vdoT + 'SEPARATOR' + vdoV + 'SEPARATOR' + vdoA;
page.win.location.href = vdoURL;
}
else {
......@@ -563,13 +547,8 @@ function SaveTube() {
if (page.url.indexOf('youtube.com/watch') != -1) {
/* Redirect Categories */
if (page.url.indexOf('gaming.youtube.com') != -1) {
page.win.location.href = page.url.replace('gaming', 'www');
}
/* Video Availability */
if (getMyContent(page.url, '"simpleText":"(This video is (private|unavailable|not available).)"', false)) return;
if (getMyContent(page.url, '"playabilityStatus":\\{"status":"(ERROR|UNPLAYABLE)"', false)) return;
var ytVideoUnavailable = getMyElement('', 'div', 'id', 'player-unavailable', -1, false);
if (ytVideoUnavailable) {
if (ytVideoUnavailable.className.indexOf('hid') == -1) {
......@@ -588,7 +567,7 @@ function SaveTube() {
var ytSignFuncName, ytSignFuncBody, ytSwapFuncName, ytSwapFuncBody, ytFuncMatch;
ytScriptSrc = ytScriptSrc.replace(/(\r\n|\n|\r)/gm, '');
ytSignFuncName = ytScriptSrc.match(/"signature"\s*,\s*([^\)]*?)\(/);
if (!ytSignFuncName) ytSignFuncName = ytScriptSrc.match(/d.set\(b,(?:encodeURIComponent\()?.*?([a-zA-Z0-9$]+)\(/);
if (!ytSignFuncName) ytSignFuncName = ytScriptSrc.match(/c&&.\.set\(b,(?:encodeURIComponent\()?.*?([$a-zA-Z0-9]+)\(/);
ytSignFuncName = (ytSignFuncName) ? ytSignFuncName[1] : null;
if (ytSignFuncName) {
ytFuncMatch = ytSignFuncName.replace(/\$/, '\\$') + '\\s*=\\s*function\\s*' + '\\s*\\(\\w+\\)\\s*\\{(.*?)\\}';
......@@ -852,7 +831,7 @@ function SaveTube() {
/* Get Videos */
var ytVideoList = {};
if (ytVideosContent) {
if (ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) {
if (ytVideosContent.match(/^s=/) || ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) {
var ytScriptURL = getMyContent(page.url, '"js":\\s*"(.*?)"', true);
if (!ytScriptURL) ytScriptURL = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"js":\\s*"(.*?)"', true);
if (ytScriptURL) {
......@@ -890,6 +869,10 @@ function SaveTube() {
else if (page.url.indexOf('dailymotion.com/video') != -1) {
/* Video Availability */
if (getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"error":\\{"title":"(.*?)"', false)) return;
if (getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"error_title":"(.*?)"', false)) return;
/* Get Video Title */
var dmVideoTitle = getMyContent(page.url.replace(/\/video\//, "/embed/video/"), '"title":"(.*?)"', false);
if (dmVideoTitle) {
......@@ -978,7 +961,8 @@ function SaveTube() {
/* Get Videos */
if (viVideosContent) {
var viVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4', '360p': 'Low Definition MP4', '270p': 'Very Low Definition MP4'};
var viVideoFormats = {'1080p': 'Full High Definition MP4', '720p': 'High Definition MP4', '480p': 'Standard Definition MP4',
'360p': 'Low Definition MP4', '270p': 'Very Low Definition MP4'};
var viVideoList = {};
var viVideoFound = false;
var viVideo, myVideoCode;
......@@ -1164,47 +1148,43 @@ function SaveTube() {
/* Get Videos Content */
var vkVideosContent;
if (vkVideoID) {
/*
A JavaScript implementation of the SHA family of hashes, as
defined in FIPS PUB 180-4 and FIPS PUB 202, as well as the corresponding
HMAC implementation as defined in FIPS PUB 198a
Copyright Brian Turek 2008-2017
/* SHA-1
Copyright 2008-2018 Brian Turek, 1998-2009 Paul Johnston & Contributors
Distributed under the BSD License
See http://caligatio.github.com/jsSHA/ for more information
Several functions taken from Paul Johnston
See https://caligatio.github.com/jsSHA/ for more information
*/
'use strict';(function(G){function r(d,b,c){var h=0,a=[],f=0,g,m,k,e,l,p,q,t,w=!1,n=[],u=[],v,r=!1;c=c||{};g=c.encoding||"UTF8";v=c.numRounds||1;if(v!==parseInt(v,10)||1>v)throw Error("numRounds must a integer >= 1");if("SHA-1"===d)l=512,p=z,q=H,e=160,t=function(a){return a.slice()};else throw Error("Chosen SHA variant is not supported");k=A(b,g);m=x(d);this.setHMACKey=function(a,f,b){var c;if(!0===w)throw Error("HMAC key already set");if(!0===r)throw Error("Cannot set HMAC key after calling update");
g=(b||{}).encoding||"UTF8";f=A(f,g)(a);a=f.binLen;f=f.value;c=l>>>3;b=c/4-1;if(c<a/8){for(f=q(f,a,0,x(d),e);f.length<=b;)f.push(0);f[b]&=4294967040}else if(c>a/8){for(;f.length<=b;)f.push(0);f[b]&=4294967040}for(a=0;a<=b;a+=1)n[a]=f[a]^909522486,u[a]=f[a]^1549556828;m=p(n,m);h=l;w=!0};this.update=function(b){var e,g,c,d=0,q=l>>>5;e=k(b,a,f);b=e.binLen;g=e.value;e=b>>>5;for(c=0;c<e;c+=q)d+l<=b&&(m=p(g.slice(c,c+q),m),d+=l);h+=d;a=g.slice(d>>>5);f=b%l;r=!0};this.getHash=function(b,g){var c,k,l,p;if(!0===
w)throw Error("Cannot call getHash after setting HMAC key");l=B(g);switch(b){case "HEX":c=function(a){return C(a,e,l)};break;case "B64":c=function(a){return D(a,e,l)};break;case "BYTES":c=function(a){return E(a,e)};break;case "ARRAYBUFFER":try{k=new ArrayBuffer(0)}catch(I){throw Error("ARRAYBUFFER not supported by this environment");}c=function(a){return F(a,e)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER");}p=q(a.slice(),f,h,t(m),e);for(k=1;k<v;k+=1)p=q(p,e,0,x(d),e);
return c(p)};this.getHMAC=function(b,g){var c,k,n,r;if(!1===w)throw Error("Cannot call getHMAC without first setting HMAC key");n=B(g);switch(b){case "HEX":c=function(a){return C(a,e,n)};break;case "B64":c=function(a){return D(a,e,n)};break;case "BYTES":c=function(a){return E(a,e)};break;case "ARRAYBUFFER":try{c=new ArrayBuffer(0)}catch(I){throw Error("ARRAYBUFFER not supported by this environment");}c=function(a){return F(a,e)};break;default:throw Error("outputFormat must be HEX, B64, BYTES, or ARRAYBUFFER");
}k=q(a.slice(),f,h,t(m),e);r=p(u,x(d));r=q(k,e,l,r,e);return c(r)}}function C(d,b,c){var h="";b/=8;var a,f;for(a=0;a<b;a+=1)f=d[a>>>2]>>>8*(3+a%4*-1),h+="0123456789abcdef".charAt(f>>>4&15)+"0123456789abcdef".charAt(f&15);return c.outputUpper?h.toUpperCase():h}function D(d,b,c){var h="",a=b/8,f,g,m;for(f=0;f<a;f+=3)for(g=f+1<a?d[f+1>>>2]:0,m=f+2<a?d[f+2>>>2]:0,m=(d[f>>>2]>>>8*(3+f%4*-1)&255)<<16|(g>>>8*(3+(f+1)%4*-1)&255)<<8|m>>>8*(3+(f+2)%4*-1)&255,g=0;4>g;g+=1)8*f+6*g<=b?h+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(m>>>
6*(3-g)&63):h+=c.b64Pad;return h}function E(d,b){var c="",h=b/8,a,f;for(a=0;a<h;a+=1)f=d[a>>>2]>>>8*(3+a%4*-1)&255,c+=String.fromCharCode(f);return c}function F(d,b){var c=b/8,h,a=new ArrayBuffer(c),f;f=new Uint8Array(a);for(h=0;h<c;h+=1)f[h]=d[h>>>2]>>>8*(3+h%4*-1)&255;return a}function B(d){var b={outputUpper:!1,b64Pad:"=",shakeLen:-1};d=d||{};b.outputUpper=d.outputUpper||!1;!0===d.hasOwnProperty("b64Pad")&&(b.b64Pad=d.b64Pad);if("boolean"!==typeof b.outputUpper)throw Error("Invalid outputUpper formatting option");
if("string"!==typeof b.b64Pad)throw Error("Invalid b64Pad formatting option");return b}function A(d,b){var c;switch(b){case "UTF8":case "UTF16BE":case "UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE");}switch(d){case "HEX":c=function(b,a,f){var g=b.length,c,d,e,l,p;if(0!==g%2)throw Error("String of HEX type must be in byte increments");a=a||[0];f=f||0;p=f>>>3;for(c=0;c<g;c+=2){d=parseInt(b.substr(c,2),16);if(isNaN(d))throw Error("String of HEX type contains invalid characters");
l=(c>>>1)+p;for(e=l>>>2;a.length<=e;)a.push(0);a[e]|=d<<8*(3+l%4*-1)}return{value:a,binLen:4*g+f}};break;case "TEXT":c=function(c,a,f){var g,d,k=0,e,l,p,q,t,n;a=a||[0];f=f||0;p=f>>>3;if("UTF8"===b)for(n=3,e=0;e<c.length;e+=1)for(g=c.charCodeAt(e),d=[],128>g?d.push(g):2048>g?(d.push(192|g>>>6),d.push(128|g&63)):55296>g||57344<=g?d.push(224|g>>>12,128|g>>>6&63,128|g&63):(e+=1,g=65536+((g&1023)<<10|c.charCodeAt(e)&1023),d.push(240|g>>>18,128|g>>>12&63,128|g>>>6&63,128|g&63)),l=0;l<d.length;l+=1){t=k+
p;for(q=t>>>2;a.length<=q;)a.push(0);a[q]|=d[l]<<8*(n+t%4*-1);k+=1}else if("UTF16BE"===b||"UTF16LE"===b)for(n=2,d="UTF16LE"===b&&!0||"UTF16LE"!==b&&!1,e=0;e<c.length;e+=1){g=c.charCodeAt(e);!0===d&&(l=g&255,g=l<<8|g>>>8);t=k+p;for(q=t>>>2;a.length<=q;)a.push(0);a[q]|=g<<8*(n+t%4*-1);k+=2}return{value:a,binLen:8*k+f}};break;case "B64":c=function(b,a,f){var c=0,d,k,e,l,p,q,n;if(-1===b.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");k=b.indexOf("=");b=b.replace(/\=/g,
"");if(-1!==k&&k<b.length)throw Error("Invalid '=' found in base-64 string");a=a||[0];f=f||0;q=f>>>3;for(k=0;k<b.length;k+=4){p=b.substr(k,4);for(e=l=0;e<p.length;e+=1)d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(p[e]),l|=d<<18-6*e;for(e=0;e<p.length-1;e+=1){n=c+q;for(d=n>>>2;a.length<=d;)a.push(0);a[d]|=(l>>>16-8*e&255)<<8*(3+n%4*-1);c+=1}}return{value:a,binLen:8*c+f}};break;case "BYTES":c=function(b,a,c){var d,m,k,e,l;a=a||[0];c=c||0;k=c>>>3;for(m=0;m<b.length;m+=
1)d=b.charCodeAt(m),l=m+k,e=l>>>2,a.length<=e&&a.push(0),a[e]|=d<<8*(3+l%4*-1);return{value:a,binLen:8*b.length+c}};break;case "ARRAYBUFFER":try{c=new ArrayBuffer(0)}catch(h){throw Error("ARRAYBUFFER not supported by this environment");}c=function(b,a,c){var d,m,k,e,l;a=a||[0];c=c||0;m=c>>>3;l=new Uint8Array(b);for(d=0;d<b.byteLength;d+=1)e=d+m,k=e>>>2,a.length<=k&&a.push(0),a[k]|=l[d]<<8*(3+e%4*-1);return{value:a,binLen:8*b.byteLength+c}};break;default:throw Error("format must be HEX, TEXT, B64, BYTES, or ARRAYBUFFER");
}return c}function n(d,b){return d<<b|d>>>32-b}function u(d,b){var c=(d&65535)+(b&65535);return((d>>>16)+(b>>>16)+(c>>>16)&65535)<<16|c&65535}function y(d,b,c,h,a){var f=(d&65535)+(b&65535)+(c&65535)+(h&65535)+(a&65535);return((d>>>16)+(b>>>16)+(c>>>16)+(h>>>16)+(a>>>16)+(f>>>16)&65535)<<16|f&65535}function x(d){var b=[];if("SHA-1"===d)b=[1732584193,4023233417,2562383102,271733878,3285377520];else throw Error("No SHA variants supported");return b}function z(d,b){var c=[],h,a,f,g,m,k,e;h=b[0];a=b[1];
f=b[2];g=b[3];m=b[4];for(e=0;80>e;e+=1)c[e]=16>e?d[e]:n(c[e-3]^c[e-8]^c[e-14]^c[e-16],1),k=20>e?y(n(h,5),a&f^~a&g,m,1518500249,c[e]):40>e?y(n(h,5),a^f^g,m,1859775393,c[e]):60>e?y(n(h,5),a&f^a&g^f&g,m,2400959708,c[e]):y(n(h,5),a^f^g,m,3395469782,c[e]),m=g,g=f,f=n(a,30),a=h,h=k;b[0]=u(h,b[0]);b[1]=u(a,b[1]);b[2]=u(f,b[2]);b[3]=u