尝试使用pytube下载时出现问题

huangapple go评论65阅读模式
英文:

Issue trying to download with pytube

问题

自8个月以来,

我一直在使用自制的YouTube视频和播放列表下载器,使用了pytube包。

但现在,它不再工作,并且每次尝试运行时都显示错误。

重要的是要说,我在尝试下载播放列表和视频时都遇到了相同的问题。

这是我的代码:

# 导入包
from pytube import YouTube
import os
from pytube import Playlist
import time

time_1 = 0
time_2 = 0
times = []
moyenne = 0

def playlist():
    p = Playlist(input('输入播放列表名称  \n>> '))

    for url in p.video_urls:
        time_1 = time.time()
        print(time_1)
        yt = YouTube(url)
        print(yt)

        # 仅提取音频

        audio = yt.streams.filter(only_audio=True).first()

        video = yt.streams.filter(only_video=True).first()

        # 下载文件
        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/BAM")
        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")

        # 保存文件
        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

        # 成功时的结果
        print(yt.title + " 已成功下载。")
        time_2 = time.time()
        print("时间 : ", time_2 - time_1)
        times.append(time_2 - time_1)
        moyenne = sum(times)/len(times)
        print("平均时间 : ", moyenne)
    print("播放列表已下载!")


def video(n):
    yt = YouTube(input("输入视频链接 \n >> "))

    if n == 1:  # 仅视频

        video = yt.streams.filter(only_video=True).first()

        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

    if n == 2: # 仅音频

        audio = yt.streams.filter(only_audio=True).first()

        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/audio")

        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)

    if n == 3:

        video = yt.streams.filter(only_video=True).first()
        audio = yt.streams.filter(only_audio=True).first()

        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")
        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/audio")

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)

    print(yt.title + " 已成功下载。")


type_ = input("播放列表 (p) 或视频 (v) ? \n >> ")


if type_ == "p":
    playlist()
elif type_ == "v":
    n = int(input(": "))
    video(n)
else:
    print("ValueError")

这是我在尝试下载播放列表时收到的错误:

Traceback (most recent call last):

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 97, in <module>

    playlist()

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 23, in playlist

    audio = yt.streams.filter(only_audio=True).first()

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 
            296, in streams
    return StreamQuery(self.fmt_streams)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line    
     161, in streaming_data

    return self.vid_info['streamingData']
KeyError: 'streamingData'

这是我尝试下载视频时收到的错误:

Traceback (most recent call last):

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 100, in <module>

    video(n)
  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 55, in video

    video = yt.streams.filter(only_video=True).first()

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

296, in streams

    return StreamQuery(self.fmt_streams)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

176, in fmt_streams

    stream_manifest = extract.apply_descrambler(self.streaming_data)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

161, in streaming_data

    return self.vid_info['streamingData']

KeyError: 'streamingData'

希望有人可以帮助找出问题所在。

我尝试从教程网站复制粘贴另一个Python YouTube视频下载器到一个新项目中,但即使这一次,我仍然收到相同的错误(我已经检查了它,包已安装,所以不是问题)。 我感到惊讶,因为8个月前,当我尝试从互联网上复制粘贴相同的代码到一个新项目时,代码可以正常工作,没有显示任何错误。

英文:

Since 8 months,

I've been using a self-made Youtube video and playlist downloader using the pytube package.

But now, it doesn't work anymore and shows an error everytime I try to run it.

It's important to say that I have the SAME issue trying to dowload playlists and videos.

Here is my code:

