'Download audio only youtube raises regexmatcherror
I am trying to execute the following code, that is supposed to download the audio file from youtube url
from pathlib import Path
from pytube import YouTube
def download_youtube_video(youtube_url, output_path):
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
audio_file = Path(audio_file)
audio_file = audio_file.replace(audio_file.with_suffix(".mp3"))
return audio_file
youtube_url = 'https://youtu.be/_H5hsUwv8lE'
output_path = Path(__file__).parent
audio_file = download_youtube_video(youtube_url, output_path)
But I got the following traceback
Traceback (most recent call last):
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, 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 "demo.py", line 18, in <module>
audio_file = download_youtube_video(youtube_url, output_path)
File "demo.py", line 6, in download_youtube_video
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 296, in streams
return StreamQuery(self.fmt_streams)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 188, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
Any idea how to fix such a problem?
Solution 1:[1]
My steps to solve the problem:
First, detect the package pytube path using the code
import pytube
import os
print(pytube.__file__)
print(os.path.dirname(pytube.__file__))
Navigate to the directory pytube and modify the cipher.py
The line 273 r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)', changed to r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
The line 288 nfunc=function_match.group(1)), changed to nfunc=re.escape(function_match.group(1))),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | YasserKhalil |
