@@ -133,14 +133,6 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
133133 if sample_rate != audio ["sample_rate" ]:
134134 waveform = torchaudio .functional .resample (waveform , audio ["sample_rate" ], sample_rate )
135135
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-
144136 # Create output with specified format
145137 output_buffer = io .BytesIO ()
146138 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
150142 output_container .metadata [key ] = value
151143
152144 # Set up the output stream with appropriate properties
153- input_container .streams .audio [0 ]
154145 if format == "opus" :
155146 out_stream = output_container .add_stream ("libopus" , rate = sample_rate )
156147 if quality == "64k" :
@@ -175,18 +166,15 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
175166 else : #format == "flac":
176167 out_stream = output_container .add_stream ("flac" , rate = sample_rate )
177168
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 ))
183172
184173 # Flush encoder
185174 output_container .mux (out_stream .encode (None ))
186175
187176 # Close containers
188177 output_container .close ()
189- input_container .close ()
190178
191179 # Write the output to file
192180 output_buffer .seek (0 )
0 commit comments