1
0
mirror of https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git synced 2026-06-08 03:55:47 +08:00

feat(audio): use PyAV instead of ffmpeg

replaced usage of ffmpeg in favor of PyAV (`av`)
This commit is contained in:
Alex Murkoff
2024-06-11 12:08:37 +07:00
parent 9d699b1d99
commit 76ba0e20ff
6 changed files with 126 additions and 67 deletions

View File

@@ -1,9 +1,9 @@
from io import BufferedWriter, BytesIO
from pathlib import Path
from typing import Dict
import ffmpeg
import numpy as np
import av
from av.audio.resampler import AudioResampler
video_format_dict: Dict[str, str] = {
"m4a": "mp4",
@@ -38,19 +38,20 @@ def load_audio(file: str, sr: int) -> np.ndarray:
raise FileNotFoundError(f"File not found: {file}")
try:
# 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 = str(clean_path(file)) # 防止小白拷路径头尾带了空格和"和回车
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)
)
container = av.open(file)
resampler = AudioResampler(format='fltp', layout='mono', rate=sr)
decoded_audio = []
for frame in container.decode(audio=0):
frame.pts = None # Clear presentation timestamp to avoid resampling issues
resampled = resampler.resample(frame)
decoded_audio.append(resampled.to_ndarray())
audio = np.concatenate(decoded_audio)
except Exception as e:
raise RuntimeError(f"Failed to load audio: {e}")
return np.frombuffer(out, np.float32).flatten()
return audio.flatten()
def clean_path(path: str) -> Path: