使用pytube+tkinter开发YouTube音频下载器遇ValueError问题求助
Hey there, this error is super common with pytube lately—YouTube keeps updating their video signature encryption logic, and older versions of pytube can't keep up with the changes. Let's walk through the fixes step by step:
1. Update pytube to the latest development version
The stable version on PyPI often lags behind when YouTube pushes updates. Install the latest build directly from GitHub, which usually has the cipher fixes already implemented:
pip install --upgrade git+https://github.com/pytube/pytube.git
After running this, restart your script and try downloading again. This should resolve the majority of cipher-related errors.
2. Manual fix for the cipher.py file (if updating doesn't work)
If updating still throws the same error, you can tweak the cipher parsing logic in pytube's source code:
- Navigate to your pytube installation directory:
D:\Python3.8\lib\site-packages\pytube\ - Open
cipher.pyin a text editor - Find the
__init__method around line 31, where you see this code:var, _ = self.transform_plan[0].split(".") - Replace that line with code that handles multiple dots in the transform plan entry:
# Handle cases where the transform plan entry has multiple dots parts = self.transform_plan[0].split(".") var = parts[-2] # Extract the second-to-last part, which is the variable name - Save the file and run your script again.
3. Consider switching to yt-dlp (more stable long-term)
Pytube tends to break frequently because of YouTube's updates. If you want a more reliable solution, try yt-dlp—it's a maintained fork of youtube-dl with better compatibility and features. Here's how to adjust your code:
First install yt-dlp:
pip install yt-dlp
Then rewrite your download function to use yt-dlp (this even converts to MP3 automatically, so you can skip the manual rename step):
def download(): link = textEntry.get() print(link) import yt_dlp # Configure download options to get best audio and convert to MP3 ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], 'outtmpl': '%(title)s.%(ext)s', # Save file with the video title } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([link])
This will download the highest quality audio and convert it directly to MP3, saving you the extra conversion step in your original code.
Quick note
YouTube regularly changes their frontend code to block scrapers, so tools like pytube need constant updates. Using yt-dlp can save you from having to fix these errors every few weeks.
内容的提问来源于stack exchange,提问作者user14021204

