pydub导出MP3是否存在最大时长限制?超长导出报错求助
Hey there! I’ve run into this exact issue before with pydub and long audio files—let’s break down what’s happening and how to get those 200+ minute MP3s exported successfully.
Why This Happens
The OSError: [Errno 22] Invalid argument traceback pointing to wave_data.writeframesraw(self._data) gives it away: Pydub’s default export flow uses Python’s built-in wave module to create a temporary WAV file before converting it to MP3.
WAV files are uncompressed, so a 205-minute PCM WAV clocks in around the 4GB mark—this hits the 32-bit file size limit that the wave module can handle. Once you cross that threshold, the module throws the invalid argument error.
Solutions to Export Long MP3s
1. Skip the Temporary WAV: Use FFmpeg Directly with Pydub
Pydub can leverage FFmpeg directly to avoid creating that problematic intermediate WAV file. Here’s how:
First, make sure FFmpeg is installed on your system (most Linux distros have it in their package managers, or you can download it for Windows/macOS). Then update your export code:
from pydub import AudioSegment # Ensure pydub can find FFmpeg (set the path if it's not in your system PATH) AudioSegment.converter = "/usr/bin/ffmpeg" # Adjust this to your FFmpeg path # Export directly using FFmpeg, no temp WAV compilation_audio.export(mp3_path, format="mp3", parameters=["-codec:a", "libmp3lame", "-qscale:a", "2"])
- The
-qscale:a 2parameter sets MP3 quality (0 = highest, 9 = lowest)—2 is a great balance of quality and file size. - This bypasses the
wavemodule entirely, so you won’t hit that 4GB limit anymore.
2. Use FFmpeg Command Line (No Pydub Needed)
If you don’t need to use Pydub for post-processing, skip it entirely and use FFmpeg directly on your source audio file. This is the most reliable method for super long files:
ffmpeg -i your_source_audio.wav -codec:a libmp3lame -qscale:a 2 your_long_output.mp3
Replace your_source_audio.wav with your actual input file (FFmpeg supports most formats like FLAC, MP4, etc.) and adjust the quality parameter as needed.
3. Fallback: Split and Merge the Audio
If for some reason you can’t use FFmpeg directly, split the long audio into smaller chunks, export each, then merge them with FFmpeg:
from pydub import AudioSegment import subprocess import os # Split audio into 200-minute segments (safe under the 205-minute limit) segment_duration = 200 * 60 * 1000 # Pydub uses milliseconds audio_segments = [ compilation_audio[i:i+segment_duration] for i in range(0, len(compilation_audio), segment_duration) ] # Export each segment to temporary MP3s temp_files = [] for idx, seg in enumerate(audio_segments): temp_path = f"temp_segment_{idx}.mp3" seg.export(temp_path, format="mp3") temp_files.append(temp_path) # Create a list file for FFmpeg to merge with open("merge_list.txt", "w") as f: for file in temp_files: f.write(f"file '{file}'\n") # Merge segments into one MP3 (no re-encoding, fast!) subprocess.run([ "ffmpeg", "-f", "concat", "-safe", "0", "-i", "merge_list.txt", "-c", "copy", "final_long.mp3" ]) # Clean up temporary files for file in temp_files + ["merge_list.txt"]: os.remove(file)
This method works because each segment is under the wave module’s limit, and merging with -c copy doesn’t re-encode the audio (so no quality loss).
Quick Notes
- Always use a recent version of FFmpeg—older versions might have their own length restrictions, but modern builds handle arbitrarily long files.
- Check your disk space: Even with compression, long MP3s can take up several gigabytes of space.
内容的提问来源于stack exchange,提问作者Yulia V

