1
- import platform
1
+ from io import BufferedWriter , BytesIO
2
+ from pathlib import Path
3
+ from typing import Dict
2
4
import ffmpeg
3
5
import numpy as np
4
6
import av
5
7
8
+ video_format_dict : Dict [str , str ] = {
9
+ "m4a" : "mp4" ,
10
+ }
6
11
7
- def wav2 (i , o , format ):
12
+ audio_format_dict : Dict [str , str ] = {
13
+ "ogg" : "libvorbis" ,
14
+ "mp4" : "aac" ,
15
+ }
16
+
17
+ def wav2 (i : BytesIO , o : BufferedWriter , format : str ):
8
18
inp = av .open (i , "r" )
9
- if format == "m4a" :
10
- format = "mp4"
19
+ format = video_format_dict .get (format , format )
11
20
out = av .open (o , "w" , format = format )
12
- if format == "ogg" :
13
- format = "libvorbis"
14
- if format == "mp4" :
15
- format = "aac"
21
+ format = audio_format_dict .get (format , format )
16
22
17
23
ostream = out .add_stream (format )
18
24
@@ -27,12 +33,15 @@ def wav2(i, o, format):
27
33
inp .close ()
28
34
29
35
30
- def load_audio (file , sr ):
36
+ def load_audio (file : str , sr : int ) -> np .ndarray :
37
+ if not Path (file ).exists ():
38
+ raise FileNotFoundError (f"File not found: { file } " )
39
+
31
40
try :
32
41
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
33
42
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
34
43
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
35
- file = clean_path (file ) # 防止小白拷路径头尾带了空格和"和回车
44
+ file = str ( clean_path (file ) ) # 防止小白拷路径头尾带了空格和"和回车
36
45
out , _ = (
37
46
ffmpeg .input (file , threads = 0 )
38
47
.output ("-" , format = "f32le" , acodec = "pcm_f32le" , ac = 1 , ar = sr )
@@ -44,7 +53,5 @@ def load_audio(file, sr):
44
53
return np .frombuffer (out , np .float32 ).flatten ()
45
54
46
55
47
- def clean_path (path_str ):
48
- if platform .system () == "Windows" :
49
- path_str = path_str .replace ("/" , "\\ " )
50
- return path_str .strip (" " ).strip ('"' ).strip ("\n " ).strip ('"' ).strip (" " )
56
+ def clean_path (path : str ) -> Path :
57
+ return Path (path .strip (' "\n ' )).resolve ()
0 commit comments