Create download_music.py
Browse files- tools/download_music.py +50 -0
tools/download_music.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
|
| 4 |
+
import yt_dlp
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
avar = ""
|
| 9 |
+
|
| 10 |
+
def my_hook(d):
|
| 11 |
+
if d['status'] == 'finished':
|
| 12 |
+
print("")
|
| 13 |
+
print(f"Download complete. File saved to: {d['filename']}")
|
| 14 |
+
print(f"A next step can be to play this file: {d['filename'].replace('.webm', '.mp3')}")
|
| 15 |
+
avar = d['filename']
|
| 16 |
+
|
| 17 |
+
class DownloadSomeMusicMeastro(Tool):
|
| 18 |
+
name = "download_music"
|
| 19 |
+
description = "Downloads a mp3 file which can be played later to the listener. This tool does not play the music, that will be done in the final answer"
|
| 20 |
+
inputs = {'youtube_id': {'type': 'string', 'description': 'The id of the song to play.'}}
|
| 21 |
+
output_type = "string" # because it is actualy a side-effect what this tool does do it download a file
|
| 22 |
+
|
| 23 |
+
def __init__(self, *args, **kwargs):
|
| 24 |
+
super().__init__()
|
| 25 |
+
|
| 26 |
+
def forward(self, youtube_id: str) -> str:
|
| 27 |
+
#json_file = open('json_config.txt')
|
| 28 |
+
#json_data = json.load(json_file)
|
| 29 |
+
json_data = {
|
| 30 |
+
"youtube_url": f"https://www.youtube.com/watch?v={youtube_id}",
|
| 31 |
+
"youtube_dl": {
|
| 32 |
+
"format": "bestaudio/best",
|
| 33 |
+
#"outtmpl": f"./downloads/{youtube_id} - %(title)s.%(ext)s",
|
| 34 |
+
"outtmpl": f"./downloads/{youtube_id}.%(ext)s",
|
| 35 |
+
"postprocessors": [{
|
| 36 |
+
"key": "FFmpegExtractAudio",
|
| 37 |
+
"preferredcodec": "mp3",
|
| 38 |
+
"preferredquality": "192"
|
| 39 |
+
}],
|
| 40 |
+
"progress_hooks": [my_hook]
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
with yt_dlp.YoutubeDL(json_data['youtube_dl']) as ydl:
|
| 45 |
+
|
| 46 |
+
print([json_data['youtube_url']])
|
| 47 |
+
ydl.download([json_data['youtube_url']])
|
| 48 |
+
|
| 49 |
+
time.sleep(2)
|
| 50 |
+
return f"File {youtube_id}.mp3 is downloaded"
|