@@ -43,9 +43,14 @@ def float_np_array_to_wav_buf(wav: np.ndarray, sr: int, f32=False) -> BytesIO:
4343 return buf
4444
4545
46- def save_audio (path : str , audio : np .ndarray , sr : int , f32 = False ):
46+ def save_audio (path : str , audio : np .ndarray , sr : int , f32 = False , format = "wav" ):
47+ buf = float_np_array_to_wav_buf (audio , sr , f32 )
48+ if format != "wav" :
49+ transbuf = BytesIO ()
50+ wav2 (buf , transbuf , format )
51+ buf = transbuf
4752 with open (path , "wb" ) as f :
48- f .write (float_np_array_to_wav_buf ( audio , sr , f32 ) .getbuffer ())
53+ f .write (buf .getbuffer ())
4954
5055
5156def wav2 (i : BytesIO , o : BufferedWriter , format : str ):
@@ -109,7 +114,7 @@ def process_packet(packet: List[AudioFrame]):
109114 frames_data = []
110115 rate = 0
111116 for frame in packet :
112- frame .pts = None # 清除时间戳,避免重新采样问题
117+ # frame.pts = None # 清除时间戳,避免重新采样问题
113118 resampled_frames = (
114119 resampler .resample (frame ) if resampler is not None else [frame ]
115120 )
@@ -137,6 +142,8 @@ def frame_iter(container):
137142
138143 np .copyto (decoded_audio [..., offset :end_index ], frame_data )
139144 offset += len (frame_data [0 ])
145+
146+ container .close ()
140147
141148 # Truncate the array to the actual size
142149 decoded_audio = decoded_audio [..., :offset ]
@@ -149,43 +156,6 @@ def frame_iter(container):
149156 return decoded_audio , rate
150157
151158
152- def downsample_audio (
153- input_path : str , output_path : str , format : str , br = 128_000
154- ) -> None :
155- """
156- default to 128kb/s (equivalent to -q:a 2)
157- """
158- if not os .path .exists (input_path ):
159- return
160-
161- input_container = av .open (input_path )
162- output_container = av .open (output_path , "w" )
163-
164- # Create a stream in the output container
165- input_stream = input_container .streams .audio [0 ]
166- output_stream = output_container .add_stream (format )
167-
168- output_stream .bit_rate = br
169-
170- # Copy packets from the input file to the output file
171- for packet in input_container .demux (input_stream ):
172- for frame in packet .decode ():
173- for out_packet in output_stream .encode (frame ):
174- output_container .mux (out_packet )
175-
176- for packet in output_stream .encode ():
177- output_container .mux (packet )
178-
179- # Close the containers
180- input_container .close ()
181- output_container .close ()
182-
183- try : # Remove the original file
184- os .remove (input_path )
185- except Exception as e :
186- print (f"Failed to remove the original file: { e } " )
187-
188-
189159def resample_audio (
190160 input_path : str , output_path : str , codec : str , format : str , sr : int , layout : str
191161) -> None :
@@ -204,7 +174,7 @@ def resample_audio(
204174 # Copy packets from the input file to the output file
205175 for packet in input_container .demux (input_stream ):
206176 for frame in packet .decode ():
207- frame .pts = None # Clear presentation timestamp to avoid resampling issues
177+ # frame.pts = None # Clear presentation timestamp to avoid resampling issues
208178 out_frames = resampler .resample (frame )
209179 for out_frame in out_frames :
210180 for out_packet in output_stream .encode (out_frame ):
@@ -217,10 +187,6 @@ def resample_audio(
217187 input_container .close ()
218188 output_container .close ()
219189
220- try : # Remove the original file
221- os .remove (input_path )
222- except Exception as e :
223- print (f"Failed to remove the original file: { e } " )
224190
225191
226192def get_audio_properties (input_path : str ) -> Tuple [int , int ]:
0 commit comments