#importing packages
from pytube import YouTube
import os
from pytube import Playlist
import time
time_1 = 0
time_2 = 0
times = []
moyenne = 0
def playlist():
p = Playlist(input(&#39;Enter Playlist name  \n&gt;&gt; &#39;))
for url in p.video_urls:
time_1 = time.time()
print(time_1)
yt = YouTube(url)
print(yt)
# extract only audio
audio = yt.streams.filter(only_audio=True).first()
video = yt.streams.filter(only_video=True).first()
# download the file
out_file_audio = audio.download(output_path=&quot;C:/Users/Eleve/Downloads/BAM&quot;)
out_file_video = video.download(output_path=&quot;C:/Users/Eleve/Downloads/video&quot;)
# save the file
base, ext = os.path.splitext(out_file_audio)
new_file = base + &#39;.mp3&#39;
os.rename(out_file_audio, new_file)
base, ext = os.path.splitext(out_file_video)
new_file = base + &#39;.mp4&#39;
os.rename(out_file_video, new_file)
# result if success
print(yt.title + &quot; has been successfully downloaded.&quot;)
time_2 = time.time()
print(&quot;time : &quot;, time_2 - time_1)
times.append(time_2 - time_1)
moyenne = sum(times)/len(times)
print(&quot;average time : &quot;, moyenne)
print(&quot;Playlist downloaded !&quot;)
def video(n):
yt = YouTube(input(&quot;Enter video url \n &gt;&gt; &quot;))
if n == 1:  #Only video
video = yt.streams.filter(only_video=True).first()
out_file_video = video.download(output_path=&quot;C:/Users/Eleve/Downloads/video&quot;)
base, ext = os.path.splitext(out_file_video)
new_file = base + &#39;.mp4&#39;
os.rename(out_file_video, new_file)
if n == 2: #only audio
audio = yt.streams.filter(only_audio=True).first()
out_file_audio = audio.download(output_path=&quot;C:/Users/Eleve/Downloads/audio&quot;)
base, ext = os.path.splitext(out_file_audio)
new_file = base + &#39;.mp3&#39;
os.rename(out_file_audio, new_file)
if n == 3:
video = yt.streams.filter(only_video=True).first()
audio = yt.streams.filter(only_audio=True).first()
out_file_video = video.download(output_path=&quot;C:/Users/Eleve/Downloads/video&quot;)
out_file_audio = audio.download(output_path=&quot;C:/Users/Eleve/Downloads/audio&quot;)
base, ext = os.path.splitext(out_file_video)
new_file = base + &#39;.mp4&#39;
os.rename(out_file_video, new_file)
base, ext = os.path.splitext(out_file_audio)
new_file = base + &#39;.mp3&#39;
os.rename(out_file_audio, new_file)
print(yt.title + &quot; has been successfully downloaded.&quot;)
type_ = input(&quot;Playlist (p) or video (v) ? \n &gt;&gt; &quot;)
if type_ == &quot;p&quot;:
playlist()
elif type_ == &quot;v&quot;:
n = int(input(&quot;: &quot;))
video(n)
else:
print(&quot;ValueError&quot;)

Here is the error I get when trying to download the playlists:

Traceback (most recent call last):
File &quot;C:\Users\Eleve\YouTubeDownloader2\main.py&quot;, line 97, in &lt;module&gt;
playlist()
File &quot;C:\Users\Eleve\YouTubeDownloader2\main.py&quot;, line 23, in playlist
audio = yt.streams.filter(only_audio=True).first()
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line 
296, in streams
return StreamQuery(self.fmt_streams)
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line 
176, in fmt_streams
stream_manifest = extract.apply_descrambler(self.streaming_data)
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line    
161, in streaming_data
return self.vid_info[&#39;streamingData&#39;]
KeyError: &#39;streamingData&#39;

And here is the error I get when trying to download a video:

Traceback (most recent call last):
File &quot;C:\Users\Eleve\YouTubeDownloader2\main.py&quot;, line 100, in &lt;module&gt;
video(n)
File &quot;C:\Users\Eleve\YouTubeDownloader2\main.py&quot;, line 55, in video
video = yt.streams.filter(only_video=True).first()
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line 
296, in streams
return StreamQuery(self.fmt_streams)
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line 
176, in fmt_streams
stream_manifest = extract.apply_descrambler(self.streaming_data)
File &quot;C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py&quot;, line 
161, in streaming_data
return self.vid_info[&#39;streamingData&#39;]
KeyError: &#39;streamingData&#39;

Hope someone could help to figure out where's the issue

I try to copy-paste another python youtube video downloader from a

tutorial site in a new project, but even this time, I got the same error

(I checked it, and the packages are installed, so it's not the issue).

I'm suprised, because 8 months ago, when I try to copy-paste the same code from the Internet to a new project,

the code worked and didn't show any error worked.

答案1

得分: 0

使用pip list来确定您已安装的pytube版本。如果不是最新版本,请使用pip uninstall pytube来卸载它,然后使用pip install pytube来安装最新版本。

更多信息请参阅此链接:https://stackoverflow.com/questions/76405667/exception-in-tkinter-callback-traceback-most-recent-call-last-error/76406313#76406313

英文:

Use pip list to determine what version of pytube you have installed. If if is not the very lastest version use pip uninstall pytube to remove it and then use pip install pytube to install the latest version.

See this link for more info: https://stackoverflow.com/questions/76405667/exception-in-tkinter-callback-traceback-most-recent-call-last-error/76406313#76406313

huangapple
  • 本文由 发表于 2023年6月25日 20:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定