Useful Commands Snippets for Subtitle Files

Bulk Renaming

You may have a list of subtitle files having different naming schemes than those video files to be matched. We can use Regex to rename these subtitle files.

Unix Shell

With Perl File::Rename utility:

rename 's/^.*?\[(\d+)\].*(\.\w+)$/$1$2/' *.ass  

Powershell

Get-ChildItem .\* -Include *.ass | Rename-Item -NewName { $_.Name -replace '^.*?(\d+).*(\.\w+)$','$1$2' }  

Encoding Conversion

Unix Shell

With enconv:

#!/bin/bash

find "$0" -type f -name "*.ass" -o -name "*.srt" -o -name "*.ssa" -o -name "*.sub" -o -name "*.sbv" -exec enconv -L chinese -x UTF8 "{}" \;  

With iconv:

#!/bin/bash

if [ -z "$1" ]; then  
    SRC="."
else  
    SRC="$1"
fi

if [ -z "$2" ]; then  
    ENCODING="gb18030"
else  
    ENCODING="$2"
fi

if [ -z "$3" ]; then  
    DESTI="UTF-8"
else  
    DESTI="$3"
fi

mkdir -p "${SRC}/out"

find "${SRC}/"* -maxdepth 0 -type f -name "*.ass" -exec sh -c "iconv -f \"${ENCODING}\" -t \"${DESTI}\" \"{}\" > \"${SRC}/out/{}\" " \;  

Powershell

ForEach ($f in Get-ChildItem .\* -Include *.ass) {[System.IO.File]::WriteAllText($f, [System.IO.File]::ReadAllText($f, [System.Text.Encoding]::GetEncoding('gb2312')), [System.Text.Encoding]::GetEncoding('utf-8'))}  

List of encoding names:

[System.Text.Encoding]::GetEncodings()

Rix

Read more posts by this author.