pythonyoutubepytube

How to download age restricted videos(musics) with python using pytube


I have made the code that can download and export mp4 video to mp3. you can choose what do you want to download playlist or video. i have try: in my code so it tries to download video but if if age restricted it skippes it. how can i download age restricted videos too. this is my code:

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}')

thanks in advance!


Solution

  • You have to find your pytube library folder and change inside that folder inertube.py 223 line clinen name is "ANDROID_MUSIC" and you have to change "ANDROID".