forked from viking-man/subtitles_translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (26 loc) · 936 Bytes
/
utils.py
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
import logging
import os
import numpy as np
import ffmpeg
# 检查文件是否存在
def check_exists(output, force):
if os.path.exists(output):
if force:
logging.info(f"{output} exists. Will overwrite it")
else:
logging.info(
f"{output} exists, skipping... Use the --force flag to overwrite"
)
return True
return False
# 使用ffmpeg分离出音频文件
def load_audio(file: str, sr: int = 16000) -> np.ndarray:
try:
out, _ = (
ffmpeg.input(file, threads=0)
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=sr)
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0