自动重命名视频文件

时间:2013-01-12 12:54:37

标签: macos bash command-line file-rename

我有很多想要重命名的文件,手动操作需要很长时间。它们是视频文件,通常采用这种格式 - “显示名称 - 情节号码 - 情节名称”,例如“Breaking Bad - 101 - Pilot”。

我想做的是将“101”部分更改为我自己的“S01E01”惯例。我认为在一个节目的一个系列中,该字符串的唯一连续部分是最后一个数字,即。 S01E01,S01E02,S01E03,S01E04等...

有没有人能够在Mac OS X上给我提供有关如何在终端上执行此操作的建议。我认为使用Automator或其他批量重命名程序太复杂了......

由于

4 个答案:

答案 0 :(得分:1)

以下脚本将找到所有.mp4,其中包含一个包含3个连续数字的字符串。示例something.111.mp4。它会将其转换为something.S01E11.mp4。它还将排除任何样本文件。

find . ! -name "*sample*" -name '*.[0-9][0-9][0-9].*.mp4' -type f | while read filename; do mv -v "${filename}" "`echo $filename | sed -e 's/[0-9]/S0&E/;s/SS00E/S0/g'`";done;

与之前的剧本一样,只有在不到10个赛季才会有效。

对于那些试图为其当前目录树进行个性化的人,我建议学习sed和find命令。它们非常强大,简单,并且允许您替换文件名中的任何字符串。

答案 1 :(得分:1)

以下解决方案:

  • 适用于3位和4位季节+剧集说明符(例如第1季第7集107或第10季第2集1002
  • 演示了高级findbash技术,例如:
    • -regex主要用于通过正则表达式匹配文件名(而不是通配符模式,与-name一样)
    • execdir在与每个匹配文件相同的目录中执行命令(其中{}仅包含匹配文件 name
    • 调用ad-hoc bash脚本,该脚本演示与=~的正则表达式匹配以及通过内置${BASH_REMATCH[@]}变量报告的捕获组;命令替换($(...))左键填充零值;变量扩展以提取子串(${var:n[:m]})。
# The regular expression for matching filenames (without paths) of interest:
# Note that the regex is partitioned into 3 capture groups 
# (parenthesized subexpressions) that span the entire filename: 
#  - everything BEFORE the season+episode specifier
#  - the season+episode specifier,
#  - everything AFTER.
# The ^ and $ anchors are NOT included, because they're supplied below.
fnameRegex='(.+ - )([0-9]{3,4})( - .+)'

# Find all files of interest in the current directory's subtree (`.`)
# and rename them. Replace `.` with the directory of interest.
# As is, the command will simply ECHO the `mv` (rename) commands.
# To perform the actual renaming, remove the `echo`.
find -E . \
 -type f -regex ".+/${fnameRegex}\$" \
 -execdir bash -c \
   '[[ "{}" =~ ^'"$fnameRegex"'$ ]]; se=$(printf "%04s" "${BASH_REMATCH[2]}");
   echo mv -v "{}" "${BASH_REMATCH[1]}S${se:0:2}E${se:2}${BASH_REMATCH[3]}"' \;

答案 2 :(得分:0)

for FOO in *; do mv "$FOO" "`echo $FOO | sed 's/\([^-]*\) - \([0-9]\)\([0-9][0-9]\)\(.*\)/\1 - S0\2E\3\4/g'`" ; done

如果少于10个赛季,这是有效的。

答案 3 :(得分:0)

使用perl rename实现,可以轻松处理正确的填充,适用于任意数量的海洋和剧集(&lt; 100,但可以很容易地适应您当前的格式):< / p>

$ ls -1 *.avi
My Show - 0301 - Qux.avi
My Show - 101 - Foo.avi
My Show - 102 - Bar.avi
My Show - 1102 - Blah.avi
My Show - 201 - Quux.avi

$ rename -n 's/- (\d+)(\d{2,}) -/sprintf("- S%02dE%02d -", $1, $2)/e' *.avi
My Show - 0301 - Qux.avi renamed as My Show - S03E01 - Qux.avi
My Show - 101 - Foo.avi renamed as My Show - S01E01 - Foo.avi
My Show - 102 - Bar.avi renamed as My Show - S01E02 - Bar.avi
My Show - 1102 - Blah.avi renamed as My Show - S11E02 - Blah.avi
My Show - 201 - Quux.avi renamed as My Show - S02E01 - Quux.avi

我认为homebrew附带了正确的版本,所以只需要安装通过

$ brew install rename
相关问题