From 98f9a57ea1bf9e90a71c3ac9438647b9f911e596 Mon Sep 17 00:00:00 2001 From: Fabrice Quenneville Date: Sat, 8 Feb 2025 13:01:15 -0500 Subject: [PATCH] feat: add video_autoreduce_rename.py for automated renaming - Adds `video_autoreduce_rename.py`, a companion script to rename video files and directories containing resolution tags. - Automatically replaces higher resolution tags (e.g., 2160p, 1080p) with a specified max height (default: 720p). - Supports debug mode for detailed output. - Prevents overwriting existing files by skipping conflicts. - Complements `video_autoreduce.py` for a streamlined video processing workflow. --- README.md | 3 +- scripts/video_autoreduce_rename.py | 129 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100755 scripts/video_autoreduce_rename.py diff --git a/README.md b/README.md index 3ecd868..e43af6f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ This repository is structured into several key directories: - **scripts/**: Contains individual scripts for various tasks. Currently, it includes: - `video_remove_audio.py`: A script for removing audio from video files. - - `video_autoreduce.py`: A script for automatic video resolution reduction of video files. + - `video_autoreduce.py`: A script for automatic resolution reduction of video files. + - `video_autoreduce_rename.py`: A script for automated renaming of video files post resolution reduction. - **notes/**: A collection of markdown files containing notes on various topics, including: diff --git a/scripts/video_autoreduce_rename.py b/scripts/video_autoreduce_rename.py new file mode 100755 index 0000000..627e5b7 --- /dev/null +++ b/scripts/video_autoreduce_rename.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +# video_autoreduce_rename.py + +import argparse +import colorama +import os +import re + +colorama.init() + +creset = colorama.Fore.RESET +ccyan = colorama.Fore.CYAN +cyellow = colorama.Fore.YELLOW +cgreen = colorama.Fore.GREEN +cred = colorama.Fore.RED + + +def autorename(input_path, max_height=720, debug=False): + ''' + Rename video files and folders in the specified directory tree that contain a resolution + in their name to the specified max_height resolution. + + Args: + input_path (str): The path of the directory to search for video files and folders. + max_height (int, optional): The maximum height (in pixels) of the video to consider for conversion. Default is 720. + debug (bool, optional): If True, print debug messages. Default is False. + ''' + # Define patterns to search for various resolutions + resolution_patterns = { + 2160: r'(4096x2160|3840x2160|2880p|2160p|2160|1440p|4k)', + 1080: r'(1920x1080|1080p|1080)', + 720: r'(1280x720|720p|720)', + } + + files_to_rename = [] + dirs_to_rename = [] + + for dirpath, dirnames, filenames in os.walk(input_path, topdown=True): + # Collect files to rename + for filename in filenames: + for resolution, pattern in resolution_patterns.items(): + # Only process resolutions greater than max_height + if resolution > max_height and re.search( + pattern, filename, re.IGNORECASE): + old_file = os.path.join(dirpath, filename) + new_filename = re.sub(pattern, + f'{max_height}p', + filename, + flags=re.IGNORECASE) + new_file = os.path.join(dirpath, new_filename) + if old_file != new_file: + files_to_rename.append((old_file, new_file)) + break + + # Collect directories to rename + for dirname in dirnames: + for resolution, pattern in resolution_patterns.items(): + # Only process resolutions greater than max_height + if resolution > max_height and re.search( + pattern, dirname, re.IGNORECASE): + old_dir = os.path.join(dirpath, dirname) + new_dirname = re.sub(pattern, + f'{max_height}p', + dirname, + flags=re.IGNORECASE) + new_dir = os.path.join(dirpath, new_dirname) + if old_dir != new_dir: + dirs_to_rename.append((old_dir, new_dir)) + break + + # Rename files first + for old_file, new_file in files_to_rename: + if os.path.exists(new_file): + print(f"Error: Target filename {new_file} already exists.") + continue + os.rename(old_file, new_file) + if debug: + print(f'Renamed file: {old_file} -> {new_file}') + + # Rename directories after + for old_dir, new_dir in sorted(dirs_to_rename, key=lambda x: -len(x[0])): + if os.path.exists(new_dir): + print(f"Error: Target directory name {new_dir} already exists.") + continue + os.rename(old_dir, new_dir) + if debug: + print(f'Renamed directory: {old_dir} -> {new_dir}') + + +def main(): + ''' + Main function to parse command line arguments and initiate file renamings. + ''' + + # Create argument parser + parser = argparse.ArgumentParser( + description= + 'Rename video files containing resolutions in their filenames to a specified max_height resolution.' + ) + + # Define command line arguments + parser.add_argument( + 'input_path', + nargs='?', + default=os.getcwd(), + help= + 'directory path to search for video files (default: current directory)' + ) + parser.add_argument( + '-mh', + '--max-height', + type=int, + default=720, + help='maximum height of videos to be converted (default: 720)') + parser.add_argument( + '--debug', + action='store_true', + help='enable debug mode for printing additional messages') + + # Parse command line arguments + args = parser.parse_args() + + # Rename videos + autorename(args.input_path, args.max_height, args.debug) + + +if __name__ == "__main__": + # Execute main function when the script is run directly + main()