You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pytube+tkinter开发YouTube音频下载器遇ValueError问题求助

Fixing "ValueError: too many values to unpack (expected 2)" in pytube YouTube Downloader

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:

  1. Navigate to your pytube installation directory: D:\Python3.8\lib\site-packages\pytube\
  2. Open cipher.py in a text editor
  3. Find the __init__ method around line 31, where you see this code:
    var, _ = self.transform_plan[0].split(".")
    
  4. 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
    
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:37:46