mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-07-24 00:17:02 +08:00
Remove dependency on deprecated torchaudio.save function (#8815)
This commit is contained in:
parent
75d327abd5
commit
7eab7d2944
@ -133,14 +133,6 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
|
|||||||
if sample_rate != audio["sample_rate"]:
|
if sample_rate != audio["sample_rate"]:
|
||||||
waveform = torchaudio.functional.resample(waveform, audio["sample_rate"], sample_rate)
|
waveform = torchaudio.functional.resample(waveform, audio["sample_rate"], sample_rate)
|
||||||
|
|
||||||
# Create in-memory WAV buffer
|
|
||||||
wav_buffer = io.BytesIO()
|
|
||||||
torchaudio.save(wav_buffer, waveform, sample_rate, format="WAV")
|
|
||||||
wav_buffer.seek(0) # Rewind for reading
|
|
||||||
|
|
||||||
# Use PyAV to convert and add metadata
|
|
||||||
input_container = av.open(wav_buffer)
|
|
||||||
|
|
||||||
# Create output with specified format
|
# Create output with specified format
|
||||||
output_buffer = io.BytesIO()
|
output_buffer = io.BytesIO()
|
||||||
output_container = av.open(output_buffer, mode='w', format=format)
|
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
|
|||||||
output_container.metadata[key] = value
|
output_container.metadata[key] = value
|
||||||
|
|
||||||
# Set up the output stream with appropriate properties
|
# Set up the output stream with appropriate properties
|
||||||
input_container.streams.audio[0]
|
|
||||||
if format == "opus":
|
if format == "opus":
|
||||||
out_stream = output_container.add_stream("libopus", rate=sample_rate)
|
out_stream = output_container.add_stream("libopus", rate=sample_rate)
|
||||||
if quality == "64k":
|
if quality == "64k":
|
||||||
@ -175,10 +166,8 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
|
|||||||
else: #format == "flac":
|
else: #format == "flac":
|
||||||
out_stream = output_container.add_stream("flac", rate=sample_rate)
|
out_stream = output_container.add_stream("flac", rate=sample_rate)
|
||||||
|
|
||||||
|
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')
|
||||||
# Copy frames from input to output
|
frame.sample_rate = sample_rate
|
||||||
for frame in input_container.decode(audio=0):
|
|
||||||
frame.pts = None # Let PyAV handle timestamps
|
|
||||||
output_container.mux(out_stream.encode(frame))
|
output_container.mux(out_stream.encode(frame))
|
||||||
|
|
||||||
# Flush encoder
|
# Flush encoder
|
||||||
@ -186,7 +175,6 @@ def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=Non
|
|||||||
|
|
||||||
# Close containers
|
# Close containers
|
||||||
output_container.close()
|
output_container.close()
|
||||||
input_container.close()
|
|
||||||
|
|
||||||
# Write the output to file
|
# Write the output to file
|
||||||
output_buffer.seek(0)
|
output_buffer.seek(0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user