pydub
pydub copied to clipboard
Audio quality reduction when concatenating multiple audio files
I wrote a small tool to combine short radio traffic recordings into longer, 30-minute segments for archival purposes. All files are being processes as m4a/aac files, and while it works I see a noticeable reduction in quality with the combined file compared to the original.
Steps to reproduce
Here is my code excerpt that handles concatenation and saving:
# If file already exists, open it
if os.path.exists(outputFullpath):
logging.debug(" File {} already exists, opening and appending any new audio".format(outputFullpath))
outputRec = AudioSegment.from_file(outputFullpath)
else:
# Create a blank file 30 minutes long
logging.info(" Starting file {}".format(outputFullpath))
outputRec = AudioSegment.silent(duration=30*60*1000)
# Flag for new audio
hasAudio = False
# Iterate through each file
for file in recFiles:
# Open file
logging.debug(" opening {}".format(file[1]))
try:
# open based on filetype
rec = None
if file[1].endswith('m4a'):
rec = AudioSegment.from_file(file[1], format='m4a')
elif file[1].endswith('wav'):
rec = AudioSegment.from_wav(file[1])
else:
logging.error("Invalid audio file {}, skipping".format(file[1]))
continue
# Get delta from start of file
delta = (file[0] - segment).total_seconds()*1000
logging.debug(" Offsetting file {} seconds from start".format(delta))
# Add file to output rec at offset
#outputRec = outputRec.overlay(rec, position=delta)
recBefore = outputRec[:delta]
recAfter = outputRec[delta + len(rec):]
outputRec = recBefore + rec + recAfter
# Flip flag
hasAudio = True
# Remove if enabled
except pydubex.CouldntDecodeError:
logging.error("Got error decoding file {}, skipping and not removing".format(file[1]))
continue
# Save
logging.debug(" Saving file {}".format(outputFullpath))
outputFile = outputRec.export(outputFullpath,
format="ipod",
bitrate="96k")
Expected behavior
Combined audio file should not lose any audio quality compared to input files. I've tried changing the aac bitrate to no effect.
Actual behavior
The combined audio file has a noticeable reduction in quality, almost as if the bit depth is being lowered.
Your System configuration
- Python version:
Python 3.9.13 (main, May 23 2022, 22:02:02) [GCC 7.5.0] on linux
- Pydub version:
0.25.1
- ffmpeg:
ffmpeg version 3.4.8-0ubuntu0.2 built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
Is there an audio file you can include to help us reproduce?
Attached is a zip with an example of a concatenated 30-minute file and the original recording of the first clip in the sequence for comparison. audio-quality-reduction.zip
Noticed the same. Tested with different FFMPEG versions and export params - always getting worse quality...
@jiaaro Kindly give us a hint on why this happening?
Issue still exists with my script as well.
If I had to guess, there's probably some kind of bit quantization going on when pydub reads in the audio samples. but I feel like the issue would be far more pronounced if it was that simple.