2024-01-26 09:10:04 +01:00
|
|
|
import platform, os
|
2024-01-23 08:15:04 +01:00
|
|
|
import ffmpeg
|
2023-08-19 13:00:56 +02:00
|
|
|
import numpy as np
|
2023-09-03 09:19:19 +02:00
|
|
|
import av
|
|
|
|
from io import BytesIO
|
2024-06-26 15:15:01 +02:00
|
|
|
import traceback
|
2024-11-23 13:46:42 +01:00
|
|
|
import re
|
2023-08-19 13:00:56 +02:00
|
|
|
|
2023-09-14 02:34:30 +02:00
|
|
|
|
2023-09-03 09:19:19 +02:00
|
|
|
def wav2(i, o, format):
|
2023-09-14 02:34:30 +02:00
|
|
|
inp = av.open(i, "rb")
|
|
|
|
if format == "m4a":
|
|
|
|
format = "mp4"
|
|
|
|
out = av.open(o, "wb", format=format)
|
|
|
|
if format == "ogg":
|
|
|
|
format = "libvorbis"
|
|
|
|
if format == "mp4":
|
|
|
|
format = "aac"
|
2023-09-03 09:19:19 +02:00
|
|
|
|
|
|
|
ostream = out.add_stream(format)
|
|
|
|
|
|
|
|
for frame in inp.decode(audio=0):
|
2023-09-14 02:34:30 +02:00
|
|
|
for p in ostream.encode(frame):
|
|
|
|
out.mux(p)
|
2023-09-03 09:19:19 +02:00
|
|
|
|
2023-09-14 02:34:30 +02:00
|
|
|
for p in ostream.encode(None):
|
|
|
|
out.mux(p)
|
2023-09-03 09:19:19 +02:00
|
|
|
|
|
|
|
out.close()
|
|
|
|
inp.close()
|
|
|
|
|
2023-09-14 02:34:30 +02:00
|
|
|
|
2023-08-19 13:00:56 +02:00
|
|
|
def load_audio(file, sr):
|
|
|
|
try:
|
2024-01-23 08:15:04 +01:00
|
|
|
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
|
|
|
|
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
|
|
|
|
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
|
|
|
|
file = clean_path(file) # 防止小白拷路径头尾带了空格和"和回车
|
2024-06-14 13:56:10 +02:00
|
|
|
if os.path.exists(file) == False:
|
|
|
|
raise RuntimeError(
|
|
|
|
"You input a wrong audio path that does not exists, please fix it!"
|
|
|
|
)
|
2024-01-23 08:15:04 +01:00
|
|
|
out, _ = (
|
|
|
|
ffmpeg.input(file, threads=0)
|
|
|
|
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
|
|
|
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2024-06-14 13:56:10 +02:00
|
|
|
traceback.print_exc()
|
2024-01-23 08:15:04 +01:00
|
|
|
raise RuntimeError(f"Failed to load audio: {e}")
|
|
|
|
|
|
|
|
return np.frombuffer(out, np.float32).flatten()
|
2023-08-27 12:14:01 +02:00
|
|
|
|
|
|
|
|
2024-06-14 13:56:10 +02:00
|
|
|
|
2024-01-23 08:15:04 +01:00
|
|
|
def clean_path(path_str):
|
2024-01-26 09:10:04 +01:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
path_str = path_str.replace("/", "\\")
|
2024-11-23 13:46:42 +01:00
|
|
|
path_str = re.sub(r'[\u202a\u202b\u202c\u202d\u202e]', '', path_str) # 移除 Unicode 控制字符
|
2024-01-23 08:15:04 +01:00
|
|
|
return path_str.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|