0

For some reason I don't understand, the codes do not work, strangely, I do not get an error, it just does not save where I want, it saves to the folder where the file is run.

af = input("Link:")
yt = YouTube(af, on_progress_callback=progress_callback)
stream = yt.streams.get_highest_resolution()
print(f"Downloading video to '{stream.default_filename}'")
pbar = tqdm(total=stream.filesize, unit="bytes")
path = stream.download()
#Download path
b = open("\Download", "w")
b.write(stream.download())
b.close()
pbar.close()
print(Fore.LIGHTGREEN_EX+"Saved video to {}".format(path))
time.sleep(10)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
W3gor
  • 5
  • 4

1 Answers1

0

First, to save the file in a specific directory we need to create the directory, I will do this through this function

import sys, os    
def createDirectory(name):
    path = sys.path[0] # Take the complete directory to the location where the code is, you can substitute it to the place where you want to save the video.
    if not(os.path.isdir(f'{path}/{name}')): # Check if directory not exist
        path = os.path.join(sys.path[0], name) # Set the directory to the specified path and the name that was given to it
        os.mkdir(path) # Create a directory

After that we need to save the file in the newly created directory, for that within the "stream.download" in the parameter "output_path" we pass the total path to save and then the variable that contains the name of the newly created directory.

directoyName = 'Video'

createDirectory(directoyName)

path = sys.path[0] # Take the complete directory to the location where the code is, you can substitute it to the place where you want to save the video.

stream.download(output_path=f'{path}/{directoyName}') # Save the video in the newly created directory.

This way the file will be saved in the directory with the specified name.