From d9371a559c861c1dcc0d0b1b47a6f1b7d5f9c881 Mon Sep 17 00:00:00 2001 From: Chris Hocking Date: Sat, 24 Jun 2023 15:57:28 +1000 Subject: [PATCH] Added `--trimafterspace` argument --- README.md | 28 ++++++++++++++++++++++++++++ Source/rename-avid-mxf.py | 18 +++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ab2e22c..a57bc2e 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,18 @@ Here's a real-world example: - `5bis-01-01 - 23Y06M08 - 5bis-01T01.wav.mxf` - `A016C010_230608_R2QM - A016C010_230608_R2QM.new.01.mxf` +You can also use the optional `--trimafterspace` argument: + +**Before:** + +- `5bis-01T01A03.E0A869B4CDC3A.mxf` +- `V01.E0A7CD6A_55C5455C54DD8V.mxf` + +**After:** + +- `5bis-01-01.mxf` +- `A016C010_230608_R2QM.mxf` + --- ## Easy Installation @@ -52,12 +64,28 @@ To process a **folder** of MXF files: ./rename-avid-mxf "/path/to/folder" ``` +You can also use the optional **Trim After Space** argument: + +```bash +./rename-avid-mxf --trimafterspace "/path/to/folder/or/file" +``` + This script will recursively search through the specified folder and rename all MXF files based on their package name. **PRO TIP:** You can right-click on files in Finder with the OPTION key held down to copy a file or folders pathname. --- +## Version History + +### v1.0.1 - Saturday 24th June 2023 +- Added `--trimafterspace` argument. + +### v1.0.0 - Saturday 24th June 2023 +- Initial Release + +--- + ## Manual Installation If you don't want to download a pre-compiled executable, you can trigger the Python Script directly. diff --git a/Source/rename-avid-mxf.py b/Source/rename-avid-mxf.py index f2bf6e2..75d6665 100644 --- a/Source/rename-avid-mxf.py +++ b/Source/rename-avid-mxf.py @@ -8,7 +8,7 @@ if getattr(sys, 'frozen', False): os.environ['DYLD_LIBRARY_PATH'] = sys._MEIPASS -def rename_file(path): +def rename_file(path, trim_after_space): # Retrieve media information media_info = MediaInfo.parse(path) @@ -17,6 +17,11 @@ def rename_file(path): if track.track_type == "General": # Get the package name package_name = track.package_name + + # If the flag is set, trim the package name after the first space + if trim_after_space: + package_name = package_name.split(' ')[0] + # Clean the package name to avoid any illegal characters for filenames package_name = package_name.replace('/', '-') @@ -27,12 +32,12 @@ def rename_file(path): # Rename the file os.rename(path, new_path) -def process_directory(dir_path): +def process_directory(dir_path, trim_after_space): for root, dirs, files in os.walk(dir_path): for file in files: if file.endswith('.mxf'): file_path = os.path.join(root, file) - rename_file(file_path) + rename_file(file_path, trim_after_space) def main(): # Create the parser @@ -41,16 +46,19 @@ def main(): # Add an argument for the file path parser.add_argument('path', type=str, help='The path to the MXF file or directory') + # Add the new argument for the trim after space flag + parser.add_argument('--trimafterspace', action='store_true', help='Trim the filename after the first space') + # Parse the arguments args = parser.parse_args() # Check if the path is a directory or a file if os.path.isdir(args.path): # If it's a directory, process all MXF files in the directory - process_directory(args.path) + process_directory(args.path, args.trimafterspace) else: # If it's a file, process the single file - rename_file(args.path) + rename_file(args.path, args.trimafterspace) if __name__ == "__main__": main() \ No newline at end of file