@@ -133,14 +133,6 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
133
133
if sample_rate != audio ["sample_rate" ]:
134
134
waveform = torchaudio .functional .resample (waveform , audio ["sample_rate" ], sample_rate )
135
135
136
- # Create in-memory WAV buffer
137
- wav_buffer = io .BytesIO ()
138
- torchaudio .save (wav_buffer , waveform , sample_rate , format = "WAV" )
139
- wav_buffer .seek (0 ) # Rewind for reading
140
-
141
- # Use PyAV to convert and add metadata
142
- input_container = av .open (wav_buffer )
143
-
144
136
# Create output with specified format
145
137
output_buffer = io .BytesIO ()
146
138
output_container = av .open (output_buffer , mode = 'w' , format = format )
@@ -150,7 +142,6 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
150
142
output_container .metadata [key ] = value
151
143
152
144
# Set up the output stream with appropriate properties
153
- input_container .streams .audio [0 ]
154
145
if format == "opus" :
155
146
out_stream = output_container .add_stream ("libopus" , rate = sample_rate )
156
147
if quality == "64k" :
@@ -175,18 +166,15 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
175
166
else : #format == "flac":
176
167
out_stream = output_container .add_stream ("flac" , rate = sample_rate )
177
168
178
-
179
- # Copy frames from input to output
180
- for frame in input_container .decode (audio = 0 ):
181
- frame .pts = None # Let PyAV handle timestamps
182
- output_container .mux (out_stream .encode (frame ))
169
+ frame = av .AudioFrame .from_ndarray (waveform .movedim (0 , 1 ).reshape (1 , - 1 ).float ().numpy (), format = 'flt' , layout = 'mono' if waveform .shape [0 ] == 1 else 'stereo' )
170
+ frame .sample_rate = sample_rate
171
+ output_container .mux (out_stream .encode (frame ))
183
172
184
173
# Flush encoder
185
174
output_container .mux (out_stream .encode (None ))
186
175
187
176
# Close containers
188
177
output_container .close ()
189
- input_container .close ()
190
178
191
179
# Write the output to file
192
180
output_buffer .seek (0 )
0 commit comments