diff --git a/config.example.toml b/config.example.toml index 9a63de8..9de9837 100644 --- a/config.example.toml +++ b/config.example.toml @@ -85,6 +85,10 @@ saveLyrics = true saveCover = true coverFormat = "jpg" coverSize = "5000x5000" +# 192000 96000 48000 44100 +alacMax = 192000 +# 2768 2448 +atmosMax = 2768 # The command to be executed after the song is ripped successfully, passing in the filename parameter. # Example: "\"C:\\Program Files\\DAUM\\PotPlayer\\PotPlayerMini64.exe\" \"{filename}\"" # Pay attention to escaping issues diff --git a/src/config.py b/src/config.py index c7ff877..434c541 100644 --- a/src/config.py +++ b/src/config.py @@ -39,6 +39,8 @@ class Download(BaseModel): saveCover: bool coverFormat: str coverSize: str + alacMax: int + atmosMax: int afterDownloaded: str diff --git a/src/mp4.py b/src/mp4.py index 70d51de..c37cbb3 100644 --- a/src/mp4.py +++ b/src/mp4.py @@ -34,13 +34,13 @@ async def get_available_codecs(m3u8_url: str) -> Tuple[list[str], list[str]]: async def extract_media(m3u8_url: str, codec: str, song_metadata: SongMetadata, - codec_priority: list[str], alternative_codec: bool = False) -> Tuple[str, list[str], str, Optional[int], Optional[int]]: + codec_priority: list[str], alternative_codec: bool = False, alacMax: Optional[int] = None, atmosMax: Optional[int] = None) -> Tuple[str, list[str], str, Optional[int], Optional[int]]: parsed_m3u8 = m3u8.loads(await download_m3u8(m3u8_url), uri=m3u8_url) - specifyPlaylist = find_best_codec(parsed_m3u8, codec) + specifyPlaylist = find_best_codec(parsed_m3u8, codec, alacMax, atmosMax) if not specifyPlaylist and alternative_codec: logger.warning(f"Codec {codec} of song: {song_metadata.artist} - {song_metadata.title} did not found") for a_codec in codec_priority: - specifyPlaylist = find_best_codec(parsed_m3u8, a_codec) + specifyPlaylist = find_best_codec(parsed_m3u8, a_codec, alacMax, atmosMax) if specifyPlaylist: codec = a_codec break diff --git a/src/rip.py b/src/rip.py index a1110e2..d5b2bdb 100644 --- a/src/rip.py +++ b/src/rip.py @@ -77,11 +77,11 @@ async def rip_song(song: Song, auth_params: GlobalAuthParams, codec: str, config logger.info(f"Use m3u8 from device for song: {song_metadata.artist} - {song_metadata.title}") if specified_m3u8: song_uri, keys, codec_id, bit_depth, sample_rate = await extract_media( - specified_m3u8, codec, song_metadata, config.download.codecPriority, config.download.codecAlternative) + specified_m3u8, codec, song_metadata, config.download.codecPriority, config.download.codecAlternative, config.download.alacMax, config.download.alacMax) else: song_uri, keys, codec_id, bit_depth, sample_rate = await extract_media( song_data.attributes.extendedAssetUrls.enhancedHls, codec, song_metadata, - config.download.codecPriority, config.download.codecAlternative) + config.download.codecPriority, config.download.codecAlternative, config.download.alacMax, config.download.atmosMax) if all([bool(bit_depth), bool(sample_rate)]): song_metadata.set_bit_depth_and_sample_rate(bit_depth, sample_rate) if not force_save and check_song_exists(song_metadata, config.download, codec, playlist): diff --git a/src/utils.py b/src/utils.py index 1af7ccb..cfd279d 100644 --- a/src/utils.py +++ b/src/utils.py @@ -36,12 +36,24 @@ def byte_length(i): return (i.bit_length() + 7) // 8 -def find_best_codec(parsed_m3u8: m3u8.M3U8, codec: str) -> Optional[m3u8.Playlist]: - available_medias = [playlist for playlist in parsed_m3u8.playlists - if regex.match(CodecRegex.get_pattern_by_codec(codec), playlist.stream_info.audio)] +def find_best_codec(parsed_m3u8: m3u8.M3U8, codec: str, alacMax, atmosMax) -> Optional[m3u8.Playlist]: + available_medias = [] + for playlist in parsed_m3u8.playlists: + if regex.match(CodecRegex.get_pattern_by_codec(codec), playlist.stream_info.audio): + split_str = playlist.stream_info.audio.split('-') + if "alac" in playlist.stream_info.audio: + if int(split_str[3]) <= alacMax: + available_medias.append(playlist) + elif "atmos" in playlist.stream_info.audio: + if int(split_str[2]) <= atmosMax: + available_medias.append(playlist) + else: + available_medias.append(playlist) + if not available_medias: return None available_medias.sort(key=lambda x: x.stream_info.average_bandwidth, reverse=True) + return available_medias[0]