Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mediainfo module #16

Merged
merged 4 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion animepipeline/mediainfo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from animepipeline.mediainfo.mediainfomini import gen_file_name, FileNameInfo, rename_file, get_media_info # noqa
from animepipeline.mediainfo.mediainfo import get_media_info # noqa
from animepipeline.mediainfo.name import gen_file_name, rename_file # noqa
from animepipeline.mediainfo.type import FileNameInfo, MediaInfo # noqa
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pymediainfo
from loguru import logger

from animepipeline.mediainfo.type import FileNameInfo, MediaInfo
from animepipeline.mediainfo.type import MediaInfo


def get_media_info(video_path: Union[str, Path]) -> MediaInfo:
Expand Down Expand Up @@ -133,49 +133,3 @@ def get_media_info(video_path: Union[str, Path]) -> MediaInfo:
audios=audios,
subtitles=subtitles,
)


def gen_file_name(anime_info: FileNameInfo) -> str:
"""
Auto generate the file name, based on the media info of the file

anime_info: FileNameInfo (path: xx.mkv, episode: 1, name: Fate/Kaleid Liner Prisma Illya, uploader: TensoRaws, type: WEBRip)

-> [TensoRaws] Fate/Kaleid Liner Prisma Illya [01] [WEBRip 1080p HEVC-10bit FLAC].mkv

:param anime_info: FileNameInfo
:return:
"""
media_info = get_media_info(anime_info.path)
resolution_heigh = str(media_info.resolution[1]) + "p"
bit_depth = str(media_info.bit_depth) + "bit"

video_format = media_info.format

audio_format_list = [audio[2] for audio in media_info.audios]
audio_format = "FLAC" if "FLAC" in audio_format_list else audio_format_list[0]

file_format = Path(anime_info.path).suffix

return f"[{anime_info.uploader}] {anime_info.name} [{str(anime_info.episode).zfill(2)}] [{anime_info.type} {resolution_heigh} {video_format}-{bit_depth} {audio_format}]{file_format}"


def rename_file(anime_info: FileNameInfo) -> Path:
"""
Rename the file name, based on the media info of the file

:param anime_info: FileNameInfo
:return:
"""
anime_path = Path(anime_info.path)

gen_name = gen_file_name(anime_info)
gen_path = anime_path.parent / gen_name

if gen_path.exists():
gen_path.unlink()
logger.warning(f"Encode File already exists, remove it: {gen_path}")

anime_path.rename(gen_path)

return gen_path
52 changes: 52 additions & 0 deletions animepipeline/mediainfo/name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pathlib import Path

from loguru import logger

from animepipeline.mediainfo.mediainfo import get_media_info
from animepipeline.mediainfo.type import FileNameInfo


def gen_file_name(anime_info: FileNameInfo) -> str:
"""
Auto generate the file name, based on the media info of the file

anime_info: FileNameInfo (path: xx.mkv, episode: 1, name: Fate/Kaleid Liner Prisma Illya, uploader: TensoRaws, type: WEBRip)

-> [TensoRaws] Fate/Kaleid Liner Prisma Illya [01] [WEBRip 1080p HEVC-10bit FLAC].mkv

:param anime_info: FileNameInfo
:return:
"""
media_info = get_media_info(anime_info.path)
resolution_heigh = str(media_info.resolution[1]) + "p"
bit_depth = str(media_info.bit_depth) + "bit"

video_format = media_info.format

audio_format_list = [audio[2] for audio in media_info.audios]
audio_format = "FLAC" if "FLAC" in audio_format_list else audio_format_list[0]

file_format = Path(anime_info.path).suffix

return f"[{anime_info.uploader}] {anime_info.name} [{str(anime_info.episode).zfill(2)}] [{anime_info.type} {resolution_heigh} {video_format}-{bit_depth} {audio_format}]{file_format}"


def rename_file(anime_info: FileNameInfo) -> Path:
"""
Rename the file name, based on the media info of the file

:param anime_info: FileNameInfo
:return:
"""
anime_path = Path(anime_info.path)

gen_name = gen_file_name(anime_info)
gen_path = anime_path.parent / gen_name

if gen_path.exists():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Consider safer handling of existing files instead of automatic deletion

Automatically deleting existing files could lead to data loss. Consider either adding a numeric suffix to create a unique filename or raising an exception to let the caller decide how to handle conflicts.

gen_path.unlink()
logger.warning(f"Encode File already exists, remove it: {gen_path}")

anime_path.rename(gen_path)

return gen_path
Empty file added tests/test_template.py
Empty file.