Update infer/lib/audio.py
Browse files- infer/lib/audio.py +56 -57
infer/lib/audio.py
CHANGED
|
@@ -1,57 +1,56 @@
|
|
| 1 |
-
import platform
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if format == "
|
| 16 |
-
format = "
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
file = clean_path(file)
|
| 37 |
-
if os.path.exists(file)
|
| 38 |
-
raise RuntimeError(
|
| 39 |
-
"You input a wrong audio path that does not
|
| 40 |
-
)
|
| 41 |
-
out, _ = (
|
| 42 |
-
ffmpeg.input(file, threads=0)
|
| 43 |
-
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
| 44 |
-
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
| 45 |
-
)
|
| 46 |
-
except Exception as e:
|
| 47 |
-
traceback.print_exc()
|
| 48 |
-
raise RuntimeError(f"Failed to load audio: {e}")
|
| 49 |
-
|
| 50 |
-
return np.frombuffer(out, np.float32).flatten()
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
return path_str.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
import os
|
| 3 |
+
import ffmpeg
|
| 4 |
+
import numpy as np
|
| 5 |
+
import av
|
| 6 |
+
import traceback
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def wav2(i, o, format):
|
| 11 |
+
inp = av.open(i, "rb")
|
| 12 |
+
if format == "m4a":
|
| 13 |
+
format = "mp4"
|
| 14 |
+
out = av.open(o, "wb", format=format)
|
| 15 |
+
if format == "ogg":
|
| 16 |
+
format = "libvorbis"
|
| 17 |
+
elif format == "mp4":
|
| 18 |
+
format = "aac"
|
| 19 |
+
|
| 20 |
+
ostream = out.add_stream(format)
|
| 21 |
+
|
| 22 |
+
for frame in inp.decode(audio=0):
|
| 23 |
+
for p in ostream.encode(frame):
|
| 24 |
+
out.mux(p)
|
| 25 |
+
|
| 26 |
+
for p in ostream.encode(None):
|
| 27 |
+
out.mux(p)
|
| 28 |
+
|
| 29 |
+
out.close()
|
| 30 |
+
inp.close()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def load_audio(file, sr):
|
| 34 |
+
try:
|
| 35 |
+
# Clean the file path
|
| 36 |
+
file = clean_path(file)
|
| 37 |
+
if not os.path.exists(file):
|
| 38 |
+
raise RuntimeError(
|
| 39 |
+
"You input a wrong audio path that does not exist, please fix it!"
|
| 40 |
+
)
|
| 41 |
+
out, _ = (
|
| 42 |
+
ffmpeg.input(file, threads=0)
|
| 43 |
+
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
| 44 |
+
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
| 45 |
+
)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
traceback.print_exc()
|
| 48 |
+
raise RuntimeError(f"Failed to load audio: {e}")
|
| 49 |
+
|
| 50 |
+
return np.frombuffer(out, np.float32).flatten()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def clean_path(path_str):
|
| 54 |
+
if platform.system() == "Windows":
|
| 55 |
+
path_str = path_str.replace("/", "\\")
|
| 56 |
+
return path_str.strip().strip('"').strip("\n").strip('"').strip()
|
|
|