I'm trying to figure out how to write this as a "standard" bash script (running on a QNAP), but not sure where to start - I can do it in NodeJS (since I use it all the time for my job), but would really rather have something.
I'm looking for pointers to how to do this rather than an actual script (though don't let that stop you) ;-)
Using the built-in Plex converted files (for compatibility etc) - input to that is a folder tree with various movie files (extensions are most commonly .avi, .mp4 or .mkv), which it then makes into an .mp4 file in a slightly different path.
I want to copy (not move - I'll let Plex clean up its own mess) the Plex output files back over the input files.
Example structure - from...
Plex Versions/Optimized for Mobile/My Tv Show/S01E01.mp4
My TV Show (1990-1995)/Season 1/s01e01 My Episode Name.mkv
The root path for both is the same (for my setup, others might have the
Plex Versionsfolder within the
My TV Show (1990-1995)folder - this script will go on GitHub when done etc).
The TV show name may change. I put the start and end date (or a trailing
-if ongoing) in brackets. Plex will use the "official" name, which might have a start year if there is more than one show with the same name.
Bearing the above in mind, I may have multiple shows with the same base name, but different years.
The target basename will be in the
Season xfolder (season number without leading
0) and have the same prefix.
If it's season 0, then it might instead be in the
Specials/folder (I'll get things consistent at some point).
If the original filename had a .mp4 filename, then the copied target file must have a .m4v filename.
After copying the new file over, the original file can be removed (leaving the copy source and target files).
If I was doing this in NodeJS I'd do something like the following:
- Get a sorted list of TV Show folders (earlier shows don't generally have years while later ones do).
- For each folder regex replace end years (
""to include the start year only, and case insensitive match against the Plex folders. If no match then try with no year.
- If no match found go back to 2 and try again.
- Get a list of Plex version files that match the pattern
/S\d+E\d+\.mp4/(there can be subtitles included from when I just ripped everything and didn't filter the streams).
- For each file get the season number, then search the
Season xfolder for the same prefix. If it's season 0 then also search the
- Replace the extension on the matched name with .mp4, if no replacement done then replace it with .m4v.
- Copy the Plex file to the new path+name.
- Remove the matched file, then go back to 5.
Now variables for names I can do, same for the obvious unlink - what I'd like help with is -
Best way to make the initial folder match (is 1-3 the best way, or is there a better shortcut? Looking at this one: Copy files to a destination folder only if the files already exist. but the source files have a different file extension )
Checking if a file exists and try different extension combinations if not.
edit: There's over 30tb of files, and Plex does not replace original files (there's been an open request for it since 2015).