mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-06-08 07:07:14 +00:00
add opus and mp3 to audio output node (#8019)
* first pass at opus and mp3 as well as migrating flac to pyav * minor mp3 encoding fix * fix ruff * delete dead code * split out save audio to separate nodes per filetype * fix ruff
This commit is contained in:
parent
158419f3a0
commit
b4abca828e
@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import av
|
||||||
import torchaudio
|
import torchaudio
|
||||||
import torch
|
import torch
|
||||||
import comfy.model_management
|
import comfy.model_management
|
||||||
@ -7,7 +8,6 @@ import folder_paths
|
|||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import struct
|
|
||||||
import random
|
import random
|
||||||
import hashlib
|
import hashlib
|
||||||
import node_helpers
|
import node_helpers
|
||||||
@ -90,60 +90,118 @@ class VAEDecodeAudio:
|
|||||||
return ({"waveform": audio, "sample_rate": 44100}, )
|
return ({"waveform": audio, "sample_rate": 44100}, )
|
||||||
|
|
||||||
|
|
||||||
def create_vorbis_comment_block(comment_dict, last_block):
|
def save_audio(self, audio, filename_prefix="ComfyUI", format="flac", prompt=None, extra_pnginfo=None, quality="128k"):
|
||||||
vendor_string = b'ComfyUI'
|
|
||||||
vendor_length = len(vendor_string)
|
|
||||||
|
|
||||||
comments = []
|
filename_prefix += self.prefix_append
|
||||||
for key, value in comment_dict.items():
|
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
|
||||||
comment = f"{key}={value}".encode('utf-8')
|
results: list[FileLocator] = []
|
||||||
comments.append(struct.pack('<I', len(comment)) + comment)
|
|
||||||
|
|
||||||
user_comment_list_length = len(comments)
|
# Prepare metadata dictionary
|
||||||
user_comments = b''.join(comments)
|
metadata = {}
|
||||||
|
if not args.disable_metadata:
|
||||||
|
if prompt is not None:
|
||||||
|
metadata["prompt"] = json.dumps(prompt)
|
||||||
|
if extra_pnginfo is not None:
|
||||||
|
for x in extra_pnginfo:
|
||||||
|
metadata[x] = json.dumps(extra_pnginfo[x])
|
||||||
|
|
||||||
comment_data = struct.pack('<I', vendor_length) + vendor_string + struct.pack('<I', user_comment_list_length) + user_comments
|
# Opus supported sample rates
|
||||||
if last_block:
|
OPUS_RATES = [8000, 12000, 16000, 24000, 48000]
|
||||||
id = b'\x84'
|
|
||||||
else:
|
|
||||||
id = b'\x04'
|
|
||||||
comment_block = id + struct.pack('>I', len(comment_data))[1:] + comment_data
|
|
||||||
|
|
||||||
return comment_block
|
for (batch_number, waveform) in enumerate(audio["waveform"].cpu()):
|
||||||
|
filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
|
||||||
|
file = f"{filename_with_batch_num}_{counter:05}_.{format}"
|
||||||
|
output_path = os.path.join(full_output_folder, file)
|
||||||
|
|
||||||
def insert_or_replace_vorbis_comment(flac_io, comment_dict):
|
# Use original sample rate initially
|
||||||
if len(comment_dict) == 0:
|
sample_rate = audio["sample_rate"]
|
||||||
return flac_io
|
|
||||||
|
|
||||||
flac_io.seek(4)
|
# Handle Opus sample rate requirements
|
||||||
|
if format == "opus":
|
||||||
|
if sample_rate > 48000:
|
||||||
|
sample_rate = 48000
|
||||||
|
elif sample_rate not in OPUS_RATES:
|
||||||
|
# Find the next highest supported rate
|
||||||
|
for rate in sorted(OPUS_RATES):
|
||||||
|
if rate > sample_rate:
|
||||||
|
sample_rate = rate
|
||||||
|
break
|
||||||
|
if sample_rate not in OPUS_RATES: # Fallback if still not supported
|
||||||
|
sample_rate = 48000
|
||||||
|
|
||||||
blocks = []
|
# Resample if necessary
|
||||||
last_block = False
|
if sample_rate != audio["sample_rate"]:
|
||||||
|
waveform = torchaudio.functional.resample(waveform, audio["sample_rate"], sample_rate)
|
||||||
|
|
||||||
while not last_block:
|
# Create in-memory WAV buffer
|
||||||
header = flac_io.read(4)
|
wav_buffer = io.BytesIO()
|
||||||
last_block = (header[0] & 0x80) != 0
|
torchaudio.save(wav_buffer, waveform, sample_rate, format="WAV")
|
||||||
block_type = header[0] & 0x7F
|
wav_buffer.seek(0) # Rewind for reading
|
||||||
block_length = struct.unpack('>I', b'\x00' + header[1:])[0]
|
|
||||||
block_data = flac_io.read(block_length)
|
|
||||||
|
|
||||||
if block_type == 4 or block_type == 1:
|
# Use PyAV to convert and add metadata
|
||||||
pass
|
input_container = av.open(wav_buffer)
|
||||||
else:
|
|
||||||
header = bytes([(header[0] & (~0x80))]) + header[1:]
|
|
||||||
blocks.append(header + block_data)
|
|
||||||
|
|
||||||
blocks.append(create_vorbis_comment_block(comment_dict, last_block=True))
|
# Create output with specified format
|
||||||
|
output_buffer = io.BytesIO()
|
||||||
|
output_container = av.open(output_buffer, mode='w', format=format)
|
||||||
|
|
||||||
new_flac_io = io.BytesIO()
|
# Set metadata on the container
|
||||||
new_flac_io.write(b'fLaC')
|
for key, value in metadata.items():
|
||||||
for block in blocks:
|
output_container.metadata[key] = value
|
||||||
new_flac_io.write(block)
|
|
||||||
|
|
||||||
new_flac_io.write(flac_io.read())
|
# Set up the output stream with appropriate properties
|
||||||
return new_flac_io
|
input_container.streams.audio[0]
|
||||||
|
if format == "opus":
|
||||||
|
out_stream = output_container.add_stream("libopus", rate=sample_rate)
|
||||||
|
if quality == "64k":
|
||||||
|
out_stream.bit_rate = 64000
|
||||||
|
elif quality == "96k":
|
||||||
|
out_stream.bit_rate = 96000
|
||||||
|
elif quality == "128k":
|
||||||
|
out_stream.bit_rate = 128000
|
||||||
|
elif quality == "192k":
|
||||||
|
out_stream.bit_rate = 192000
|
||||||
|
elif quality == "320k":
|
||||||
|
out_stream.bit_rate = 320000
|
||||||
|
elif format == "mp3":
|
||||||
|
out_stream = output_container.add_stream("libmp3lame", rate=sample_rate)
|
||||||
|
if quality == "V0":
|
||||||
|
#TODO i would really love to support V3 and V5 but there doesn't seem to be a way to set the qscale level, the property below is a bool
|
||||||
|
out_stream.codec_context.qscale = 1
|
||||||
|
elif quality == "128k":
|
||||||
|
out_stream.bit_rate = 128000
|
||||||
|
elif quality == "320k":
|
||||||
|
out_stream.bit_rate = 320000
|
||||||
|
else: #format == "flac":
|
||||||
|
out_stream = output_container.add_stream("flac", rate=sample_rate)
|
||||||
|
|
||||||
|
|
||||||
|
# Copy frames from input to output
|
||||||
|
for frame in input_container.decode(audio=0):
|
||||||
|
frame.pts = None # Let PyAV handle timestamps
|
||||||
|
output_container.mux(out_stream.encode(frame))
|
||||||
|
|
||||||
|
# Flush encoder
|
||||||
|
output_container.mux(out_stream.encode(None))
|
||||||
|
|
||||||
|
# Close containers
|
||||||
|
output_container.close()
|
||||||
|
input_container.close()
|
||||||
|
|
||||||
|
# Write the output to file
|
||||||
|
output_buffer.seek(0)
|
||||||
|
with open(output_path, 'wb') as f:
|
||||||
|
f.write(output_buffer.getbuffer())
|
||||||
|
|
||||||
|
results.append({
|
||||||
|
"filename": file,
|
||||||
|
"subfolder": subfolder,
|
||||||
|
"type": self.type
|
||||||
|
})
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
return { "ui": { "audio": results } }
|
||||||
|
|
||||||
class SaveAudio:
|
class SaveAudio:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.output_dir = folder_paths.get_output_directory()
|
self.output_dir = folder_paths.get_output_directory()
|
||||||
@ -153,50 +211,70 @@ class SaveAudio:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def INPUT_TYPES(s):
|
def INPUT_TYPES(s):
|
||||||
return {"required": { "audio": ("AUDIO", ),
|
return {"required": { "audio": ("AUDIO", ),
|
||||||
"filename_prefix": ("STRING", {"default": "audio/ComfyUI"})},
|
"filename_prefix": ("STRING", {"default": "audio/ComfyUI"}),
|
||||||
|
},
|
||||||
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_TYPES = ()
|
RETURN_TYPES = ()
|
||||||
FUNCTION = "save_audio"
|
FUNCTION = "save_flac"
|
||||||
|
|
||||||
OUTPUT_NODE = True
|
OUTPUT_NODE = True
|
||||||
|
|
||||||
CATEGORY = "audio"
|
CATEGORY = "audio"
|
||||||
|
|
||||||
def save_audio(self, audio, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=None):
|
def save_flac(self, audio, filename_prefix="ComfyUI", format="flac", prompt=None, extra_pnginfo=None):
|
||||||
filename_prefix += self.prefix_append
|
return save_audio(self, audio, filename_prefix, format, prompt, extra_pnginfo)
|
||||||
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
|
|
||||||
results: list[FileLocator] = []
|
|
||||||
|
|
||||||
metadata = {}
|
class SaveAudioMP3:
|
||||||
if not args.disable_metadata:
|
def __init__(self):
|
||||||
if prompt is not None:
|
self.output_dir = folder_paths.get_output_directory()
|
||||||
metadata["prompt"] = json.dumps(prompt)
|
self.type = "output"
|
||||||
if extra_pnginfo is not None:
|
self.prefix_append = ""
|
||||||
for x in extra_pnginfo:
|
|
||||||
metadata[x] = json.dumps(extra_pnginfo[x])
|
|
||||||
|
|
||||||
for (batch_number, waveform) in enumerate(audio["waveform"].cpu()):
|
@classmethod
|
||||||
filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
|
def INPUT_TYPES(s):
|
||||||
file = f"{filename_with_batch_num}_{counter:05}_.flac"
|
return {"required": { "audio": ("AUDIO", ),
|
||||||
|
"filename_prefix": ("STRING", {"default": "audio/ComfyUI"}),
|
||||||
|
"quality": (["V0", "128k", "320k"], {"default": "V0"}),
|
||||||
|
},
|
||||||
|
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
||||||
|
}
|
||||||
|
|
||||||
buff = io.BytesIO()
|
RETURN_TYPES = ()
|
||||||
torchaudio.save(buff, waveform, audio["sample_rate"], format="FLAC")
|
FUNCTION = "save_mp3"
|
||||||
|
|
||||||
buff = insert_or_replace_vorbis_comment(buff, metadata)
|
OUTPUT_NODE = True
|
||||||
|
|
||||||
with open(os.path.join(full_output_folder, file), 'wb') as f:
|
CATEGORY = "audio"
|
||||||
f.write(buff.getbuffer())
|
|
||||||
|
|
||||||
results.append({
|
def save_mp3(self, audio, filename_prefix="ComfyUI", format="mp3", prompt=None, extra_pnginfo=None, quality="128k"):
|
||||||
"filename": file,
|
return save_audio(self, audio, filename_prefix, format, prompt, extra_pnginfo, quality)
|
||||||
"subfolder": subfolder,
|
|
||||||
"type": self.type
|
|
||||||
})
|
|
||||||
counter += 1
|
|
||||||
|
|
||||||
return { "ui": { "audio": results } }
|
class SaveAudioOpus:
|
||||||
|
def __init__(self):
|
||||||
|
self.output_dir = folder_paths.get_output_directory()
|
||||||
|
self.type = "output"
|
||||||
|
self.prefix_append = ""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def INPUT_TYPES(s):
|
||||||
|
return {"required": { "audio": ("AUDIO", ),
|
||||||
|
"filename_prefix": ("STRING", {"default": "audio/ComfyUI"}),
|
||||||
|
"quality": (["64k", "96k", "128k", "192k", "320k"], {"default": "128k"}),
|
||||||
|
},
|
||||||
|
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
||||||
|
}
|
||||||
|
|
||||||
|
RETURN_TYPES = ()
|
||||||
|
FUNCTION = "save_opus"
|
||||||
|
|
||||||
|
OUTPUT_NODE = True
|
||||||
|
|
||||||
|
CATEGORY = "audio"
|
||||||
|
|
||||||
|
def save_opus(self, audio, filename_prefix="ComfyUI", format="opus", prompt=None, extra_pnginfo=None, quality="V3"):
|
||||||
|
return save_audio(self, audio, filename_prefix, format, prompt, extra_pnginfo, quality)
|
||||||
|
|
||||||
class PreviewAudio(SaveAudio):
|
class PreviewAudio(SaveAudio):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -248,7 +326,20 @@ NODE_CLASS_MAPPINGS = {
|
|||||||
"VAEEncodeAudio": VAEEncodeAudio,
|
"VAEEncodeAudio": VAEEncodeAudio,
|
||||||
"VAEDecodeAudio": VAEDecodeAudio,
|
"VAEDecodeAudio": VAEDecodeAudio,
|
||||||
"SaveAudio": SaveAudio,
|
"SaveAudio": SaveAudio,
|
||||||
|
"SaveAudioMP3": SaveAudioMP3,
|
||||||
|
"SaveAudioOpus": SaveAudioOpus,
|
||||||
"LoadAudio": LoadAudio,
|
"LoadAudio": LoadAudio,
|
||||||
"PreviewAudio": PreviewAudio,
|
"PreviewAudio": PreviewAudio,
|
||||||
"ConditioningStableAudio": ConditioningStableAudio,
|
"ConditioningStableAudio": ConditioningStableAudio,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||||
|
"EmptyLatentAudio": "Empty Latent Audio",
|
||||||
|
"VAEEncodeAudio": "VAE Encode Audio",
|
||||||
|
"VAEDecodeAudio": "VAE Decode Audio",
|
||||||
|
"PreviewAudio": "Preview Audio",
|
||||||
|
"LoadAudio": "Load Audio",
|
||||||
|
"SaveAudio": "Save Audio (FLAC)",
|
||||||
|
"SaveAudioMP3": "Save Audio (MP3)",
|
||||||
|
"SaveAudioOpus": "Save Audio (Opus)",
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user