feat: restructure renamer package and implement media extraction features

- Updated `pyproject.toml` to reflect new package structure.
- Created `renamer/__init__.py` to initialize the package.
- Implemented `RenamerApp` in `renamer/app.py` for the main application interface.
- Added constants for video extensions in `renamer/constants.py`.
- Developed `MediaExtractor` class in `renamer/extractor.py` for extracting metadata from media files.
- Created various extractor classes in `renamer/extractors/` for handling filename, metadata, and media info extraction.
- Added formatting classes in `renamer/formatters/` for displaying media information and proposed filenames.
- Implemented utility functions in `renamer/utils.py` for detecting file types and extracting media track information.
- Introduced `OpenScreen` in `renamer/screens.py` for user input of directory paths.
- Enhanced error handling and user feedback throughout the application.
This commit is contained in:
sha
2025-12-25 03:10:40 +00:00
parent 9e331e58ce
commit 305dd5f43e
21 changed files with 792 additions and 329 deletions
View File
+25
View File
@@ -0,0 +1,25 @@
from pathlib import Path
class FileInfoExtractor:
"""Class to extract file information"""
@staticmethod
def extract_size(file_path: Path) -> int:
"""Extract file size in bytes"""
return file_path.stat().st_size
@staticmethod
def extract_modification_time(file_path: Path) -> float:
"""Extract file modification time"""
return file_path.stat().st_mtime
@staticmethod
def extract_file_name(file_path: Path) -> str:
"""Extract file name"""
return file_path.name
@staticmethod
def extract_file_path(file_path: Path) -> str:
"""Extract full file path as string"""
return str(file_path)
+59
View File
@@ -0,0 +1,59 @@
import re
from pathlib import Path
from ..constants import SOURCE_DICT
class FilenameExtractor:
"""Class to extract information from filename"""
@staticmethod
def extract_title(file_path: Path) -> str | None:
"""Extract movie title from filename"""
file_name = file_path.name
temp_name = re.sub(r'\s*\(\d{4}\)\s*|\s*\d{4}\s*|\.\d{4}\.', '', file_name)
# Find and remove source
source = FilenameExtractor.extract_source(file_path)
if source:
for alias in SOURCE_DICT[source]:
temp_name = re.sub(r'\b' + re.escape(alias) + r'\b', '', temp_name, flags=re.IGNORECASE)
return temp_name.rsplit('.', 1)[0].strip()
@staticmethod
def extract_year(file_path: Path) -> str | None:
"""Extract year from filename"""
file_name = file_path.name
year_match = re.search(r'\((\d{4})\)|(\d{4})', file_name)
return (year_match.group(1) or year_match.group(2)) if year_match else None
@staticmethod
def extract_source(file_path: Path) -> str | None:
"""Extract video source from filename"""
file_name = file_path.name
temp_name = re.sub(r'\s*\(\d{4}\)\s*|\s*\d{4}\s*|\.\d{4}\.', '', file_name)
for src, aliases in SOURCE_DICT.items():
for alias in aliases:
if re.search(r'\b' + re.escape(alias) + r'\b', temp_name, re.IGNORECASE):
return src
return None
@staticmethod
def extract_resolution(file_path: Path) -> str | None:
"""Extract resolution from filename (e.g., 2160p, 1080p, 720p)"""
file_name = file_path.name
match = re.search(r'(\d{3,4})[pi]', file_name, re.IGNORECASE)
if match:
height = int(match.group(1))
if height >= 2160:
return '2160p'
elif height >= 1080:
return '1080p'
elif height >= 720:
return '720p'
elif height >= 480:
return '480p'
else:
return f'{height}p'
return None
+76
View File
@@ -0,0 +1,76 @@
from pathlib import Path
from pymediainfo import MediaInfo
from collections import Counter
class MediaInfoExtractor:
"""Class to extract information from MediaInfo"""
def __init__(self):
self.lang_map = {
'en': 'eng', 'fr': 'fre', 'de': 'ger', 'uk': 'ukr', 'ru': 'rus',
'es': 'spa', 'it': 'ita', 'pt': 'por', 'ja': 'jpn', 'ko': 'kor',
'zh': 'chi', 'und': 'und'
}
def extract_resolution(self, file_path: Path) -> str | None:
"""Extract resolution from media info"""
try:
media_info = MediaInfo.parse(file_path)
video_tracks = [t for t in media_info.tracks if t.track_type == 'Video']
if video_tracks:
height = getattr(video_tracks[0], 'height', None)
if height:
if height >= 2160:
return '2160p'
elif height >= 1080:
return '1080p'
elif height >= 720:
return '720p'
elif height >= 480:
return '480p'
else:
return f'{height}p'
except:
pass
return None
def extract_hdr(self, file_path: Path) -> str | None:
"""Extract HDR info from media info"""
try:
media_info = MediaInfo.parse(file_path)
video_tracks = [t for t in media_info.tracks if t.track_type == 'Video']
if video_tracks:
profile = getattr(video_tracks[0], 'format_profile', '')
if 'HDR' in profile.upper():
return 'HDR'
except:
pass
return None
def extract_audio_langs(self, file_path: Path) -> str:
"""Extract audio languages from media info"""
try:
media_info = MediaInfo.parse(file_path)
audio_tracks = [t for t in media_info.tracks if t.track_type == 'Audio']
langs = [getattr(a, 'language', 'und').lower()[:3] for a in audio_tracks]
langs = [self.lang_map.get(lang, lang) for lang in langs]
lang_counts = Counter(langs)
audio_langs = [f"{count}{lang}" if count > 1 else lang for lang, count in lang_counts.items()]
return ','.join(audio_langs)
except:
return ''
def extract_video_dimensions(self, file_path: Path) -> tuple[int, int] | None:
"""Extract video width and height"""
try:
media_info = MediaInfo.parse(file_path)
video_tracks = [t for t in media_info.tracks if t.track_type == 'Video']
if video_tracks:
width = getattr(video_tracks[0], 'width', None)
height = getattr(video_tracks[0], 'height', None)
if width and height:
return width, height
except:
pass
return None
+48
View File
@@ -0,0 +1,48 @@
import mutagen
from pathlib import Path
class MetadataExtractor:
"""Class to extract information from file metadata"""
@staticmethod
def extract_title(file_path: Path) -> str | None:
"""Extract title from metadata"""
try:
info = mutagen.File(file_path)
if info:
return getattr(info, 'title', None) or getattr(info, 'get', lambda x, default=None: default)('title', [None])[0]
except:
pass
return None
@staticmethod
def extract_duration(file_path: Path) -> float | None:
"""Extract duration from metadata"""
try:
info = mutagen.File(file_path)
if info:
return getattr(info, 'length', None)
except:
pass
return None
@staticmethod
def extract_artist(file_path: Path) -> str | None:
"""Extract artist from metadata"""
try:
info = mutagen.File(file_path)
if info:
return getattr(info, 'artist', None) or getattr(info, 'get', lambda x, default=None: default)('artist', [None])[0]
except:
pass
return None
@staticmethod
def extract_all_metadata(file_path: Path) -> dict:
"""Extract all metadata"""
return {
'title': MetadataExtractor.extract_title(file_path),
'duration': MetadataExtractor.extract_duration(file_path),
'artist': MetadataExtractor.extract_artist(file_path)
}