Rename movie to parent folder name (Now with a windows version too!)

Come up with a useful post-processing script? Share it here!
oluminion
Newbie
Newbie
Posts: 2
Joined: January 7th, 2010, 8:05 pm

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by oluminion »

hi there,

even if the topic is quite old, i am really interested in the first unix script. i tried to get it to work on osx which should work so far, but i can't find any mencoder version for terminal use. any one got a suggestion or maybe a osx compatible script which does the same as the above one?

thx in advance
rudyb
Newbie
Newbie
Posts: 28
Joined: December 8th, 2009, 11:46 pm

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by rudyb »

Here's another script for renaming movie files to match the folder (well, job) name. This time, in Python.

I made this one partially because I wanted to learn some python, partially because I wanted to integrate this script into the one that updates xbmc and sends a prowl notification, and partially because I wanted something more readable/configurable than a batch file.

I've never actually used python before, so it's probably not the prettiest script you'll see, code-wise, and there may be much simpler ways of doing things, but in the handful of tests I ran, it works great.

It works perfectly (thus far) on Python 2.6.4

I *think* it should work on any platform you can run python on, but I have no way of testing this. The only thing I can think of that might cause problems is the direction of the slashes in the last line. They're windows-style, since that's what I use, but I don't know how much other OS'es care. Everything else should be OS-independent.

As I haven't done extensive testing, use at your own risk. I can't imagine it would cause any damage to anything, but I'm always nervous about stuff like that. :)

The whole thing is heavily commented (pretty much every line, lol). This is mostly for my sake, so I know what the heck is going on if I ever have to look at the code again, but if you look through it and find something I could have done better, please let me know, and feel free to post corrections/changes (an explanation of why would be great for those of us that aren't great with python yet).

Either way, feedback is definitely appreciated.

Get it here:
http://pastebin.com/f1346653b

Or just copy/paste:

Code: Select all

# Python 2.6.4 | http://www.python.org/download/releases/2.6.4/
import socket,sys,urllib2,os
from urllib2 import Request, URLError, urlopen


# Rename the file to match the folder (job) name
# Get the Newzbin category
cat = sys.argv[5]
# Get the folder name again
ugly_folder = sys.argv[1]
# Get the job name
ugly_jobname = sys.argv[3]
# Set movie (and related) file extensions. These are filetypes we care about. Add more if you want those renamed, too
movie_extensions = ['avi', 'mkv', 'wmv', 'avi', 'ts', 'img', 'iso', 'sub', 'idx', 'srt']


def ext_count(file_list):
    # Create the extensions dictionary
    extensions = {}
    # Loop through the file list to make a list of extensions and how many there are of each
    for filename in file_list:
        # Split filename by period
        ext = filename.split('.')
        # Get the file extension
        ext = ext[-1]
        # Check if the extension exists in the array list yet
        if extensions.has_key(ext):
            extensions[ext] = extensions[ext] + 1
            # If so, add one to the existing entry
        else:
            # Otherwise, create the list entry
            extensions[ext] = 1
    return extensions


# Apply this to movies only
if cat == "movies":
    # Make an empty dictionary for counting how many we've renamed of each extension
    ext_tally = {}
    # Make a list (downloaded_files) containing all of the downloaded files
    downloaded_files = sorted(os.listdir(ugly_folder))
    # Create a dictionary of extensions (the key) and the number of each (the value)
    extensions = ext_count(downloaded_files)
    # Loop through the list of downloaded files
    for filename in downloaded_files:
        # We don't know if this file is relevant to our interests
        is_video = 0
        # See if the ext matches one we care about. Loop through movie_extensions
        for mov_ext in movie_extensions:
            # See if the filename ends with a relevant extension
            if filename.endswith('.' + mov_ext):
                # Flag the file as relevant
                is_video = 1
                # Stop checking (theoretically, it shouldn't have more than one extension)
                break
        # If we determined that the file was relevant...
        if is_video == 1:
            # Start building the new filename
            new_filename = ugly_jobname
            # Get the extension of the file
            file_extension = filename.split('.')[-1]
            # Check to see if there was more than one of that extension
            if extensions[file_extension] > 1:
                # If so, add " - CD" to the end
                new_filename = new_filename + ' - CD'
                # Check to see if we've already renamed one file with this extension
                if ext_tally.has_key(file_extension):
                    # If so, add one to the count
                    ext_tally[file_extension] = ext_tally[file_extension] + 1
                else:
                    # If not, create a counter and set it to 1
                    ext_tally[file_extension] = 1
                # Then append that number to the end of the filename
                new_filename = new_filename + str(ext_tally[file_extension])
            # Finally, add the extension
            new_filename = new_filename + '.' + file_extension
            # See if the new filename and the old filename match
            if filename == new_filename:
                # If so, end this iteration without renaming, and say so:
                print "Filenames are the same. Not renaming"
                continue
            # Uncomment this line to print the original filename and new filename
            print 'Renaming ' + filename + ' to ' + new_filename
            # Last, but not least, rename the file
            os.rename(ugly_folder + '\\' + filename, ugly_folder + '\\' + new_filename)

/edit: Added a couple of lines to keep it from trying to rename a file if the current filename and new filename are the same. Haven't tested this yet, but the code is pretty simple.
Last edited by rudyb on January 9th, 2010, 5:53 am, edited 1 time in total.
oluminion
Newbie
Newbie
Posts: 2
Joined: January 7th, 2010, 8:05 pm

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by oluminion »

msgid_5312411 American Beauty (1999).log

    /Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 2: import: command not found
from: can't read /var/mail/urllib2
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 4:
: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 5:
: command not found
cat: =: No such file or directory
cat: sys.argv[5]\r: No such file or directory
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 10: ugly_folder: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 12: ugly_jobname: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 14: movie_extensions: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 15:
: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 16:
: command not found
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 17: syntax error near unexpected token `('
/Volumes/Storage 1/SABnzbd-0.2.5-w32/scripts/rename.py: line 17: `def ext_count(file_list):
'

that's the output i get when using your script under macos :/ maybe it helps you.
JohnSCS
Newbie
Newbie
Posts: 42
Joined: August 17th, 2009, 12:23 am

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by JohnSCS »

oluminion,

Make sure you are running Python 2.6.4.

Python versions lower than 2.6 and 3.0 and above will not work.
JohnSCS
Newbie
Newbie
Posts: 42
Joined: August 17th, 2009, 12:23 am

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by JohnSCS »

rudyb,

Love it - you just saved me writing my own version. Only tested with 2 movies so far, but has handled both as expected - one movie had 2 avi's and it renamed then to CD1 & CD2.

Great work.
mra6368
Newbie
Newbie
Posts: 32
Joined: September 30th, 2009, 6:02 am

Re: Rename movie to parent folder name (Now with a windows version too!)

Post by mra6368 »

Can i use the python version in Mac OSX 10.6 Snow Leopard? If so what do i need to do? Thanks
Post Reply