还是老样子,先上GitHub:Yuuki_Dach’s GitHub

上次自己写了一个字幕批量重命名的脚本,结果发现在处理字幕文件的时候还是存在bug(毕竟是自己一边看番,一边测试+改进,没有特意的去找测试样本)

男子高中生的日常 - 好..好尴尬

首先是关于视频文件的查找,原来的查找方式十分简单,也没有使用正则表达式,只是粗略的查找了一下,这里利用find的功能,使得查找更加精确。

在处理字幕文件的时候,也有问题。for循环在利用find查找的时候,会把空格视为一个间断。因此,这里利用IFS,把find指令找到的文件名当作一整块来处理就行了。

另外,字幕文件的后缀名有多种,而且利用正则表达式来匹配的方式也有很多,这里就留到下次再来处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#! /bin/bash

echo "Before the opration stars, please make sure you have been in the right folder!"
while true; do
read -p "Do you want to continue? y/n " yn
case ${yn} in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done

echo "What is the videos' extension name?"
read video_extension_name

echo "How many episodes does it have?"
read total_video_episode_num

echo "These files were renamed successfully:"
for target_episode in `seq -w 1 ${total_video_episode_num}`
do
target_video=`find -regex ".*[^0-9]?${target_episode}[^0-9]?.*${video_extension_name}"`
target_video=${target_video%${video_extension_name}}
target_video=${target_video:2}
echo "${target_video}"
find -regex ".*[^0-9\.i]${target_episode}[^0-9\.P].*ass" | while IFS=" " read target_sub
do
echo "${target_sub}"
rename "s/.*[^0-9\.i]${target_episode}[^0-9\.P].*\./${target_video}/" "${target_sub}"
done
done