Added download real time feature back.

This commit is contained in:
KDalu 2025-01-21 21:34:05 -05:00
parent 7d8b3ddb39
commit 8d80719f6b
2 changed files with 35 additions and 1 deletions

View file

@ -283,7 +283,7 @@ class App:
desc=f"({count}/{total}) {track.name}", desc=f"({count}/{total}) {track.name}",
total=track.input_stream.size, total=track.input_stream.size,
) as p_bar: ) as p_bar:
file = track.write_audio_stream(output, p_bar) file = track.write_audio_stream(output, p_bar, self.__config.download_real_time)
# Download lyrics # Download lyrics
if playable.type == PlayableType.TRACK and self.__config.lyrics_file: if playable.type == PlayableType.TRACK and self.__config.lyrics_file:

View file

@ -1,5 +1,6 @@
from math import floor from math import floor
from pathlib import Path from pathlib import Path
from time import time, sleep
from librespot.core import PlayableContentFeeder from librespot.core import PlayableContentFeeder
from librespot.metadata import AlbumId from librespot.metadata import AlbumId
@ -197,6 +198,39 @@ class Track(PlayableContentFeeder.LoadedStream, Playable):
) )
return self.__lyrics return self.__lyrics
def write_audio_stream(
self,
output: Path | str,
p_bar: tqdm = tqdm(disable=True),
real_time: bool = False,
) -> LocalFile:
"""
Writes audio stream to file
Args:
output: File path of saved audio stream
p_bar: tqdm progress bar
real_time: Enable delay to emulate real time streaming
Returns:
LocalFile object
"""
if not isinstance(output, Path):
output = Path(output).expanduser()
file = f"{output}.ogg"
time_start = time()
downloaded = 0
with open(file, "wb") as f, p_bar as p_bar:
chunk = None
while chunk != b"":
chunk = self.input_stream.stream().read(1024)
p_bar.update(f.write(chunk))
if real_time:
downloaded += len(chunk)
delta_current = time() - time_start
delta_required = (downloaded / self.input_stream.size) * (self.duration/1000)
if delta_required > delta_current:
sleep(delta_required - delta_current)
return LocalFile(Path(file), AudioFormat.VORBIS)
class Episode(PlayableContentFeeder.LoadedStream, Playable): class Episode(PlayableContentFeeder.LoadedStream, Playable):
def __init__(self, episode: PlayableContentFeeder.LoadedStream, api): def __init__(self, episode: PlayableContentFeeder.LoadedStream, api):