File size: 624 Bytes
6249bc9
 
 
 
 
 
 
 
 
 
 
 
 
d5c9693
6249bc9
 
d5c9693
 
 
6249bc9
 
d5c9693
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"