Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from Arlind-dev/dev
Browse files Browse the repository at this point in the history
 Version 1.0.3
  • Loading branch information
Arlind-dev authored Jul 4, 2023
2 parents a1c9158 + 2548393 commit 8caaebd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This Python script renames video files in subdirectories named `Season YY` to th

- Folders that have videos in them which are already sorted the correct way: ex.
```
jellyfin_rename_videos/
.
├── Season 01/
│ ├── video1.mkv
│ ├── video2.mkv
Expand Down Expand Up @@ -39,7 +39,7 @@ pip install natsort
Suppose you have the following folder structure:

```
jellyfin_rename_videos/
.
├── Season 01/
│ ├── video1.mkv
│ ├── video2.mkv
Expand All @@ -53,28 +53,25 @@ jellyfin_rename_videos/
You run the script by typing `python jellyfin_rename_videos.py`, and enter `mkv` (or leave it blank) for the file extension. The script will display the following:

```
Enter a folder path (default is current directory):
Enter a directory path (default is current directory):
Enter a file extension (default is mkv):
./Season 01/video1.mkv --> ./Season 01/SE01EP01.mkv
./Season 01/video2.mkv --> ./Season 01/SE01EP02.mkv
./Season 01/video3.mkv --> ./Season 01/SE01EP03.mkv
.\Season 01\video1.mkv --> .\Season 01\SE01EP01.mkv
.\Season 01\video2.mkv --> .\Season 01\SE01EP02.mkv
.\Season 01\video3.mkv --> .\Season 01\SE01EP03.mkv
Do you want to proceed with renaming the files in Season 01? (y/n):
Done renaming files in Season 01.
./Season 02/video4.mkv --> ./Season 02/SE02EP01.mkv
./Season 02/video5.mkv --> ./Season 02/SE02EP02.mkv
./Season 02/video6.mkv --> ./Season 02/SE02EP03.mkv
.\Season 02\video4.mkv --> .\Season 02\SE02EP01.mkv
.\Season 02\video5.mkv --> .\Season 02\SE02EP02.mkv
.\Season 02\video6.mkv --> .\Season 02\SE02EP03.mkv
Do you want to proceed with renaming the files in Season 02? (y/n):
Done renaming files in Season 02.
All files renamed.
Done.
```
After confirming that you want to proceed with renaming the files for each season, the script will rename the files as follows:

```
jellyfin_rename_videos/
.
├── Season 01/
│ ├── SE01EP01.mkv
│ ├── SE01EP02.mkv
Expand Down
89 changes: 59 additions & 30 deletions jellyfin_rename.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
# Version: 1.0.2
# Author: Arlind-dev
# Version: 1.0.3
# Contributors: Arlind-dev
# Python 3.11.0

import os
from natsort import natsort_keygen

# Ask the user for a folder path to work in
folder_path = input("Enter a folder path (default is current directory): ")
# Ask the user for a directory path to work in
directory_path = input("Enter a directory path (default is current directory): ")

# Check if the user entered a value or not
if not folder_path:
folder_path = "." # Set default value to current directory
if not directory_path:
directory_path = "." # Set default value to current directory

# Resolve the folder path to an absolute path using os.path.abspath()
folder_path = os.path.abspath(folder_path)
# Resolve the directory path to an absolute path using os.path.abspath()
directory_path = os.path.abspath(directory_path)

# Check if the folder exists
if not os.path.exists(folder_path):
print(f"The folder {folder_path} does not exist.")
# Check if the directory exists
if not os.path.exists(directory_path):
print(f"The directory {directory_path} does not exist.")
exit()

# Create a list of all subdirectories containing "Season" in their name
season_directories = [
d
for d in os.listdir(directory_path)
if os.path.isdir(os.path.join(directory_path, d)) and "Season" in d
]

# Check if there are any season directories
if not season_directories:
print("No 'Season' directory found.")
print("For more information check out the README")
print("https://github.com/Arlind-dev/jellyfin-rename-videos/tree/main#requirements")
exit()

# Ask the user for a file extension to filter by
Expand All @@ -27,22 +41,29 @@
if not file_ext:
file_ext = "mkv"

# Create a list of all subdirectories containing "Season" in their name
season_dirs = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d)) and "Season" in d]

# Iterate through each season directory
for season_dir in season_dirs:
season_number_str = season_dir.split()[-1]
for season_directory in season_directories:
season_number_str = season_directory.split()[-1]
try:
season_number = int(season_number_str)
except ValueError:
print(f"Invalid season number: {season_number_str}")
print("'Season' directory can only contain 2 Digits (00-99)")
continue

print("")

# List the files in the season directory with the specified extension, sorted naturally
season_dir_path = os.path.join(folder_path, season_dir)
season_directory_path = os.path.join(directory_path, season_directory)
nkey = natsort_keygen()
files = sorted([f for f in os.listdir(season_dir_path) if f.lower().endswith("." + file_ext.lower())], key=nkey)
files = sorted(
[
f
for f in os.listdir(season_directory_path)
if f.lower().endswith("." + file_ext.lower())
],
key=nkey,
)

# Check if the number of files is a three-digit number
if len(files) > 99:
Expand All @@ -53,30 +74,38 @@
# Show the user the new filenames
renamed = False
for i, filename in enumerate(files, start=1):
old_path = os.path.join(season_dir_path, filename)
old_path = os.path.join(season_directory_path, filename)
new_filename = new_file_ext.format(i)
new_path = os.path.join(season_dir_path, new_filename)
rel_old_path = os.path.relpath(old_path, start=folder_path)
rel_new_path = os.path.relpath(new_path, start=folder_path)
print(f".{os.path.sep}{rel_old_path} --> .{os.path.sep}{rel_new_path}")
new_path = os.path.join(season_directory_path, new_filename)
rel_old_path = os.path.relpath(old_path, start=directory_path)
rel_new_path = os.path.relpath(new_path, start=directory_path)
print(f".{os.path.sep}{rel_old_path}\t -->\t .{os.path.sep}{rel_new_path}")
if filename != new_filename:
renamed = True

if not renamed:
print(f"Episodes in {season_dir} are already in the correct naming scheme.")
print(f"Episodes in {season_directory} dont need to be renamed.")
continue

# Ask the user if they want to proceed with renaming the files
confirmationRename = input(f"Do you want to proceed with renaming the files in {season_dir}? (y/n): ").strip().lower() or "y"
confirmationRename = (
input(
f"Do you want to proceed with renaming the files in {season_directory}? (y/n): "
)
.strip()
.lower()
or "y"
)
if confirmationRename != "y":
print(f"File renaming cancelled for {season_dir}.")
print(f"File renaming cancelled for {season_directory}.")
continue

print("")
# Rename the files according to the specified naming convention
for i, filename in enumerate(files, start=1):
old_path = os.path.join(season_dir_path, filename)
old_path = os.path.join(season_directory_path, filename)
new_filename = new_file_ext.format(i)
new_path = os.path.join(season_dir_path, new_filename)
new_path = os.path.join(season_directory_path, new_filename)
os.rename(old_path, new_path)

print("Done")
print("")
print("Done.")

0 comments on commit 8caaebd

Please sign in to comment.