Last active
October 31, 2023 07:10
-
-
Save midwire/696748ac75912b5769ae94c3d38fe9b0 to your computer and use it in GitHub Desktop.
[Ruby script to find duplicate movies] Edit as necessary for your own needs. #ruby #utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Find duplicate movies with possible different extensions | |
require 'fileutils' | |
require 'colored' | |
include FileUtils | |
module FindDuplicateMovies | |
DIRECTORIES = [ | |
'/Volumes/Media1/Movies' | |
] | |
MOVIE_FILETYPES = [ | |
'.mp4', | |
'.mov', | |
'.m4v', | |
'.avi', | |
'.mkv', | |
'.mpg' | |
] | |
@file_count = 0 | |
@file_array = [] | |
def duplicate_count(array) | |
array.each_with_object(Hash.new(0)) do |value, hash| | |
hash[value] += 1 | |
end.each_with_object([ ]) do |(value,count), result| | |
if (count > 1) | |
result << value | |
end | |
end | |
end | |
module_function :duplicate_count | |
DIRECTORIES.each do |directory| | |
Dir.glob(File.join(directory, '**', '*')).each do |current_path| | |
@file_count += 1 | |
ext = File.extname(current_path) | |
basepath = current_path.gsub(/#{ext}$/, '') | |
next if File.directory?(current_path) | |
next unless MOVIE_FILETYPES.include?(ext) | |
@file_array << basepath | |
end | |
dupes = duplicate_count(@file_array) | |
if dupes.count > 0 | |
puts('You have duplicates:'.yellow) | |
else | |
puts('You have no duplicates.'.green) | |
end | |
dupes.each do |basepath| | |
puts(basepath.yellow) | |
end | |
end | |
puts ">>> Analyzed #{@file_count} files".red | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Docs
Here are some limited docs for those who need them.
Installation
DIRECTORIES
array to reflect where your movie libraries are located. If you are on Windows it will be something likeC:/Path/To/Movies
, etc.Usage
The script will simply collect all of the movie filenames (without extensions) into an array and count the occurrences. If there are more than 1 occurence, it qualifies as a duplicate.
Feedback
This script works perfectly for my needs, but if I can modify it to work better for you please let me know in the comments below.