0

Firstly, this question was made as a follow up of my previous question: How to batch rename files with "variables"?. I'm just restating it considering Sergey's recommendations.

I use xmbc to view my tv shows. Before I began using xmbc, I batch downloaded the first few seasons of "Bleach". What can I say, I'm an anime fan ;-). These are named: "bleachxx.mp4" where xx is the episode number in respect to the whole series, not to the season. so "bleach21.mp4" is the 1st episode of the second season and 21st overall. The files themselves are, however, divided into their own season folders.

The problem is that the command will rename all the files into "Bleach S0XExx" but because the files have the overall file numbers, bleach52.mp4 --> 'Bleach S03E52.mp4' when there obviously aren't 52 episodes in season 3.

To solve this problem, I would have to subtract the number of episodes in the previous seasons by the overall number and essentially get the specific season number.

Example: If there are 20 episodes in season 1, then 25-20=5 so the 25th episode is the 5th of season 2 and renaming would carry out as normal.

I'm a complete newb in coding, I just suggested a python script as I was told this was an effective way of solving this problem. Can somebody please help me in the step by step process of writing this script? Thanks!

deox
  • 624
  • There already is a rename script. Why reinvent the wheel? – Matt Oct 27 '11 at 02:00
  • The scripts if have tried (pyrename, the "rename" command and a plethora of "tv show organizing" scripts) can't do this, i.e. Changing the number value in a filename by a certain function/math operation. Do you know any script/program that can do this? – deox Oct 27 '11 at 02:25
  • Actually, it is a perl script..[Well, the one I'm talking about (rename) is] and now that I check the symlinks, on my system, it points to prename. And what do you mean? give me an example of a filename, and tell me what it needs to be renamed to. – Matt Oct 27 '11 at 02:37
  • So "bleach21.mp4" means S2 E01? "bleach53.mp4" means S5 E03? If the first number is the season ["5" in 53] and the second is the episode number, I think it is doable to rename them all. – Matt Oct 27 '11 at 02:44
  • 3
    @Matt: you don't want a person who describes themselves as "complete newb in coding" to mess with Perl, do you? :) – Sergey Oct 27 '11 at 02:45
  • @user600: there are a few answers to your previous question. If you're not satisfied I suggest you to ask the question at stackoverflow - you'll get better answers there. – Sergey Oct 27 '11 at 02:47
  • @Sergey.. you could be using the rename script, and not know it was perl. Because I for one was one of those people. I knew the "s/find/replace/g" thing from sed. So it was easy to me.. It's pretty easy to use once you get the syntax – Matt Oct 27 '11 at 02:47
  • Question Solved! not sure how to mark it as such. Refer to the original question for the scripts. – deox Oct 27 '11 at 03:22
  • @Sergey I posted this question before you answered. Usually people will ask you to move things to a new thread if they differ from the original topic (going from "rename command" to writing a python script), and I just decided to do that. Thank you again to all of you. – deox Oct 27 '11 at 03:27
  • @user600: Please post a short summary here as an answer (a link is fine) and accept that answer. That way, this question won't continue to show up as unanswered. – Scott Severance Oct 27 '11 at 05:11

1 Answers1

1

This is off the top of the head, but try:

# call program as: epirename.py /path/to/mp4files 20 41 59
# the numbers are the sequence of first episodes in each season after the first
import os
import re
import sys
mp4dir = sys.argv[1]
seasons = [int(startnum) for startnum in sys.argv[2:]]
seasons.insert(0, 0) # start with '0' as the first episode of season 1
season = 0
patt = re.compile(r'([[:alpha:]]+)([[:digit:]]+).mp4')
for filename in sorted(os.listdir(mp4dir)):
    match = patt.search(filename)
    if match:
        episode = int(match.group(2))
        try:
            if episode >= seasons[season+1]:
                season += 1
        except:
            pass
        # create new filename as '{basename}S{season}E{episode}'
        newname = '%sS%02dE%03d.mp4' % (match.group(1), season+1, episode - seasons[season])
        os.rename(os.path.join(dir, filename),
                  os.path.join(dir, newname))
        print 'renamed', filename 'to', newname

The result is that the files that match '{name}{epi}.mp4' will be renamed to '{name}S{season}E{episode}.mp4'. Other files are left untouched.

Again, this is off the top of my head; there may be more efficient techniques.

Arcege
  • 5,418