-1

I've written the following script to download mp4 videos from youtube using pytube API:

from pytube import YouTube
import sys 


SAVE_PATH = input('Enter a saving path: ') 
LINK = input('Enter the link of the video: ')

try:
    yt = YouTube(LINK)
except:
    print('Connection error') 

print(yt.title)
items = yt.streams.filter(only_audio=True).all()
stream = items[0]

stream.download()

When i run the code i get the following error :

Connection error                                                                          
Traceback (most recent call last):                                                          
File "C:\Users\Dell\Desktop\Python\youtube.py", line 24, in <module>                        
  print(yt.title)                                                                       
NameError: name 'yt' is not defined 

What's the problem and how can i fix it ?

Garde Des Ombres
  • 173
  • 1
  • 3
  • 12

3 Answers3

0

The problem is in the video-link you are giving to the program, if video-link is not proper or internet connection is not proper the yt will not be initialized and when it is not initialized you can't use it.

  1. Try using a predefined link without taking input from user such as

    LINK= #any youtube-video link

  2. If your program fails after giving a predefined link then your internet connection is not proper.

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
0

It should be yt = pytube.YouTube(LINK)

XyYang
  • 31
  • 4
0

I think the answer to this question is that every mention to "yt" variable should be into the "try" block

Ryuzaki
  • 1
  • 1