from abc import ABC, abstractmethod import pytube as pt class YouTubeAudioExtractor(ABC): @abstractmethod def extract(self, url: str, save_path: str) -> str: pass class PytubeAudioExtractor(YouTubeAudioExtractor): def __init__(self, only_audio: bool = True) -> None: self.only_audio = only_audio def extract(self, url: str, save_path: str = "yt_audio.mp3") -> str: yt = pt.YouTube(url) stream = yt.streams.filter(only_audio=self.only_audio)[0] stream.download(filename=save_path) return "yt_audio.mp3"