-1

I don`t know what is the problem in my code i wanna download youtube playlist with numeric

from pytube import Playlist
from tkinter import filedialog
import os
playlist = Playlist(input("input the link:\n"))
folder_name = filedialog.askdirectory()
print("print total videos in playlist :", len(playlist.video_urls))
v = 1
for video in playlist.videos:
    video.streams.get_by_resolution('360p').download(folder_name) ##remove video.title+str(v)
    os.rename( video.streams.get_by_resolution('360p').default_filename,str(v)+video.title)
    print("Video Downloaded: ", video.title)
    v += 1

And the problem message is

C:\Users\HP\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\HP\PycharmProjects\pythonProject\yt_playlist.py 
input the link:
https://www.youtube.com/playlist?list=PLWlltQ6Oy0zpgxVhd2vZqTDvVXpPhSVd0
print total videos in playlist : 8
Traceback (most recent call last):
  File "C:\Users\HP\PycharmProjects\pythonProject\yt_playlist.py", line 10, in <module>
    os.rename( video.streams.get_by_resolution('360p').default_filename,str(v)+video.title)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'What is Sustainable Agriculture Episode 1 A Whole-Farm Approach to Sustainability.mp4' -> '1What is Sustainable Agriculture? Episode 1: A Whole-Farm Approach to Sustainability'

Process finished with exit code 1

I tried this code and didn`t work

from pytube import Playlist
from tkinter import filedialog
import os
playlist = Playlist(input("input the link:\n"))
folder_name = filedialog.askdirectory()
print("print total videos in playlist :", len(playlist.video_urls))
v = 1
for video in playlist.videos:
    video.streams.get_by_resolution('360p').download(folder_name)
    os.chdir(folder_name)
    os.rename( video.streams.get_by_resolution('360p').default_filename,str(v)+video.title)
    print("Video Downloaded: ", video.title)
    v += 1

it shows this problem

C:\Users\HP\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\HP\PycharmProjects\pythonProject\yt_playlist.py 
input the link:
https://www.youtube.com/playlist?list=PLWlltQ6Oy0zpgxVhd2vZqTDvVXpPhSVd0
print total videos in playlist : 8
Traceback (most recent call last):
  File "C:\Users\HP\PycharmProjects\pythonProject\yt_playlist.py", line 11, in <module>
    os.rename( video.streams.get_by_resolution('360p').default_filename,str(v)+video.title)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'What is Sustainable Agriculture Episode 1 A Whole-Farm Approach to Sustainability.mp4' -> '1What is Sustainable Agriculture? Episode 1: A Whole-Farm Approach to Sustainability'

Process finished with exit code 1

just to know it works with just video not playlists (that`s what happend with me)

1 Answers1

0

You have the error:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'What is Sustainable Agriculture Episode 1 A Whole-Farm Approach to Sustainability.mp4' -> '1What is Sustainable Agriculture? Episode 1: A Whole-Farm Approach to Sustainability'

However when looking in your folder_name directory, you see the file What is Sustainable Agriculture Episode 1 A Whole-Farm Approach to Sustainability.mp4. So os.rename source file is specified in an incorrect directory. Indeed video.streams.get_by_resolution('360p').default_filename isn't taking care of your folder_name.

So to solve your issue you can just move your working directory to folder_name by using os.chdir(folder_name) just after folder_name assignment (folder_name = filedialog.askdirectory()).

So your code should become:

from pytube import Playlist
from tkinter import filedialog
import os
playlist = Playlist(input("input the link:\n"))
folder_name = filedialog.askdirectory()
os.chdir(folder_name)
print("print total videos in playlist :", len(playlist.video_urls))
v = 1
for video in playlist.videos:
    video.streams.get_by_resolution('360p').download(folder_name)
    os.rename( video.streams.get_by_resolution('360p').default_filename,str(v)+video.title)
    print("Video Downloaded: ", video.title)
    v += 1
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • look at my edit ,please it didn`t work – Ahmed Soliman Oct 13 '22 at 04:25
  • [I edited my answer](https://stackoverflow.com/posts/74047046/revisions) as you misplaced `os.chdir(folder_name)`. Note that your new error is different from the old one `OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'What is Sustainable Agriculture Episode 1 A Whole-Farm Approach to Sustainability.mp4' -> '1What is Sustainable Agriculture? Episode 1: A Whole-Farm Approach to Sustainability'` I suspect that your OS doesn't support filename with `?` or `:`. – Benjamin Loison Oct 13 '22 at 08:38
  • [This problem](https://stackoverflow.com/q/295135/7123660) was already raised on StackOverflow, have a look at its answers. – Benjamin Loison Oct 13 '22 at 08:38