Skip to content

Instantly share code, notes, and snippets.

@Riboe
Last active July 6, 2024 22:54
Show Gist options
  • 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;
}
}
@secretchase
Copy link

hi, this no longer has the option to download above 1080p. not sure if you know this.

@Riboe
Copy link
Author

Riboe commented Jan 11, 2021

Do you have a link to a video where this issue occurs?

@onyzer
Copy link

onyzer commented Jan 12, 2021

Is it possible to make it work for normal ph cause the options appear but i can't download anything

@Riboe
Copy link
Author

Riboe commented Jan 12, 2021

@onyzer It should work, but I've just tested it and have found a weird issue: it seems like it fails occasionally, but refreshing the page will eventually make it work. You may need to refresh more than once.

I've just updated the script to contain some debugging stuff, which will help me figure out what the issue is and how to fix it. If you're interested in helping me out, read the notes for the new release at the top of this page, for instructions on how you can help. :)

@Riboe
Copy link
Author

Riboe commented Jan 22, 2021

@arinarjani I've updated your comment and removed a lot of irrelevant stuff from it.
This is in an effort to keep the comment section somewhat readable and each conversation easier to follow.

Rather than starting multiple conversations, as well as jumping into existing conversations in a single comment, in the future, please post individual comments for each conversation you want to participate in.

I've removed the section on the 1080p+ issue, because the conversation is clearly already dead and asking the user for (practically) the same stuff I've already asked them is not helping. I asked them and they didn't reply. Asking them again will only serve as spam.

I've also removed your rewrite of my script.
It's fine that you wanted to rewrite it, in order to learn something, but it has no place being posted here afterwards. You made no improvements or suggestions on how to improve my script, so it's just pointless noise. Especially considering that all you did was use some slightly more modern JS APIs, but I've specifically chosen not to use those APIs for maximum compatibility.
If you have specific suggestions, feature requests, or questions about why I do things a certain way, when a (seemingly) better option is available, feel free to ask about it.


Now, as for the remaining parts of your comment, here's my reply:

As for the F5 issue, it's seemingly random and once it's happened on a video, I've not been able to make it happen on that video again. The issue only happens very rarely.

In regards to how I figured it out, that's simple: I viewed the source code for the website, manually found the URL of the video and then came up with an algorithm to automatically find the URL. Nothing special, fancy, or particularly complicated stuff.

@ApolloThirteen
Copy link

Hi there, your script seems to be awesome, but as a beginner, there must be something I don't understand correctly.

I'm able to download any normal videos from PH, except the premium videos.
When I reach a premium video, I don't manage to see the "Download(GM)" tab.
In fact, I don't see any of the usual tabs, such as "Report", or "Download" or "Share" and so on.

Any ideas of why this happens? I could use some help here!

@Riboe
Copy link
Author

Riboe commented Jan 30, 2021

@ApolloThirteen Do you actually have access to view those premium videos?

The script does not bypass any paywalls or anything like that. It was originally made, so that people with a PornHub Premium trial could download the premium videos, since PornHub apparently only allows downloading those, if you have non-trial premium.

@GayMerGirlGwen
Copy link

Hey, sorry to bother, love the script, but recently adblock has started blocking the download button. Is there a way to exclude that element?

@thebanker7
Copy link

No longer works. Download button opens popup ad now since Nov 1st

@Riboe
Copy link
Author

Riboe commented Nov 16, 2022

@thebanker7

It seems to work perfectly fine. Make sure you use the button labelled "Download (GM)".

The popups are not from the script, as it does not create a popup or any other kind of ad. It creates an entirely new button, which only displays the download links. PH isn't modifying the button generated from the script to add ads to it either.

@thebanker7
Copy link

The download button is preset, the issue is PH ad above the report, share, download is being blocked by some sort inviable border. When clicking on download (gm) link, the ad above opens instead. Using opera browser.

@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.

@scottodono
Copy link

Anyway to get this to work in Brave browser? I love it because it blocks the ads but when I click the Download tab , no links are displayed

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