Skip to content

Commit b66b8df

Browse files
authored
refactor: mediainfo module (#16)
1 parent 778dc1c commit b66b8df

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

animepipeline/mediainfo/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from animepipeline.mediainfo.mediainfomini import gen_file_name, FileNameInfo, rename_file, get_media_info # noqa
1+
from animepipeline.mediainfo.mediainfo import get_media_info # noqa
2+
from animepipeline.mediainfo.name import gen_file_name, rename_file # noqa
3+
from animepipeline.mediainfo.type import FileNameInfo, MediaInfo # noqa

animepipeline/mediainfo/mediainfomini.py animepipeline/mediainfo/mediainfo.py

+1-47
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pymediainfo
77
from loguru import logger
88

9-
from animepipeline.mediainfo.type import FileNameInfo, MediaInfo
9+
from animepipeline.mediainfo.type import MediaInfo
1010

1111

1212
def get_media_info(video_path: Union[str, Path]) -> MediaInfo:
@@ -133,49 +133,3 @@ def get_media_info(video_path: Union[str, Path]) -> MediaInfo:
133133
audios=audios,
134134
subtitles=subtitles,
135135
)
136-
137-
138-
def gen_file_name(anime_info: FileNameInfo) -> str:
139-
"""
140-
Auto generate the file name, based on the media info of the file
141-
142-
anime_info: FileNameInfo (path: xx.mkv, episode: 1, name: Fate/Kaleid Liner Prisma Illya, uploader: TensoRaws, type: WEBRip)
143-
144-
-> [TensoRaws] Fate/Kaleid Liner Prisma Illya [01] [WEBRip 1080p HEVC-10bit FLAC].mkv
145-
146-
:param anime_info: FileNameInfo
147-
:return:
148-
"""
149-
media_info = get_media_info(anime_info.path)
150-
resolution_heigh = str(media_info.resolution[1]) + "p"
151-
bit_depth = str(media_info.bit_depth) + "bit"
152-
153-
video_format = media_info.format
154-
155-
audio_format_list = [audio[2] for audio in media_info.audios]
156-
audio_format = "FLAC" if "FLAC" in audio_format_list else audio_format_list[0]
157-
158-
file_format = Path(anime_info.path).suffix
159-
160-
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}"
161-
162-
163-
def rename_file(anime_info: FileNameInfo) -> Path:
164-
"""
165-
Rename the file name, based on the media info of the file
166-
167-
:param anime_info: FileNameInfo
168-
:return:
169-
"""
170-
anime_path = Path(anime_info.path)
171-
172-
gen_name = gen_file_name(anime_info)
173-
gen_path = anime_path.parent / gen_name
174-
175-
if gen_path.exists():
176-
gen_path.unlink()
177-
logger.warning(f"Encode File already exists, remove it: {gen_path}")
178-
179-
anime_path.rename(gen_path)
180-
181-
return gen_path

animepipeline/mediainfo/name.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from pathlib import Path
2+
3+
from loguru import logger
4+
5+
from animepipeline.mediainfo.mediainfo import get_media_info
6+
from animepipeline.mediainfo.type import FileNameInfo
7+
8+
9+
def gen_file_name(anime_info: FileNameInfo) -> str:
10+
"""
11+
Auto generate the file name, based on the media info of the file
12+
13+
anime_info: FileNameInfo (path: xx.mkv, episode: 1, name: Fate/Kaleid Liner Prisma Illya, uploader: TensoRaws, type: WEBRip)
14+
15+
-> [TensoRaws] Fate/Kaleid Liner Prisma Illya [01] [WEBRip 1080p HEVC-10bit FLAC].mkv
16+
17+
:param anime_info: FileNameInfo
18+
:return:
19+
"""
20+
media_info = get_media_info(anime_info.path)
21+
resolution_heigh = str(media_info.resolution[1]) + "p"
22+
bit_depth = str(media_info.bit_depth) + "bit"
23+
24+
video_format = media_info.format
25+
26+
audio_format_list = [audio[2] for audio in media_info.audios]
27+
audio_format = "FLAC" if "FLAC" in audio_format_list else audio_format_list[0]
28+
29+
file_format = Path(anime_info.path).suffix
30+
31+
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}"
32+
33+
34+
def rename_file(anime_info: FileNameInfo) -> Path:
35+
"""
36+
Rename the file name, based on the media info of the file
37+
38+
:param anime_info: FileNameInfo
39+
:return:
40+
"""
41+
anime_path = Path(anime_info.path)
42+
43+
gen_name = gen_file_name(anime_info)
44+
gen_path = anime_path.parent / gen_name
45+
46+
if gen_path.exists():
47+
gen_path.unlink()
48+
logger.warning(f"Encode File already exists, remove it: {gen_path}")
49+
50+
anime_path.rename(gen_path)
51+
52+
return gen_path

tests/test_template.py

Whitespace-only changes.

0 commit comments

Comments
 (0)