I tried to download all the videos in a youtube channel and create separate folders for the playlist. I used the following code in youtube-dl.
youtube-dl -f 22 --no-post-overwrites -ciw -o '%(uploader)s/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' https://www.youtube.com/channel/UCYab7Ft7scC83Sk4e9WWqew/playlists
All video files have been downloaded. But you can't play them all. The reason is that the file does not have a name and extention. There's only index no. Here is the screenshot.
This is caused by a minor error in the code. After I saw it. The corrected code should be as follows.
youtube-dl -f 22 --no-post-overwrites -ciw -o '%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s https://www.youtube.com/channel/UCYab7Ft7scC83Sk4e9WWqew/playlists
Once the code is correct, it downloads as follows.
This is the point I want now. All these files should be renamed as playlists. But without re-downloading all video files. I downloaded the json files to get file information. Having a general knowledge of coding, I don't understand how to use it.
I can't download it again. It is difficult to name one. It takes a lot of time. Because there is a lot of file. How do I do this?
For a sanity check, let's see what's currently in your folder:
from pathlib import Path
folder_path = '/Users/kevinwebb/Desktop/test_json'
p = Path(folder_path).glob('**/*')
files = [x for x in p if x.is_file()]
print(files)
Output:
[PosixPath('/Users/kevinwebb/Desktop/test_json/02-Ranking Factor.info.json'),
PosixPath('/Users/kevinwebb/Desktop/test_json/02'),
PosixPath('/Users/kevinwebb/Desktop/test_json/01'),
PosixPath('/Users/kevinwebb/Desktop/test_json/01-How to do- Stuff in Science.info.json')]
Now, we're going to specifically look for json files, get the indices and names, and rename the files.
# Grab all files that have json as extension
json_list = list(Path(folder_path).rglob('*.json'))
# Split on the first occurance of the dash
index = [x.name.split("-",1)[0] for x in json_list]
# Split again on the dot
names = [x.name.split("-",1)[1].split(".",1)[0] for x in json_list]
folder_p = Path(folder_path)
# zipping puts the two lists side by side
# and iteratively goes through both of them
# one by one
for i,name in zip(index,names):
# combine the folder name with the id
p = folder_p / i
# rename file with new name and new suffix
p.rename((p.parent / (i + "-" + name)).with_suffix('.mp4'))
You should now see the newly name mp4 files.
pathlib
module: