我已经厌倦了用字幕来重命名每一集(我有100 +)
如果两个文件名称中的情节编号相同,如何比较这两个文件,之后我希望将其替换为情节名称的副标题
我尝试自己做,成功地更改了名称(简单),但是比较部分是我所缺少的。
谢谢。
已编辑:
文件名将是这样;
(片段):[PuyaSubs!]香蕉鱼-12 [1080p] .mkv
(字幕):香蕉鱼-12.ass
到目前为止我想到的是什么
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)
这只会更改名称,但不会自动更改,我将必须手动处理每个字幕文件。
答案 0 :(得分:2)
我看待这个问题的方式是这样的:
我如何实现此目标:
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")
希望这会有所帮助!
答案 1 :(得分:0)
这是我的解决方案...为变量inDir
分配文件路径-我认为-为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)