Jump to content


Photo

Automatic renamer for Anime [TheTVDB]

Anime Rename Series Script Linux

  • Please log in to reply
4 replies to this topic

#1 vinanrra OFFLINE  

vinanrra

    Newbie

  • Members
  • 3 posts
  • Local time: 08:13 PM

Posted 15 February 2019 - 06:34 PM

Original source Unix.stackexchange.com

Thx to Adrian for this amazing script

 
 
Since Emby doesnt have a proper scraper this will help to "fix" that

  • Files to be renamed are all of the form [<tag>] <name> - <serial> [<quality>].mkv.

  • Each anime has a lookup file called <name>.lst, listing the episodes in serial order, e.g. One Piece.lst contains:
S01E01
S01E02
...
S01E08
S02E01
...

 
You use a bash shell at version 4 (minimum).
 
 

#!/bin/bash
# USAGE: canon_vids <dir> ...
# Canonicalize the filenames of all MKV vids in each <dir>

# All the anime lookup tables are in the lookup subdirectory
# where canon_vids is stored
lookup_dir="$(dirname "$0")/lookup"

log_skip() {
echo "SKIP ($1): $2"
}

find "$@" -name \*.mkv | while read f; do
# Check filename against our desired pattern
# (We don't want to rename what's already been renamed!)
if [[ $f =~ /(\[[^]]+\])\ (.*)\ -\ ([0-9]+)\ (\[[^]]+\].mkv) ]]; then
# We've now split our filename into:
prefix="${BASH_REMATCH[1]}"
name="${BASH_REMATCH[2]}"
serial="${BASH_REMATCH[3]##0}"
suffix="${BASH_REMATCH[4]}"
# Some sanity checks
if (( serial <= 0 )); then
log_skip "$f" "Invalid serial# '$serial' for $name"; continue
fi
# Let's look up the episode
episode="$(sed -n ${serial}p "$lookup_dir/${name}.lst")"
if [[ -z "$episode" ]]; then
log_skip "$f" "Can't find serial# '$serial' for $name"; continue
fi
mv -vn "$f" "${f%/*}/${prefix} ${name} - ${episode} ${suffix}"
fi
done

 
And here's a bonus script that generates those lookup files, given the number of episodes in each season:
 
 

#!/bin/bash
# USAGE: generate_series <#eps> ...
while [[ $1 ]]; do
((s++))
for e in $(seq "$1"); do
printf "S%02dE%02d\n" $s $e
done
shift
done



Example:

 

$ ls
canon_vids  generate_series

# Create One Piece lookup table
$ mkdir lookup
$ ./generate_series 8 22 17 13 9 22 39 13 52 31 99 56 100 35 62 49 118 33 96 > lookup/One\ Piece.lst
$ tail -n lookup/One\ Piece.lst
S19E92
S19E93
S19E94
S19E95
S19E96
$ wc -l lookup/One\ Piece.lst 
874 lookup/One Piece.lst

# Create fake One Piece MKVs (adding a couple more to trigger errors)
$ mkdir op
$ for i in $(seq 0 876); do touch "$(printf "op/[TAG] One Piece - %02d [quality].mkv" $i)"; done
$ ls op | wc -l
877

# And now, the moment of truth...
$ ./canon_vids op
renamed 'op/[TAG] One Piece - 724 [quality].mkv' -> 'op/[TAG] One Piece - S17E97 [quality].mkv'
renamed 'op/[TAG] One Piece - 86 [quality].mkv' -> 'op/[TAG] One Piece - S06E17 [quality].mkv'
...
renamed 'op/[TAG] One Piece - 819 [quality].mkv' -> 'op/[TAG] One Piece - S19E41 [quality].mkv'
SKIP (op/[TAG] One Piece - 00 [quality].mkv): Invalid serial# '0' for One Piece
renamed 'op/[TAG] One Piece - 52 [quality].mkv' -> 'op/[TAG] One Piece - S04E05 [quality].mkv'
...
renamed 'op/[TAG] One Piece - 865 [quality].mkv' -> 'op/[TAG] One Piece - S19E87 [quality].mkv'
SKIP (op/[TAG] One Piece - 875 [quality].mkv): Can't find serial# '875' for One Piece
renamed 'op/[TAG] One Piece - 295 [quality].mkv' -> 'op/[TAG] One Piece - S11E69 [quality].mkv'
...
renamed 'op/[TAG] One Piece - 430 [quality].mkv' -> 'op/[TAG] One Piece - S13E49 [quality].mkv'
SKIP (op/[TAG] One Piece - 876 [quality].mkv): Can't find serial# '876' for One Piece
renamed 'op/[TAG] One Piece - 655 [quality].mkv' -> 'op/[TAG] One Piece - S17E28 [quality].mkv'
...
renamed 'op/[TAG] One Piece - 93 [quality].mkv' -> 'op/[TAG] One Piece - S07E02 [quality].mkv'
renamed 'op/[TAG] One Piece - 278 [quality].mkv' -> 'op/[TAG] One Piece - S11E52 [quality].mkv'

# OK, but what happens when we run it again? Will our files be further renamed? Will Luffy find One Piece?
$ ./canon_vids op 
SKIP (op/[TAG] One Piece - 00 [quality].mkv): Invalid serial# '0' for One Piece
SKIP (op/[TAG] One Piece - 875 [quality].mkv): Can't find serial# '875' for One Piece
SKIP (op/[TAG] One Piece - 876 [quality].mkv): Can't find serial# '876' for One Piece

# Of course! Those files were never found in the lookup table, so they're still
# candidates for renaming. More importantly, no other files were touched.

Little explanation:

 
Lpmj_mr-RlijHGrgpspfRw.png

./generate_series 8 22 17 13 9 22 39 13 52 31 99 56 100 35 62 49 118 33 96 > lookup/One\ Piece.lst


Extra:

If u want to automate create a line at crontab.
 

Attached Files


Edited by vinanrra, 15 February 2019 - 06:53 PM.


#2 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 138305 posts
  • Local time: 03:13 PM

Posted 15 February 2019 - 06:40 PM

Very cool, thanks !



#3 vinanrra OFFLINE  

vinanrra

    Newbie

  • Members
  • 3 posts
  • Local time: 08:13 PM

Posted 15 February 2019 - 07:17 PM

Very cool, thanks !

 

Just one question when will be supported custom scrappers or a scrapper for anime?



#4 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 138305 posts
  • Local time: 03:13 PM

Posted 15 February 2019 - 10:12 PM

Have you explored the anime plugin?

#5 vinanrra OFFLINE  

vinanrra

    Newbie

  • Members
  • 3 posts
  • Local time: 08:13 PM

Posted 15 February 2019 - 10:14 PM

Have you explored the anime plugin?

 

Not working for me with Absolute series for example:

One Piece - 872.mkv

 

Give me: Season 8 Chapter 72 :P







Also tagged with one or more of these keywords: Anime, Rename, Series, Script, Linux

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users