如何使用pytube通过python下载年龄限制的视频(音乐)

问题描述 投票:0回答:1

我已经制作了可以下载mp4视频并将其导出为mp3的代码。您可以选择要下载的播放列表或视频。 我尝试过:在我的代码中,它会尝试下载视频,但如果年龄限制,它会跳过它。 我怎样才能下载有年龄限制的视频。 这是我的代码:

from pytube import *
from moviepy.editor import *
import os

choice  = input('What do you want to dowload playlist or the video? (P,V) ')

if choice == 'V':

    #ask for the link from user
    link = input("Enter the link of YouTube video you want to download:  ")
    yt = YouTube(link)



    #Showing details
    print("Title: ",yt.title)
    print("Number of views: ",yt.views)
    print("Length of video: ",yt.length)
    print("Author of video: ",yt.author)
    print("Rating of video: ",yt.rating)

    try:
        #Getting the highest resolution possible
        youtube_stream = yt.streams.get_highest_resolution()

        #Starting download
        print("Downloading...")
        youtube_stream.download()
        print("Download completed!!")


        # Load the mp4 file
        video = VideoFileClip(f"{yt.title}.mp4")

        # Extract audio from video
        video.audio.write_audiofile(f"{yt.title}.mp3")
    except:
        print('Sorry, this video is age restricted.')

elif choice == 'P':
    restricted_videos = []
    restricted_videos_links = []

    playlist = input('Enter your playlist link: ')
    playlist = Playlist(playlist)

    print('NOTE: if your video is age restricted it won\'t be downloaded! \n')

    for video in playlist.videos:
        try:
            print(f'Downloading {video.title}...')
            video_stream = video.streams.get_highest_resolution()
            video_stream.download()
            print('Downloaded.')
            # Load the mp4 file
            new_video = VideoFileClip(f"{video.title}.mp4")

            new_video.audio.write_audiofile(f"{video.title}.mp3")
        except:
            print(f'This video: {video.title} is age restricted!')
            restricted_videos.append(video.title)
            restricted_videos_links.append(str(video))
            pass


    restricted_videos_names = '\n'.join(restricted_videos)
    print(f'Video titles they hasn\'t been downloaded: {restricted_videos_names}')

提前致谢!

python youtube pytube
1个回答
0
投票

你必须找到你的 pytube libry floder 并在该 floder inertube.py 223 行 clinen 名称中更改为“ANDROID_MUSIC”,并且你必须更改“ANDROID”

© www.soinside.com 2019 - 2024. All rights reserved.