SaveAudioMP3: avcodec_open2(libmp3lame) EINVAL — three bugs in _ui.py
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
### Custom Node Testing
- [x] I have tried disabling custom nodes and the issue persists (see [how to disable custom nodes](https://docs.comfy.org/troubleshooting/custom-node-issues#step-1%3A-test-with-all-custom-nodes-disabled) if you need help)
### Expected Behavior
Possibly related to #11495 and #13495 but with full root cause analysis and fixes.
`SaveAudioMP3` node should successfully encode and save audio in MP3 format at all supported sample rates and quality settings including V0.
### Actual Behavior
`SaveAudioMP3` fails with:
av.error.ValueError: [Errno 22] Invalid argument: 'avcodec_open2(libmp3lame)'
for any audio input, regardless of quality setting. With V0 quality selected the error always occurs. With audio sources outputting at 40000Hz (e.g. certain RVC models) the error occurs even if other bugs are fixed.
### Steps to Reproduce
1. Connect any audio source to a `SaveAudioMP3` node
2. Set quality to V0
3. Run the workflow
4. Error occurs at the encode step
For the 40kHz issue specifically: use an RVC voice conversion model that outputs at 40000Hz sample rate (e.g. models trained with non-standard sample rates).
---
**Root Cause Analysis**
Three separate bugs identified in `D:\ComfyUI\resources\ComfyUI\comfy_api\latest\_ui.py`:
**Bug 1 — Invalid `codec_context.format` assignment (line ~337)**
```python
# BUGGY:
out_stream.codec_context.format = "fltp"
Setting `codec_context.format` before the codec is opened causes `avcodec_open2` to fail with `EINVAL`. The `AudioResampler` already converts frames to `fltp` before encoding, making this line both redundant and fatal.
**Fix:** Remove the line entirely.
**Bug 2 — `codec_context.qscale` treated as integer but is a bool (line ~341)**
```python
# BUGGY:
out_stream.codec_context.qscale = 1
The code's own comment acknowledges this is a boolean property in this version of PyAV, not an integer quality scale. Setting it to `1` causes `avcodec_open2` to fail with `EINVAL`.
**Fix:** Replace with `pass` — libmp3lame's default VBR is equivalent to V0 quality anyway.
**Bug 3 — 40000Hz sample rate rejected by libmp3lame (line ~336)**
`libmp3lame` only accepts these sample rates: `8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000`. Audio sources that output at 40000Hz (confirmed with RVC models) cause `avcodec_open2` to fail with `EINVAL`. There is no guard or resample step before stream creation.
**Fix:** Add sample rate remapping before `add_stream`:
```python
elif format == "mp3":
_valid_mp3_rates = [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]
if sample_rate not in _valid_mp3_rates:
_new_rate = min(_valid_mp3_rates, key=lambda r: abs(r - sample_rate))
waveform = torchaudio.functional.resample(waveform, sample_rate, _new_rate)
sample_rate = _new_rate
out_stream = output_container.add_stream("libmp3lame", rate=sample_rate, layout=layout)
if quality == "V0":
pass # libmp3lame default VBR ≈ V0
elif quality == "128k":
out_stream.bit_rate = 128000
elif quality == "320k":
out_stream.bit_rate = 320000
### Debug Logs
```powershell
av.error.ValueError: [Errno 22] Invalid argument: 'avcodec_open2(libmp3lame)'
File "comfy_api/latest/_ui.py", line 358, in save_audio
output_container.mux(out_stream.encode(resampled))
File "av/codec/context.pyx", line 220, in av.codec.context.CodecContext.open
err_check(lib.avcodec_open2(self.ptr, self.codec.ptr, &options.ptr), ...)
av.error.ValueError: [Errno 22] Invalid argument: 'avcodec_open2(libmp3lame)'
```
### Other
**System Information**
- **ComfyUI Version:** 0.21.1
- **OS:** Windows 11
- **Python Version:** 3.12.11
- **PyTorch Version:** 2.11.0+cu128
- **PyAV Version:** 16.1.0
- **GPU:** NVIDIA GeForce RTX 2070 SUPER (8GB)
All three bugs were confirmed by direct inspection of the running bytecode via `inspect.getsource()` and isolated reproduction of the 40kHz failure with a minimal PyAV test script. The 40kHz issue is triggered specifically by RVC voice conversion models with non-standard output sample rates and is unlikely to be caught by standard test cases using typical 44100/48000Hz audio sources.
Contributor guide
Assessment
This issue has not been assessed yet.