How to Join Two Wav Files Using Python

How to join two wav files using python?

You could use audiolab:

import audiolab, scipy
a, fs, enc = audiolab.wavread('file1.wav')
b, fs, enc = audiolab.wavread('file2.wav')
c = scipy.vstack((a,b))
audiolab.wavwrite(c, 'file3.wav', fs, enc)

How to join multiple .wav files with python

Use module like pydub

#!/usr/bin/env python
from pydub import AudioSegment
sound1 = AudioSegment.from_wav("filename01.wav")
sound2 = AudioSegment.from_wav("filename02.wav")
sound3 = AudioSegment.from_wav("filename03.wav")
combined_sounds = sound1 + sound2 + sound3
combined_sounds.export("joinedFile.wav", format="wav")

Combine audio files in Python

You can use the pydub module. It's one of the easiest ways to cut, edit, merge audio files using Python.

Here's an example of how to use it to combine audio files with volume control:

from pydub import AudioSegment
sound1 = AudioSegment.from_file("/path/to/sound.wav", format="wav")
sound2 = AudioSegment.from_file("/path/to/another_sound.wav", format="wav")

# sound1 6 dB louder
louder = sound1 + 6

# sound1, with sound2 appended (use louder instead of sound1 to append the louder version)
combined = sound1 + sound2

# simple export
file_handle = combined.export("/path/to/output.mp3", format="mp3")

To overlay sounds, try this:

from pydub import AudioSegment
sound1 = AudioSegment.from_file("1.wav", format="wav")
sound2 = AudioSegment.from_file("2.wav", format="wav")

# sound1 6 dB louder
louder = sound1 + 6

# Overlay sound2 over sound1 at position 0 (use louder instead of sound1 to use the louder version)
overlay = sound1.overlay(sound2, position=0)

# simple export
file_handle = overlay.export("output.mp3", format="mp3")

Full documentation here pydub API Documentation

Merging audio wav files together

You can try pydub.

Install

pip3 install pydub

Example

# pydub example
from pydub import AudioSegment
audio_one = AudioSegment.from_file("0006606.wav")
audio_two = AudioSegment.from_file("0007514.wav")
merged = audio_one + audio_two

Original Example: http://pydub.com/

pypi: https://pypi.org/project/pydub/

Concatenate .wav files with a loop

Here's how I would approach this using R and ffmpeg. I'm sure you can do the same type of looping with bash, but this seemed pretty straightforward:

combiner <- function(path, segments_per_file) {
## Get a list of the wav files
files <- list.files(path = path, pattern = ".wav", full.names = TRUE)
## Split the list of wav files according to the number of files you want to combine at a time
groups <- cumsum(seq_along(files) %% segments_per_file == 1)
file_list <- split(files, groups)
## Loop through the list and use the concat protocol for ffmpeg to combine the files
lapply(seq_along(file_list), function(x) {
a <- tempfile(fileext = ".txt")
writeLines(sprintf("file '%s'", file_list[[x]]), a)
system(sprintf('ffmpeg -f concat -safe 0 -i %s -c copy Group_%s.wav', a, x))
})
}

If you'd prefer to use sox, the loop's a little more straightforward:

combiner <- function(path, segments_per_file) {
files <- list.files(path = path, pattern = ".wav", full.names = TRUE)
groups <- cumsum(seq_along(files) %% segments_per_file == 1)
file_list <- split(files, groups)
lapply(seq_along(file_list), function(x) {
system(sprintf("sox %s Group_%s.wav", paste(file_list[[x]], collapse = " "), x))
})
}

In R, you would then run combiner(path_to_your_wav_files, 60) if you want to combine 60 files at a time.

Note that the combined files will be in the working directory that you run the script from (use getwd() to verify where that is).



Related Topics



Leave a reply



Submit