如何使用 YouTube-DL 根据播放列表中文件名的标题重命名所有下载的文件?

How to rename all downloaded files by the title of the filename in the playlist using YouTube-DL?

我尝试下载 YouTube 频道中的所有视频并为播放列表创建单独的文件夹。我在 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

所有视频文件已下载完毕。但你不能全部播放。原因是该文件没有名称和扩展名。只有索引号。这是屏幕截图。

这是由代码中的一个小错误引起的。我看到之后。更正后的代码应如下所示。

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

一旦代码正确,下载如下。

这就是我现在要的点 所有这些文件都应重命名为播放列表。但没有 re-downloading 所有视频文件。 我下载了 json 文件以获取文件信息。对编码有一定的了解,不知道怎么用。

我无法再次下载。很难说出一个。这需要很多时间。因为文件很多。我该怎么做?

为了进行完整性检查,让我们看看您的文件夹中当前有什么:

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)

输出:

[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')]

现在,我们将专门查找 json 个文件,获取索引和名称,然后重命名文件。

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

您现在应该会看到新命名的 mp4 文件。

来自 pathlib 模块的更多内容: