Skip to content

Commit

Permalink
cosmetic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Rieck committed Dec 12, 2015
1 parent b88f6ee commit 1db6423
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@ A Movie Renamer for Plex Metadata

## Overview

This simple Python script for renaming movie files in a Plex database.
[Plex](http://plex.tv/) is a software suite for organizing videos,
music and photos. It can identify movies based on their file names
and retrieve corresponding metadata. In practice, however, not all
file names are appropriate and thus some movies usually need to be
assigned to the corresponding movie titles manually. Unfortunately,
This is a simple script for renaming movie files based on Plex
metadata. [Plex](http://plex.tv/) is a software suite for organizing
videos, music and photos. It can identify movies based on their file
names and retrieve corresponding metadata. In practice, however, not
all files are named appropriate and thus some movies usually need to
be manually assigned to the corresponding movie title. Unfortunately,
Plex is not able to rename these files and thus if the database is
initialized from the scratch these files need to be manually fixed
again.
deleted or re-installed, these files need to be manually fixed again.

The Python script `perplex` solves this problem. Given an
installation of Plex and the corresponding database, it lists all
imported movies and renames them according to the retrieved movie
metadata. To avoid breaking the current installation, the renamed
files are copied to another directory.
The script `perplex.py` solves this problem. Given an installation of
Plex and the corresponding database of metadata, the script extracts
all movies and renames the corresponding files according to the
retrieved metadata. To avoid breaking the current installation, the
renamed files are copied to another directory.

That's it.
## Usage

First, extract all the metadata from the Plex database and save it.

$ ./perplex.py --save movies.db --plex /var/plex
Analyzing Plex database: Found 335 movies and 365 files
Saving metadata to movies.db

Now start the renaming process. The renamed movies are written to
the given directory.

$ ./perplex.py --load movies.db --dest ./output
Loading metadata from movies.db
Copying renamed files to ./output
100% |#################################################| Time: 0:00:00

Done.
8 changes: 8 additions & 0 deletions perplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
def build_db(plex_dir, movies={}):
""" Build movie database from sqlite database """

print "Analyzing Plex database: ",
dbfile = os.path.join(plex_dir, *dbpath.split("/"))
db = sqlite3.connect(dbfile)

Expand All @@ -35,11 +36,15 @@ def build_db(plex_dir, movies={}):
SELECT mp.file FROM media_items AS mi, media_parts AS mp
WHERE mi.metadata_item_id = %s AND mi.id = mp.media_item_id """

files = 0
for id in movies:
for file in db.execute(query % id):
movies[id][2].append(file[0])
files += 1

db.close()
print "Found %d movies and %d files" % (len(movies), files)

return movies


Expand Down Expand Up @@ -99,13 +104,16 @@ def copy_rename(mapping, dest):
movies = build_db(args.plex)
mapping = build_map(movies)
elif args.load:
print "Loading metadata from " + args.load
movies, mapping = json.load(gzip.open(args.load))
else:
print "Error: Provide a Plex database or stored database."
sys.exit(-1)

if args.save:
print "Saving metadata to " + args.save
json.dump((movies, mapping), gzip.open(args.save, 'w'))

if args.dest:
print "Copying renamed files to " + args.dest
copy_rename(mapping, args.dest)

0 comments on commit 1db6423

Please sign in to comment.