我厌倦了用字幕重命名每一集(我有100+)
如果两个文件的名称中有相同的集号,我如何比较它们,然后我希望它将副标题替换为集名。
我试着做我自己,我成功地改变了名字(容易),但是比较部分是我所缺少的。
谢谢。
编辑:
文件名会是这样的;
(小集):PuyaSubs!香蕉鱼- 12 1080p.mkv
(副标题):香蕉鱼-12
到目前为止,我得出的结论是:
import os
old_file = os.path.join(r"D:\Downloads\Media\Anime\[PuyaSubs!] Banana Fish [1080p][Batch]\Banana Fish - 12.ass", "Banana Fish - 12.ass")
new_file = os.path.join(r"D:\Downloads\Media\Anime\[PuyaSubs!] Banana Fish [1080p][Batch]\Banana Fish - 12.ass", "[PuyaSubs!] Banana Fish - 12 [1080p][2ECC9207].ass")
os.rename(old_file, new_file)这将只改变名称,但它不是自动的,我将不得不手动为每个字幕文件。
发布于 2019-02-17 12:45:40
我的看法是这样的:
我是如何实现这个的:
os.listdir获取目录中所有文件的列表.ass后缀(这意味着它是一个字幕文件)try语句中这样做,这允许我们突破for循环,这样就不会出现任何错误。os.rename#!/usr/bin/env python3
import os, re
def fixMySubtitles(mediaFolderPath):
# Lists all the files in the folder
files = os.listdir(mediaFolderPath)
# Loops through every single file in the directory
for file in files:
# In the beginning we set both names to "" since we don't know what they are yet.
movie = ""
subtitle = ""
# If searches the current file name to see if it ends in .ass
if re.search(r'\.ass', file):
# This removes the .ass part from the subtitle name
subtitle = re.search(r'(.*).ass', file).group(1)
# This chunk matches the subtitle file to the movie file
movie = [movie for movie in files if subtitle in movie]
movie = [movie for movie in movie if '.mkv' in movie]
movie = "".join(movie)
# Now we try to get the movie without the .mkv part
try:
movie = re.search(r'(.*).mkv', movie).group(1)
except:
print("Movie not found error for subtitle '%s'" % (file))
break
# Lastly we can set the path of the new subtitle file
newPath = os.path.join(mediaFolderPath, "%s.ass" % (movie))
# And if the movie variable has a value then we can rename
if movie:
os.rename(os.path.join(mediaFolderPath, file), newPath)
print("I have fixed your subtitles!")如何运行此
我现在实际上是在Mac上,但是这仍然适用于windows。
def来创建函数(函数/方法可以互换使用),但是实际上我们没有在任何地方运行该函数。fixMySubtitles("[DIRECTORY OF FOLDER WITH MEDIA]")并将[DIRECTORY OF FOLDER...]替换为文件夹的路径python.exe [PATH TO PYTHON FILE]运行该文件。作为参考,这是我的文件中的确切代码:
#!/usr/bin/env python3
import os, re
def fixMySubtitles(mediaFolderPath):
files = os.listdir(mediaFolderPath)
for file in files:
movie = ""
subtitle = ""
if re.search(r'\.ass', file):
subtitle = re.search(r'(.*).ass', file).group(1)
movie = [movie for movie in files if subtitle in movie]
movie = [movie for movie in movie if '.mkv' in movie]
movie = "".join(movie)
try:
movie = re.search(r'(.*).mkv', movie).group(1)
except:
print("Movie not found error for subtitle '%s'" % (file))
newPath = os.path.join(mediaFolderPath, "%s.ass" % (movie))
if movie:
os.rename(os.path.join(mediaFolderPath, file), newPath)
print("I have fixed your subtitles!")
fixMySubtitles("/Users/My_User/Desktop/test")希望这能有所帮助!
发布于 2019-02-17 12:39:36
这是我的解决方案。为变量inDir分配-I认为是D:\Downloads\Media\Anime的文件路径
import os
import re
from glob import glob
inDir = "D:\Downloads\Media\Anime"
lst = glob(os.path.join(inDir, r"*.ass"))
for filepath in lst:
parent, filename = os.path.split(filepath)
episode = re.findall(r"Banana Fish - (\d+).ass", filename)[0]
newName = "[PuyaSubs!] Banana Fish - {} [1080p][2ECC9207].ass".format(episode)
os.chdir(parent)
os.rename(filename, newName)https://stackoverflow.com/questions/54731164
复制相似问题