File size: 2,595 Bytes
20d05ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import requests
import os
import ffmpeg
import numpy as np
import random
import shutil
import torchaudio
from pydub import AudioSegment
import tempfile
class Audio:
audio_path = "./audios"
def __init__(self, name, url):
self._name = name
self._url = url
if not os.path.exists(Audio.audio_path):
os.mkdir(Audio.audio_path)
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def url(self):
return self._url
@url.setter
def url(self, url):
self._url = url
def __str__(self):
return f'Audio: {self._name} {self._url}'
@classmethod
def load_audio(cls, file, sr):
try:
file = file.strip(' "\n') # Eliminar espacios y comillas del nombre del archivo
# Convertir a formato WAV si no lo está
if not file.endswith(".wav"):
file_formanted = f"{file}.wav"
if not os.path.isfile(file_formanted):
(
ffmpeg.input(file)
.output(file_formanted, format="wav")
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
)
else:
file_formanted = file
# Cargar el archivo formateado y devolverlo como NumPy array
out, _ = (
ffmpeg.input(file_formanted)
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
)
# Eliminar el archivo formateado
os.remove(file_formanted)
except Exception as e:
raise RuntimeError(f"Failed to load audio: {e}")
return np.frombuffer(out, np.float32).flatten()
@classmethod
def dowload_from_url(self, url = None, output = "./audios/file.wav"):
"""
Descarga un aduio desde una url
Args:
path: Folder where the audio will be downloaded
Returns:
return: the path of the downloaded audio
"""
request = requests.get(url, allow_redirects=True)
open(output, 'wb').write(request.content)
return output
def delete_files(paths):
for path in paths:
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)
if os.path.isfile(path):
os.remove(path) |