Duplicate Movie Removal Automation?

I like the way you think, I’ll definitely look into that.

I’ve written a little python script that use the python plexapi to find duplicates and export them to a CSV. This is my first python script, so there may be a better way to do it, but this has worked for me (tested on Windows).

Needs Python3

Install plexapi by running pip install plexapi

from getpass import getpass
from pprint import pprint
from plexapi.myplex import MyPlexAccount # https://python-plexapi.readthedocs.io/en/latest/introduction.html
import csv

# Fill in your info here.  Leaving user_name or server_name as None will prompt for input
user_name = None # example: "MyUserName"
server_name = None # example: "MyPlexServer"
output_file = "plex_duplicates.csv"

# Connect to Plex (you can change this to use a token if you're fully automating)
user_name = input('Username: ') if user_name is None else user_name
account = MyPlexAccount(user_name, getpass())

# Let user choose the server
if server_name is None:
    server_names = [s.name for s in account.resources() if s.product == "Plex Media Server"]
    print(f'Plex servers:')
    for s in server_names:
        print(f'  {s}')

    server_name = input('Choose plex server: ')

# Get user input for resources
print(f'Connecting to server: {server_name}')
plex = account.resource(server_name).connect()


class Duplicate:
    def __init__(self, video=None, media=None, part=None):
        # There's probably a better way to handle Nones
        self.video = video
        self.media = media
        self.part = part
        self.librarySectionID = video.librarySectionID if video is not None else None
        self.title = video.title if video is not None else None
        self.file = part.file if part is not None else None
        self.size = part.size if part is not None else None
        self.aspect_ratio = media.aspectRatio if media is not None else None
        self.bitrate = media.bitrate if media is not None else None
        self.container = media.container if media is not None else None
        self.duration = media.duration if media is not None else None
        self.height = media.height if media is not None else None
        self.width = media.width if media is not None else None
        self.media_id = media.id if media is not None else None
        self.video_codec = media.videoCodec if media is not None else None
        self.video_resolution = media.videoResolution if media is not None else None
        self.video_profile = media.videoProfile if media is not None else None


with open(output_file, 'w', encoding="utf-8", newline='') as csv_file:
    writer = csv.DictWriter(csv_file, vars(Duplicate()).keys())
    writer.writeheader()

    # Iterate through sections that are "show" or "movie" type
    sections = [s for s in plex.library.sections() if s.type in [
                                                 'show', 'movie']]
    for s in sections:
        print(f'Looking for duplicates in section: {s.title}')
        # Iterate through videos
        for v in s.search(duplicate=True, libtype=('episode' if s.type == 'show' else 'movie') ):
            # Iterate through media
            for m in v.media:
                # Iterate through media parts
                for p in m.parts:
                    # Write info to CSV (suppress console output)
                    _ = writer.writerow(vars(Duplicate(v, m, p)))

print(f'Output file: {output_file}')

This combined with a little filtering in excel or bash can make quick work out of deleting duplicates

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.