Faster downloading

This commit is contained in:
BarsTiger
2023-10-31 12:54:02 +02:00
parent 8cd956388e
commit ee10a32391

View File

@@ -27,21 +27,33 @@ class YouTubeBytestream:
duration=int(duration), duration=int(duration),
) )
async def rerender(self):
segment = AudioSegment.from_file(
file=self.file
)
self.file = segment.export(BytesIO(), format='mp3', codec='libmp3lame')
return self
@define @define
class Downloader: class Downloader:
audio_stream: Stream audio_stream: Stream
filename: str filename: str
duration: int
@classmethod @classmethod
def from_id(cls, yt_id: str): def from_id(cls, yt_id: str):
video = YouTube.from_id(yt_id) video = YouTube.from_id(yt_id)
audio_stream = video.streams.filter( audio_stream = video.streams.filter(
only_audio=True, only_audio=True,
).order_by('abr').desc().first() ).order_by('abr').desc().first()
return cls( return cls(
audio_stream=audio_stream, audio_stream=audio_stream,
filename=f'{audio_stream.default_filename}.mp3', filename=f'{audio_stream.default_filename}.mp3',
duration=int(video.length),
) )
def __to_bytestream(self): def __to_bytestream(self):
@@ -49,14 +61,10 @@ class Downloader:
self.audio_stream.stream_to_buffer(audio_io) self.audio_stream.stream_to_buffer(audio_io)
audio_io.seek(0) audio_io.seek(0)
segment = AudioSegment.from_file(
file=audio_io
)
return YouTubeBytestream.from_bytestream( return YouTubeBytestream.from_bytestream(
segment.export(BytesIO(), format='mp3', codec='libmp3lame'), audio_io,
self.filename, self.filename,
segment.duration_seconds self.duration,
) )
async def to_bytestream(self): async def to_bytestream(self):