Blane187 commited on
Commit
ad8a960
·
verified ·
1 Parent(s): e52d4e5

Update infer/lib/audio.py

Browse files
Files changed (1) hide show
  1. infer/lib/audio.py +56 -57
infer/lib/audio.py CHANGED
@@ -1,57 +1,56 @@
1
- import platform, os
2
- import ffmpeg
3
- import numpy as np
4
- import av
5
- from io import BytesIO
6
-
7
-
8
- def wav2(i, o, format):
9
- inp = av.open(i, "rb")
10
- if format == "m4a":
11
- format = "mp4"
12
- out = av.open(o, "wb", format=format)
13
- if format == "ogg":
14
- format = "libvorbis"
15
- if format == "mp4":
16
- format = "aac"
17
-
18
- ostream = out.add_stream(format)
19
-
20
- for frame in inp.decode(audio=0):
21
- for p in ostream.encode(frame):
22
- out.mux(p)
23
-
24
- for p in ostream.encode(None):
25
- out.mux(p)
26
-
27
- out.close()
28
- inp.close()
29
-
30
-
31
- def load_audio(file, sr):
32
- try:
33
- # https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
34
- # This launches a subprocess to decode audio while down-mixing and resampling as necessary.
35
- # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
36
- file = clean_path(file) # 防止小白拷路径头尾带了空格和"和回车
37
- if os.path.exists(file) == False:
38
- raise RuntimeError(
39
- "You input a wrong audio path that does not exists, 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
-
54
- def clean_path(path_str):
55
- if platform.system() == "Windows":
56
- path_str = path_str.replace("/", "\\")
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()