Spaces:
Sleeping
Sleeping
Create youtube_audio_downloadTool.py
Browse files
youtube_audio_downloadTool.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents.tools import Tool
|
2 |
+
from pytube import YouTube
|
3 |
+
import os
|
4 |
+
|
5 |
+
class YouTubeAudioDownloadTool(Tool):
|
6 |
+
name = "youtube_audio_download"
|
7 |
+
description = "Downloads the audio from a YouTube video and returns the local file path."
|
8 |
+
inputs = {"youtube_url": {"type": "string", "description": "URL of the YouTube video"}}
|
9 |
+
output_type = "string"
|
10 |
+
|
11 |
+
def __init__(self, download_dir: str = "./downloads"):
|
12 |
+
super().__init__()
|
13 |
+
self.download_dir = download_dir
|
14 |
+
os.makedirs(self.download_dir, exist_ok=True)
|
15 |
+
|
16 |
+
def forward(self, youtube_url: str) -> str:
|
17 |
+
yt = YouTube(youtube_url)
|
18 |
+
stream = yt.streams.filter(only_audio=True).first()
|
19 |
+
output_path = stream.download(output_path=self.download_dir, filename="temp_audio.mp3")
|
20 |
+
return output_path
|