Skip to content

Commit

Permalink
refactor: mediainfo module (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Dec 22, 2024
1 parent 778dc1c commit b66b8df
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
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():
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.

0 comments on commit b66b8df

Please sign in to comment.