Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MAIN SCRIPT FUNCTIONS
- * Main functions contain minimal unique code and are designed to chain smaller script modules
- * to generate a desired output.
- *
- * FOR VLC MAIN FUNCTIONS:
- * VLC sometimes parses a command line internally. Documentation on this is poor. This will result
- * in buggy behavior. During testing most coming bugs have been worked around (mainly URL parsing).
- * More specific bugs may still appear. Please post on the forum if you encounter odd behavior.
- */
- "_Initialize"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_INIFile;
- global $g_BATFile;
- $g_INIFile = "<xyscripts>\YouTube.ini";
- $g_BATFile = "<xyscripts>\YouTube.bat";
- if (exists($g_INIFile)){
- $g_Vlc = getkey ('Vlc', 'Config', $g_INIFile, );
- $g_Downloads = getkey ('Downloads', 'Config', $g_INIFile, );
- } else {
- msg <<<#
- This is your first time running YouTube.xys 0.6 or you have deleted your configuration file. This configuration wizard helps you quickly configure the script for use. If you wish to change the configuration at a later stage you can do so by editing this file:
- <xyscripts>\YouTube.ini
- This wizard will configure the script through these steps:
- 1. Select vlc.exe
- 2. Select download location
- #;
- $g_Vlc = inputfile ('C:', 'exe', 'Select vlc.exe');
- $g_Downloads = inputfolder ('C:', 'Select download location');
- $Configuration = <<<#
- [Config]
- Vlc=$g_Vlc
- Downloads=$g_Downloads
- #;
- writefile ($g_INIFile, $Configuration, o, t);
- msg <<<#
- The script is now configured.
- #;
- }
- }
- "Decode to CDN Link"
- {
- global $g_YoutubeSourceUrl;
- global $g_YoutubeSourceContent;
- $g_YoutubeSourceUrl = "<clipboard>";
- sub "_Youtube-Get-SourceData";
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- text $g_PlayableUrl;
- }
- "1 Link - XY"
- {
- global $g_Downloads;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- sub "_Return-Playable-Url";
- download ($g_PlayableUrl, "$g_Downloads\$g_MetaTitle$g_VersionsMenuChoiceExt", o);
- }
- "1 Link - VLC"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- sub "_Return-Playable-Url";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} :sout-keep --play-and-exit';
- text $VlcCmd;
- writefile ($g_BATFile, $VlcCmd."<crlf>del ".'"'.$g_BATFile.'"', o, t);
- run ($VlcCmd, , 0, 0);
- }
- "1 Link - VLC (Audio only)"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- sub "_Return-Playable-Url";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#transcode{vcodec=none}:file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} :sout-keep --play-and-exit';
- writefile ($g_BATFile, $VlcCmd."<crlf>del ".'"'.$g_BATFile.'"', o, t);
- run ($VlcCmd, , 0, 0);
- }
- "1 Link - VLC (Video only)"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- sub "_Return-Playable-Url";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#transcode{acodec=none}:file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} :sout-keep --play-and-exit';
- text $VlcCmd;
- writefile ($g_BATFile, $VlcCmd."<crlf>del ".'"'.$g_BATFile.'"', o, t);
- run ($VlcCmd, , 0, 0);
- }
- "Multiple Links - XY"
- {
- global $g_Downloads;
- $YoutubeSourceContentArray = '';
- $DownloadQueue = '';
- $Urls = input ("Download Multiple Links - XY", "Paste 1 Youtube link per line followed by Enter", , m, , , );
- //Load all Youtube sources into $YoutubeSourceContentArray
- foreach ($Url, $Urls, "<crlf>"){
- global $g_YoutubeSourceUrl;
- global $g_YoutubeSourceContent;
- $g_YoutubeSourceUrl = $Url;
- sub "_Youtube-Get-SourceData";
- //List delimiter must be multiple characters as sourcecode may contain all unicode characters
- //|159@@@@@753|
- $YoutubeSourceContentArray = $YoutubeSourceContentArray.'|159@@@@@753|'.$g_YoutubeSourceContent;
- }
- $YoutubeSourceContentArray = formatlist ($YoutubeSourceContentArray, e, '|159@@@@@753|', );
- //Ask the user what quality to download per video and store the result in $DownloadQueue
- foreach ($a, $YoutubeSourceContentArray, '|159@@@@@753|'){
- //Can not use $g_YoutubeSourceContent directly instead use $a as intermediate
- $g_YoutubeSourceContent = $a;
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- global $g_VersionsMenuChoiceExt;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- //Smuggle filename with into URL. We regex out the filename later and delete it leaving only the URL
- //This code needs clean-up at some point.
- //This could be done in a cleaner way by using buffer files.
- $Filename = "$g_MetaTitle$g_VersionsMenuChoiceExt";
- $DownloadQueue = $DownloadQueue.'|'.$g_PlayableUrl.">$Filename>";
- }
- $DownloadQueue = formatlist ($DownloadQueue, e, '|', );
- foreach ($Link, $DownloadQueue, '|'){
- $Filename = regexmatches ($Link, '[>][^>]+[>]', , 0);
- $Filename = replace ($Filename, '>', '', , , );
- $Link = regexreplace ($Link, '[>][^>]+[>]', '', );
- download ($Link, "$g_Downloads\$Filename", o);
- }
- }
- "Multiple Links - VLC"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_YoutubeSourceUrl;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- writefile ($g_BATFile, '', o, t);
- $Urls = input ("Download Multiple Links - VLC", "Paste 1 Youtube link per line followed by Enter", , m, , , );
- foreach ($Url, $Urls, "<crlf>"){
- $g_YoutubeSourceUrl = $Url;
- sub "_Youtube-Get-SourceData";
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- global $g_VersionsMenuChoiceExt;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} --play-and-exit';
- //For unknown reasons the parameter %2C causes VLC to quit when multiple files are queued up in a batch file. When only a single file is present the batch file will run. As a countermeasure I'm replace %2C by comma. If anyone knows why this behavior occurs please let me know.
- $VlcCmd = replace ($VlcCmd, '%2C', ',', , , );
- writefile ($g_BATFile, $VlcCmd."<crlf>", a, t);
- }
- writefile ($g_BATFile, "del ".'"'.$g_BATFile.'"', a, t);
- run ($g_BATFile, , 0, 0);
- }
- "Multiple Links - VLC (Audio only)"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_YoutubeSourceUrl;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- writefile ($g_BATFile, '', o, t);
- $Urls = input ("Download Multiple Links - VLC", "Paste 1 Youtube link per line followed by Enter", , m, , , );
- foreach ($Url, $Urls, "<crlf>"){
- $g_YoutubeSourceUrl = $Url;
- sub "_Youtube-Get-SourceData";
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- global $g_VersionsMenuChoiceExt;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#transcode{vcodec=none}:file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} :sout-keep --play-and-exit';
- //For unknown reasons the parameter %2C causes VLC to quit when multiple files are queued up in a batch file. When only a single file is present the batch file will run. As a countermeasure I'm replace %2C by comma. If anyone knows why this behavior occurs please let me know.
- $VlcCmd = replace ($VlcCmd, '%2C', ',', , , );
- writefile ($g_BATFile, $VlcCmd."<crlf>", a, t);
- }
- writefile ($g_BATFile, "del ".'"'.$g_BATFile.'"', a, t);
- run ($g_BATFile, , 0, 0);
- }
- "Multiple Links - VLC (Video only)"
- {
- global $g_Vlc;
- global $g_Downloads;
- global $g_BATFile;
- global $g_YoutubeSourceUrl;
- global $g_MetaTitle;
- global $g_VersionsMenuChoiceExt;
- global $g_PlayableUrl;
- writefile ($g_BATFile, '', o, t);
- $Urls = input ("Download Multiple Links - VLC", "Paste 1 Youtube link per line followed by Enter", , m, , , );
- foreach ($Url, $Urls, "<crlf>"){
- $g_YoutubeSourceUrl = $Url;
- sub "_Youtube-Get-SourceData";
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- global $g_VersionsMenuChoiceExt;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- $VlcCmd = '"'.$g_Vlc.'" "'.$g_PlayableUrl.'" :sout=#transcode{acodec=none}:file{dst="'.$g_Downloads.'\'.$g_MetaTitle.$g_VersionsMenuChoiceExt.'"} :sout-keep --play-and-exit';
- //For unknown reasons the parameter %2C causes VLC to quit when multiple files are queued up in a batch file. When only a single file is present the batch file will run. As a countermeasure I'm replace %2C by comma. If anyone knows why this behavior occurs please let me know.
- $VlcCmd = replace ($VlcCmd, '%2C', ',', , , );
- writefile ($g_BATFile, $VlcCmd."<crlf>", a, t);
- }
- writefile ($g_BATFile, "del ".'"'.$g_BATFile.'"', a, t);
- run ($g_BATFile, , 0, 0);
- }
- /* SUPPORT SCRIPT FUNCTIONS
- * Support functions are designed to perform stand-alone tasks only and return results to the main script functions
- */
- //------------------------------------Youtube Support scripts------------------------------------//
- "_Return-Playable-Url"
- {
- global $g_YoutubeSourceUrl;
- global $g_YoutubeSourceContent;
- $g_YoutubeSourceUrl = "<clipboard>";
- sub "_Youtube-Get-SourceData";
- global $g_MetaTitle;
- global $g_MetaDescription;
- sub "_SourceData-Get-MetaData";
- global $g_StreamMap;
- sub "_SourceData-Get-StreamMap";
- sub "_StreamMap-Decode";
- global $g_Itags;
- sub "_StreamMap-Get-Versions";
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- sub "_Versions-Get-Choice";
- global $g_PlayableUrl;
- sub "_Choice-Create-PlayableUrl";
- }
- "_Youtube-Get-SourceData"
- {
- global $g_YoutubeSourceUrl;
- global $g_YoutubeSourceContent;
- if (regexmatches ($g_YoutubeSourceUrl, "http[s]?://www\.youtube\.com[^ ]+", , 0)){
- } else {
- $g_YoutubeSourceUrl = input ("Youtube.com", "Paste Youtube video URL here:", , s, , 300, 200);
- }
- $g_YoutubeSourceContent = readurl ($g_YoutubeSourceUrl, 0, 0, 4);
- }
- "_SourceData-Get-MetaData"
- {
- global $g_YoutubeSourceContent;
- global $g_MetaTitle;
- global $g_MetaDescription;
- $g_MetaTitle = regexmatches ($g_YoutubeSourceContent, '"og:title" content="[^"]+', , 0);
- $g_MetaTitle = replace ($g_MetaTitle, '"og:title" content="', , , );
- $g_MetaDescription = regexmatches ($g_YoutubeSourceContent, '"og:description" content="[^"]+', , 0);
- $g_MetaDescription = replace ($g_MetaDescription, '"og:description" content="', , , );
- //Convert HTML character codes
- //Remove &|' because they interfere with VLC
- $g_MetaTitle = replacelist ($g_MetaTitle, '"|"|&|&|'|'|<|<|>|>', "||&|&|||<|<|>|>", '|', 0);
- $g_MetaDescription = replacelist ($g_MetaDescription, '"|"|&|&|'|'|<|<|>|>', "||&|&|||<|<|>|>", '|', 0);
- //Remove all NTFS filename illegal characters.
- //Remove comma because it interferes with VLC
- $g_MetaTitle = replacelist ($g_MetaTitle, '/-,-\-:-*-?-<->-"-|', , "-", 0);
- $g_MetaDescription = replacelist ($g_MetaDescription, '/-,-\-:-*-?-<->-"-|', , "-", 0);
- }
- "_SourceData-Get-StreamMap"
- {
- global $g_RegEx_Haystack;
- global $g_RegEx_Needles;
- global $g_RegEx_Marker;
- global $g_RegEx_Result;
- global $g_YoutubeSourceContent;
- global $g_StreamMap;
- $g_RegEx_Haystack = $g_YoutubeSourceContent;
- $g_RegEx_Needles = '"url_encoded_fmt_stream_map": "[^"]+~\w+=[^,]+';
- $g_RegEx_Marker = '~';
- sub "_RegEx_Iso";
- $g_StreamMap = $g_RegEx_Result;
- }
- "_StreamMap-Decode"
- {
- global $g_StreamMap;
- $Streams = '';
- foreach ($Map, $g_StreamMap, "|"){
- $Map = urldecode ($Map, 1);
- $Map = replacelist ($Map, '\u0026,sig', '&,signature', ",", 0);
- $Streams = $Streams.$Map.'|';
- }
- $g_StreamMap = formatlist ($Streams, "e", "|", );
- }
- "_StreamMap-Get-Versions"
- {
- global $g_StreamMap;
- global $g_Itags;
- $g_Itags = regexmatches ($g_StreamMap, 'itag=\d+', '|', 0);
- $g_Itags = replace ($g_Itags, 'itag=', , , , );
- $g_Itags = formatlist ($g_Itags, d, '|', );
- }
- "_Versions-Get-Choice"
- {
- global $g_VersionsDatabase;
- global $g_VersionsMenuList;
- global $g_VersionsMenuChoice;
- global $g_VersionsMenuChoiceExt;
- global $g_Itags;
- global $g_MetaTitle;
- global $g_MetaDescription;
- $g_VersionsDatabase = <<<#
- [036] 240p - 3GP
- [017] 144p - 3GP
- [013] Unknown - 3GP
- [035] 480p - FLV
- [034] 360p - FLV
- [006] 270p - FLV
- [005] 240p - FLV
- [102] 360p - WebM (3D)
- [101] 360p - WebM (3D)
- [100] 720p - WebM (3D)
- [046] 1080p - WebM
- [045] 720p - WebM
- [044] 480p - WebM
- [043] 360p - WebM
- [085] 520p - MP4 (3D)
- [084] 720p - MP4 (3D)
- [083] 240p - MP4 (3D)
- [082] 360p - MP4 (3D)
- [038] 3072p - MP4
- [037] 1080p - MP4
- [022] 720p - MP4
- [018] 360p - MP4
- #;
- // Loop compares tag numbers found in the Youtube source to a database of corresponding quality levels
- // and creates list $g_VersionsMenuList that allows users to choose which video quality they want to download.
- $g_VersionsMenuList = '';
- foreach ($Tag, $g_Itags, '|'){
- $Tag = format ($Tag, '000');
- $g_VersionsMenuList = $g_VersionsMenuList.regexmatches($g_VersionsDatabase, "\[$Tag\][^\[]+", , 0).'|';
- }
- $g_VersionsMenuChoice = inputselect ("Choose video quality:<crlf 2>$g_MetaTitle", $g_VersionsMenuList, "|", 0, , , , );
- $g_VersionsMenuChoiceExt = '.'.recase(regexmatches ($g_VersionsMenuChoice, "3GP|FLV|MP4|WebM", , 0), l, );
- // After selecting a quality we retrieve the itag numerical value to use it in the PlayableUrl
- $g_VersionsMenuChoice = regexmatches ($g_VersionsMenuChoice, "\[\d+\]", , 0);
- $g_VersionsMenuChoice = replacelist ($g_VersionsMenuChoice, '[,]', , ',', );
- $g_VersionsMenuChoice = trim ($g_VersionsMenuChoice, "0", "L");
- }
- "_Choice-Create-PlayableUrl"
- {
- global $g_StreamMap;
- global $g_VersionsMenuChoice;
- global $g_PlayableUrl;
- $g_PlayableUrl = regexmatches ($g_StreamMap, "[^|]+([&]?itag=$g_VersionsMenuChoice[^|]+)", , 0);
- $HttpFront = regexmatches ($g_PlayableUrl, "http[s]?:[^&]+", , 0);
- $g_PlayableUrl = regexreplace ($g_PlayableUrl, "[&]?(type|quality|itag|fallback_host|url)=[^&]+", , 0);
- $g_PlayableUrl = regexreplace ($g_PlayableUrl, "[?][&]", '?', 0);
- $g_PlayableUrl = $HttpFront.'&'.$g_PlayableUrl."&itag=$g_VersionsMenuChoice";
- //Prevents && appearing in the URL
- $g_PlayableUrl = replace ($g_PlayableUrl, '&&', '&', , , );
- }
- //------------------------------------Regular Expression Engine------------------------------------//
- "_RegEx_Iso"
- {
- /*
- _RegEx_Iso is a sub script that will recursively search a text source.
- By using sequenced regular expressions it is able to isolate embedded text that you
- can not isolate using just a single regular expression.
- You must define global variables Haystack, Needles, and Marker in your script as the "arguments"
- to the "method" _RegEx_Iso.
- E.g:
- $g_RegEx_Haystack = $Source;
- $g_RegEx_Needles = "www\.youtube\.com\/user\/[^""]+"."~"."[^\/]+$";
- $g_RegEx_Marker = "~";
- sub "_RegEx_Iso";
- text $g_RegEx_Result;
- */
- global $g_RegEx_Haystack; // Text source to search in
- global $g_RegEx_Needles; // List of recursive regex to be used
- global $g_RegEx_Marker; // Delimiting character used to sequence list of regex
- global $g_RegEx_Result; // Result of the search are demarked by "|"
- $j = $g_RegEx_Haystack;
- $k = $g_RegEx_Needles;
- $l = $g_RegEx_Marker;
- foreach ($i, $k, $l){
- $j = regexmatches ("$j", "$i", "|", "0");
- }
- $g_RegEx_Result = $j;
- }
- "_DEBUGRegEx_Iso"
- {
- /*
- _DEBUGRegEx_Iso will do exactly the same as "_RegEx_Iso".
- In addition it will show the Haystack, Needle, Marker and Result for each
- recursive regular expression cycle. Useful to identify buggy behavior.
- */
- global $g_RegEx_Haystack;
- global $g_RegEx_Needles;
- global $g_RegEx_Marker;
- global $g_RegEx_Result;
- $j = $g_RegEx_Haystack;
- $k = $g_RegEx_Needles;
- $l = $g_RegEx_Marker;
- foreach ($i, $k, $l){
- text "$j";
- $j = regexmatches ("$j", "$i", "|", "0");
- text "$j <crlf 2>$i <crlf 2>$l";
- }
- $g_RegEx_Result = $j;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement