Advertisement
anonymous1184

ytdl.ahk

Apr 27th, 2021
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv
  2.  
  3. ; youtube-dl binary path
  4. global ytdl := "D:\CLI\youtube-dl"
  5.  
  6. /* ffmpeg
  7. ffmpeg must be accessible to youtube-dl for
  8. format conversions. The function expects it
  9. to be in the same level as the application.
  10.  
  11. Example:
  12.     D:\CLI\ffmpeg
  13.     D:\CLI\youtube-dl
  14.  
  15. To change, Use the option --ffmpeg-location
  16. */
  17.  
  18. EnvGet UserProfile, USERPROFILE
  19.  
  20. music := UserProfile "\Music"
  21. video := UserProfile "\Video"
  22.  
  23.  ^y::ytdl(music, 0) ; Audio
  24. +^y::ytdl(video, 1) ; Video
  25.  
  26. ytdl(outDir, video := true)
  27. {
  28.     if !url := getUrl(WinActive("A"))
  29.     {
  30.         MsgBox % 0x10|0x40000, Error, Couldn't retrieve URL.
  31.         return
  32.     }
  33.  
  34.     resource := "v" ; video
  35.     outDir := RTrim(outDir, "\")
  36.     if RegExMatch(url, "\blist=")
  37.     {
  38.         MsgBox % 0x4|0x20|0x40000, Playlist, Do you want to download the full playlist?
  39.         IfMsgBox Yes
  40.             resource := "list"
  41.             , outDir .= "\%(playlist)s"
  42.     }
  43.     outDir .= "\%(title)s.%(ext)s"
  44.  
  45.     if !RegExMatch(url, resource "=(?<Id>[\w-]+)", resource)
  46.     {
  47.         MsgBox % 0x10|0x40000, Error, Cannot parse the URL.
  48.         return
  49.     }
  50.  
  51.     ; Order of the options:
  52.     ; youtube-dl.exe --help
  53.     cmd := "youtube-dl.exe"
  54.         ; General Options
  55.         . " --ignore-errors"
  56.         . " --ignore-config"
  57.         ; Network Options
  58.         . " --force-ipv4"
  59.         ; Geo restriction
  60.         ; . " --geo-bypass"
  61.         ; . " --geo-bypass-country US"
  62.         . " --geo-bypass-ip-block 35.190.247.0/24" ; Google's
  63.         ; Video Selection
  64.         . " --yes-playlist"
  65.         ; Download Options
  66.         . " --hls-prefer-native"
  67.         ; . " --hls-prefer-ffmpeg"
  68.         ; Filesystem Options
  69.         . " --output """ outDir """"
  70.         . " --no-overwrites"
  71.         . " --continue"
  72.         . " --no-part"
  73.         . " --cookies .\cache\cookies.txt"
  74.         . " --cache-dir .\cache"
  75.         ; Verbosity
  76.         . " --no-warnings"
  77.         . " --console-title"
  78.         . " --no-call-home"
  79.         ; Workarounds
  80.         . " --no-check-certificate"
  81.         . " --prefer-insecure"
  82.         ; Mimic latest Firefox (as of Apr 27, 2021) when reloading the page without cache:
  83.         . " --user-agent ""Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"""
  84.         . " --referer " StrReplace(url, "&", "^&")
  85.         . " --add-header Accept-Encoding:identity"
  86.         . " --add-header Accept-Language:en-US,en;q=0.5"
  87.         . " --add-header Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  88.         . " --add-header Cache-Control:no-cache"
  89.         . " --add-header Connection:keep-alive"
  90.         . " --add-header DNT:1"
  91.         . " --add-header Pragma:no-cache"
  92.         . " --add-header TE:trailers"
  93.         . " --add-header Upgrade-Insecure-Requests:1"
  94.         . " --sleep-interval 0" ; Increase when changing 'geo-bypass'
  95.         ; Video Format Options
  96.         . " --youtube-skip-dash-manifest"
  97.         . " --merge-output-format mkv"
  98.         ; Subtitle Options
  99.         ; Authentication Options
  100.         ; Adobe Pass Options
  101.         ; Post-processing Options
  102.         . " --add-metadata"
  103.         . " --ffmpeg-location ..\ffmpeg"
  104.  
  105.     if video ; Video in a Matroska container
  106.         cmd .= " --embed-thumbnail"
  107.             ; 8k  = 4320
  108.             ; 4k  = 2160
  109.             ; QHD = 1440
  110.             ; FHD = 1080
  111.             ; HD  =  720
  112.             . " --format bestvideo[height<=1080]+bestaudio"
  113.     else ; AAC audio compatible with phones
  114.         cmd .= " --format bestaudio[ext=m4a]"
  115.  
  116.     ; Other options by format code:
  117.     ; https://voussoir.net/writing/youtubedl_formats
  118.  
  119.     RunWait % cmd " " resourceId, % ytdl
  120. }
  121.  
  122. #Inlcude getUrl.ahk
  123.  
  124. ; See:
  125. ; https://gist.github.com/anonymous1184/7cce378c9dfdaf733cb3ca6df345b140#geturl
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement