mirror of
https://github.com/shadoll/moma.git
synced 2026-08-28 03:27:34 +00:00
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:
@@ -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
|
||||
Reference in New Issue
Block a user