Skip to content

Commit

Permalink
add select Sampling rate
Browse files Browse the repository at this point in the history
add select Sampling rate
  • Loading branch information
zhaarey authored May 31, 2024
2 parents 841c6e7 + 8d4c8e2 commit 73e2117
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Download(BaseModel):
saveCover: bool
coverFormat: str
coverSize: str
alacMax: int
atmosMax: int
afterDownloaded: str


Expand Down
6 changes: 3 additions & 3 deletions src/mp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/rip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 15 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down

0 comments on commit 73e2117

Please sign in to comment.