Skip to content

Instantly share code, notes, and snippets.

@Riboe
Last active December 3, 2023 04:34
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Riboe/58acabe04abe74421e88b4269cbdf41e to your computer and use it in GitHub Desktop.
Save Riboe/58acabe04abe74421e88b4269cbdf41e to your computer and use it in GitHub Desktop.
PH download script
The script is developed and maintained by /u/StackedLasagna.
2021/01/12: Added some logging to help with debugging an issue, which causes the script to fail sometimes.
A temporary workaround is to refresh the page a few times and the issue should go away.
This update contains no bug fixes or new functionality, but if you'd like to help me solve the aforementioned issue, please install this version.
If you installed this update and encounter the issue, then press F12 to open your web browser's built-in developer tools, then go to the "Console" tab.
This tab likely contains a lot of messages, some of which will be red and others will be yellow or white. These are messages logged by PornHub themselves, as well as any other scripts or extensions you may be running.
Look for two white messages, both starting with "PornHub Downloader Script:". They'll likely be followed by quite a bit of text. Copy the two messages in full (if you're in doubt, just copy everything in the console tab) post them as a comment on here or send them privately on GitHub or Reddit.
If you're wondering, those two messages are supposed to contain the download links for the video you're watching, but I suspect that in some cases, they'll contain something else, though I don't know what yet.
2021/01/07: Updated the script, since PornHub changed a few things and broke it.
Thanks to /u/VividHamster for figuring out how to get around PornHub's changes.
I've incorporated his code and changed things around a bit to make it run a tiny bit smoother.
2020/??/??: Additional match clauses added, thanks to /u/marcelotk.
Download an extension that supports running user scripts (I prefer ViolentMonkey on Chrome-based browsers), add the script below as a new script and that's it.
Go to a video (URL looks like this: https://www.pornhubpremium.com/view_video.php?viewkey=xxxxxxxxxxxxx), and you should see a new tab labelled "Download (GM)" together with the other tabs below the video (About, Share, and so on.)
Copy the title of the video (so you can easily paste it when beginning the download in the next step.)
Click the tab and you should see a list of download links (one for each resolution that's available), right click one of them and select "Save as..." or whatever it's called in your browser. Paste the title you copied into the file name box, then click Save/OK or whatever.
The video will be downloaded just like any other file download.
// ==UserScript==
// @name PH Downloader - pornhubpremium.com
// @namespace Violentmonkey Scripts
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @match https://www.pornhubpremium.com/view_video.php
// @match https://www.pornhub.com/view_video.php*
// @match *://www.pornhub.com/view_video.php*
// @match *://*.pornhub.com/view_video.php*
// @match *://pornhub.com/view_video.php*
// @match https://www.pornhubpremium.com/view_video.php*
// @match *://www.pornhubpremium.com/view_video.php*
// @match *://*.pornhubpremium.com/view_video.php*
// @match *://pornhubpremium.com/view_video.php*
// @grant none
// @version 1.0
// @author -
// @description 3/12/2020, 10:56:35 PM
// ==/UserScript==
$ = unsafeWindow.jQuery.noConflict(true);
$.ajaxSetup({
async: true
});
var onJsonDataDownloaded = function (data) {
console.log("PornHub Downloader Script: JSON = ", JSON.stringify(data));
// Create a new tab element on the page:
var tabs = document.getElementsByClassName('video-actions-tabs')[0];
var tab = document.createElement('div');
tab.className = 'video-action-tab my-custom-tab';
tabs.appendChild(tab);
var title = document.createElement('div');
title.className = 'title';
title.innerText = 'Use right click and "Save as..." to download.';
tab.appendChild(title);
// Create the download links for each resolution:
for(var i = 0; i < data.length; i++) {
var videoFile = data[i];
var container = document.createElement('div');
var a = document.createElement('a');
a.innerText = 'Download (' + videoFile.quality + 'p, .' + videoFile.format + ')';
a.dataset.videoUrl = videoFile.videoUrl;
a.href = videoFile.videoUrl;
a.download = 'test-' + videoFile.quality + '.' + videoFile.format;
a.target = '_blank';
a.style.fontSize = '140%';
container.style.marginBottom = '6px';
container.appendChild(a);
tab.appendChild(container);
}
// Create a new menu item, which opens the new tab:
var menu = document.getElementsByClassName('tab-menu-wrapper-row')[0];
var newItem = document.createElement('div');
var sub = document.createElement('div');
var icon = document.createElement('i');
var text = document.createElement('span');
text.innerText = 'Download (GM)';
icon.className = 'main-sprite-dark-2';
sub.className = 'tab-menu-item tooltipTrig';
sub.dataset.title = 'Download (GM)';
sub.dataset.tab = 'my-custom-tab';
sub.appendChild(icon);
sub.appendChild(text);
newItem.className = 'tab-menu-wrapper-cell';
newItem.appendChild(sub);
menu.appendChild(newItem);
};
// First find the name of the flashvar variable in one of the scripts on the page:
var start = document.documentElement.innerHTML.indexOf('var flashvars_') + 'var '.length;
var tempDoc = document.documentElement.innerHTML.substr(start);
var end = tempDoc.indexOf(' =');
var flashVarsName = tempDoc.substr(0, end);
var flashVars = unsafeWindow[flashVarsName];
console.log("PornHub Downloader Script: MediaDefinitions = ", JSON.stringify(flashVars.mediaDefinitions));
for(var i = 0; i < flashVars.mediaDefinitions.length; i++) {
var definition = flashVars.mediaDefinitions[i];
if (definition.format === 'mp4') {
$.getJSON(definition.videoUrl, onJsonDataDownloaded);
return;
}
}
@Riboe
Copy link
Author

Riboe commented Nov 17, 2022

@thebanker7
If the issue is an ad blocking the button, then I recommend you get a reputable ad blocker. I recommend uBlock Origin.
I also use the Vivaldi browser, which comes with a built-in ad blocker.

On my end, everything works fine, even if I disable both uBlock Origin and the built-in ad blocker.

@thebanker7
Copy link

It is opera, firefox works. Thanks

@Ghosto0o0
Copy link

Hi! I've been using this for the past couple days and its been working very well, issue now is when I try to download anything today it tells me to right click save as to dl the video instead of giving me download links, was wondering if you knew anything about this or how to possibly fix it?

@Riboe
Copy link
Author

Riboe commented Sep 17, 2023

@Ghosto0o0

Sadly I cannot fix the issue.

It is caused by PornHub no longer (at least for the videos I checked) use .mp4 files. Instead they use .hls files.
This is the same format used for most livestream services and as such, there's not a single file I can get the link to, because it's broken up into chunks instead and you have to download the initial .hls file to find the chunks and such.

In short, it's a pain and way beyond the scope of the script, so I won't be making any attempts to fix this, sadly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment