20

I used to download songs the following way:

from pytube import YouTube
video = YouTube('https://www.youtube.com/watch?v=AWXvSBHB210')
video.streams.get_by_itag(251).download()

Since today there is this error:

Traceback (most recent call last):
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 170, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 387, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 293, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 278, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Me\Documents\YouTubeDownloader.py", line 3, in <module>
    video.streams.get_by_itag(251).download()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 285, in streams
    return StreamQuery(self.fmt_streams)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 177, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 387, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 293, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\cipher.py", line 278, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Sciveo
  • 329
  • 1
  • 2
  • 7
  • Does this answer your question? [I was making a youtube video downloader but pytube is giving error](https://stackoverflow.com/questions/68956373/i-was-making-a-youtube-video-downloader-but-pytube-is-giving-error) – Hussam F. Alkdary Aug 29 '21 at 13:00
  • Try the following one. In my case it worked. python -m pip install git+https://github.com/kinshuk-h/pytube – NeuroMorphing Apr 17 '22 at 19:16
  • 1
    The latest instance of this issue is [here](https://github.com/pytube/pytube/issues/1281) – snakecharmerb Apr 19 '22 at 12:56

8 Answers8

36

Because youtube changed something on its end, and now you have to change pytube's cipher.py's get_throttling_function_name variable function_patterns to the following

r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'
r'\([a-z]\s*=\s*([a-zA-Z0-9$]{2,3})(\[\d+\])?\([a-z]\)'

And you also have to change line 288 to this:

nfunc=re.escape(function_match.group(1))),

You'll have to use this workaround until pytube officially releases a fix.

EDIT: This will very likely be broken again in the future as youtube updates their website, make sure to check the github issues for possible solutions

eroc123
  • 624
  • 1
  • 4
  • 14
15

I had same issue when i was using pytube 11.0.0

so found out that there is a regular expression filter mismatch in pytube library in cipher.py class

function_patterns = [

    r'a\.C&&\(b=a\.get\("n"\)\)&&\(b=([^(]+)\(b\),a\.set\("n",b\)\)}};',
]

Now there is a update of pytube code yesterday to 11.0.1

function_patterns = [

    r'a\.[A-Z]&&\(b=a\.get\("n"\)\)&&\(b=([^(]+)\(b\)',
]

With this code update now downloading youtube video with pytube works!!!

Update your pytube library with this command:

python3 -m pip install --upgrade pytube
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Lavanya Rani
  • 194
  • 1
  • 3
9

Update your (cipher.py -> get_throttling_function_name -> function_patterns) with this one and then it will work.

function_patterns = [
    # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
    # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
    # var Bpa = [iha];
    # ...
    # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
    # Bpa.length || iha("")) }};
    # In the above case, `iha` is the relevant function name
    r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
    r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)', ]
Maaz Ali
  • 133
  • 1
  • 6
7

In Pytube version 15.0.0, you just need to remove ; in line 287 of cipher.py file.

Change r'var {nfunc}\s*=\s*(\[.+?\];)'.format( to r'var {nfunc}\s*=\s*(\[.+?\])'.format(

Anoop Janakar
  • 147
  • 2
  • 8
4

The updated regex expression:

r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'

r'\([a-z]\s*=\s*([a-zA-Z0-9$]{2,3})(\[\d+\])?\([a-z]\)'

in the answer above may be parsed incorrectly by pycharm if just copy/pasted from web. To fix, try merging the two strings onto one line:

r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*\([a-z]\s*=\s*([a-zA-Z0-9$]{2,3})(\[\d+\])?\([a-z]\)'

I found this fixed the problem for V12.0.0

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Andrzej May
  • 101
  • 1
  • 3
3

You can use yt-dlp: https://github.com/yt-dlp/yt-dlp

!pip install -U yt-dlp

Then for your video (mp4 + 1080p) use the following code:

!yt-dlp -f "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" "https://www.youtube.com/watch?v=AWXvSBHB210"
Slybot
  • 588
  • 1
  • 11
0

Upgrade pytube

python3 -m pip install --upgrade pytube

0

Thank you, that resolved that error - pytube is now giving various other errors, and by adding the following at the lines where the errors occur solved it for me:

Around line 417...

if(raw_code == ""):
    return ""

Around line 344

if(raw_code == ""):
    return []

And around 321 change it to:

if(match != None):
    code_lines_list = find_object_from_startpoint(js, match.span()[1]).split('\n')
    joined_lines = "".join(code_lines_list)
    # Prepend function definition (e.g. `Dea=function(a)`)
    return match.group(0) + joined_lines
else:
    return ""

After this it started working for me.

k_guk
  • 46
  • 5