Create a py gui that renames all videos in a folder to have the folders name added as a…
- User
Create a py gui that renames all videos in a folder to have the folders name added as a prefix
- Assistant
Create a py gui that renames all videos in a folder to have the folders name added as a prefix
Certainly! Below is a step-by-step guide to creating a Python GUI application that renames all video files in a selected folder by adding the folder's name as a prefix to each video file. We'll use the Tkinter library for the GUI and Python's built-in os module for file operations.
Python Installed: Ensure you have Python 3.x installed on your computer. You can download it from Python's official website.
Required Libraries: The script uses standard Python libraries (tkinter, os, tkinter.filedialog, and tkinter.messagebox), so no additional installations are necessary.
import os
import tkinter as tk
from tkinter import filedialog, messagebox
To ensure that only video files are renamed, define a list of common video file extensions.
VIDEO_EXTENSIONS = (
'.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv', '.mpeg', '.mpg', '.3gp', '.webm'
)
Here's the complete script with detailed comments:
import os
import tkinter as tk
from tkinter import filedialog, messagebox
# Supported video file extensions
VIDEO_EXTENSIONS = (
'.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv', '.mpeg', '.mpg', '.3gp', '.webm'
)
class VideoRenamerApp:
def __init__(self, master):
self.master = master
master.title("Video Renamer")
master.geometry("400x200")
master.resizable(False, False)
# Instruction Label
self.label = tk.Label(master, text="Select a folder to rename its video files", wraplength=380)
self.label.pack(pady=20)
# Browse Button
self.browse_button = tk.Button(master, text="Browse Folder", command=self.browse_folder)
self.browse_button.pack(pady=10)
# Rename Button
self.rename_button = tk.Button(master, text="Rename Videos", command=self.rename_videos, state=tk.DISABLED)
self.rename_button.pack(pady=10)
# Status Label
self.status_label = tk.Label(master, text="", fg="green")
self.status_label.pack(pady=10)
# Initialize folder path
self.folder_path = ""
def browse_folder(self):
folder_selected = filedialog.askdirectory()
if folder_selected:
self.folder_path = folder_selected
self.status_label.config(text=f"Selected Folder: {self.folder_path}")
self.rename_button.config(state=tk.NORMAL)
else:
self.status_label.config(text="No folder selected.")
self.rename_button.config(state=tk.DISABLED)
def rename_videos(self):
if not self.folder_path:
messagebox.showwarning("No Folder Selected", "Please select a folder first.")
return
folder_name = os.path.basename(self.folder_path)
renamed_files = []
skipped_files = []
try:
for filename in os.listdir(self.folder_path):
if filename.lower().endswith(VIDEO_EXTENSIONS):
new_filename = f"{folder_name}_{filename}"
src = os.path.join(self.folder_path, filename)
dst = os.path.join(self.folder_path, new_filename)
# Check if the new filename already exists to avoid overwriting
if os.path.exists(dst):
skipped_files.append(filename)
continue
os.rename(src, dst)
renamed_files.append(filename)
# Display results
result_message = f"Renamed {len(renamed_files)} files successfully."
if skipped_files:
result_message += f"\nSkipped {len(skipped_files)} files (already renamed or naming conflict)."
messagebox.showinfo("Renaming Complete", result_message)
self.status_label.config(text=result_message)
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
self.status_label.config(text="An error occurred during renaming.")
def main():
root = tk.Tk()
app = VideoRenamerApp(root)
root.mainloop()
if __name__ == "__main__":
main()
Class VideoRenamerApp: This class encapsulates the GUI application.
__init__ Method: Sets up the GUI components:
browse_folder Method:
folder_path attribute and enables the rename button if a folder is selected.rename_videos Method:
os.rename.main Function: Initializes and runs the Tkinter main loop.
Save the Script: Copy the above script into a file named, for example, video_renamer.py.
Run the Script:
Open a terminal or command prompt.
Navigate to the directory containing video_renamer.py.
Execute the script using Python:
python video_renamer.py
If you have multiple versions of Python installed, you might need to use python3 instead of python.
Using the Application:
Backup Your Files: Before running bulk rename operations, it's a good idea to back up your files to prevent accidental data loss.
Handling Naming Conflicts: The script currently skips renaming files if the new filename already exists. You can modify this behavior as needed (e.g., appending a number to make the filename unique).
Extending Video Formats: If you have video files with extensions not listed in VIDEO_EXTENSIONS, you can add them to the tuple.
Cross-Platform Compatibility: The script is compatible with Windows, macOS, and Linux, as it uses standard Python libraries.
Progress Bar: For folders with many files, adding a progress bar can improve user experience.
Selective Renaming: Allow users to select which file types to rename or provide options to customize the prefix.
Undo Feature: Implement functionality to revert the renaming in case of mistakes.
Logging: Save the results of the renaming operation to a log file for future reference.
Feel free to modify and enhance the script based on your specific needs!
Not yet included in a published release.