Skip to content

Commit 0e4fc14

Browse files
committed
add joining audio files tutorial
1 parent 04e8d51 commit 0e4fc14

8 files changed

+96
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9797
- [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python). ([code](general/video-to-audio-converter))
9898
- [How to Combine a Static Image with Audio in Python](https://www.thepythoncode.com/article/add-static-image-to-audio-in-python). ([code](python-for-multimedia/add-photo-to-audio))
9999
- [How to Concatenate Video Files in Python](https://www.thepythoncode.com/article/concatenate-video-files-in-python). ([code](python-for-multimedia/combine-video))
100+
- [How to Concatenate Audio Files in Python](https://www.thepythoncode.com/article/concatenate-audio-files-in-python). ([code](python-for-multimedia/combine-audio))
100101

101102

102103
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)

Diff for: python-for-multimedia/combine-audio/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Concatenate Audio Files in Python](https://www.thepythoncode.com/article/concatenate-audio-files-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- There are 3 files, one for each method, check [the tutorial](https://www.thepythoncode.com/article/concatenate-audio-files-in-python) for more info.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from moviepy.editor import concatenate_audioclips, AudioFileClip
2+
3+
4+
def concatenate_audio_moviepy(audio_clip_paths, output_path):
5+
"""Concatenates several audio files into one audio file using MoviePy
6+
and save it to `output_path`. Note that extension (mp3, etc.) must be added to `output_path`"""
7+
clips = [AudioFileClip(c) for c in audio_clip_paths]
8+
final_clip = concatenate_audioclips(clips)
9+
final_clip.write_audiofile(output_path)
10+
11+
12+
if __name__ == "__main__":
13+
import argparse
14+
parser = argparse.ArgumentParser(description="Simple Audio file combiner using MoviePy library in Python")
15+
parser.add_argument("-c", "--clips", nargs="+",
16+
help="List of audio clip paths")
17+
parser.add_argument("-o", "--output", help="The output audio file, extension must be included (such as mp3, etc.)")
18+
args = parser.parse_args()
19+
concatenate_audio_moviepy(args.clips, args.output)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pydub import AudioSegment
2+
from tqdm import tqdm
3+
import os
4+
5+
6+
def concatenate_audio_pydub(audio_clip_paths, output_path, verbose=1):
7+
"""
8+
Concatenates two or more audio files into one audio file using PyDub library
9+
and save it to `output_path`. A lot of extensions are supported, more on PyDub's doc.
10+
"""
11+
def get_file_extension(filename):
12+
"""A helper function to get a file's extension"""
13+
return os.path.splitext(filename)[1].lstrip(".")
14+
15+
clips = []
16+
# wrap the audio clip paths with tqdm if verbose
17+
audio_clip_paths = tqdm(audio_clip_paths, "Reading audio file") if verbose else audio_clip_paths
18+
for clip_path in audio_clip_paths:
19+
# get extension of the audio file
20+
extension = get_file_extension(clip_path)
21+
# load the audio clip and append it to our list
22+
clip = AudioSegment.from_file(clip_path, extension)
23+
clips.append(clip)
24+
25+
final_clip = clips[0]
26+
range_loop = tqdm(list(range(1, len(clips))), "Concatenating audio") if verbose else range(1, len(clips))
27+
for i in range_loop:
28+
# looping on all audio files and concatenating them together
29+
# ofc order is important
30+
final_clip = final_clip + clips[i]
31+
# export the final clip
32+
final_clip_extension = get_file_extension(output_path)
33+
if verbose:
34+
print(f"Exporting resulting audio file to {output_path}")
35+
final_clip.export(output_path, format=final_clip_extension)
36+
37+
38+
if __name__ == "__main__":
39+
import argparse
40+
parser = argparse.ArgumentParser(description="Simple Audio file combiner using PyDub library in Python")
41+
parser.add_argument("-c", "--clips", nargs="+",
42+
help="List of audio clip paths")
43+
parser.add_argument("-o", "--output", help="The output audio file, extension must be included (such as mp3, etc.)")
44+
args = parser.parse_args()
45+
concatenate_audio_pydub(args.clips, args.output)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import wave
2+
3+
def concatenate_audio_wave(audio_clip_paths, output_path):
4+
"""Concatenates several audio files into one audio file using Python's built-in wav module
5+
and save it to `output_path`. Note that extension (wav) must be added to `output_path`"""
6+
data = []
7+
for clip in audio_clip_paths:
8+
w = wave.open(clip, "rb")
9+
data.append([w.getparams(), w.readframes(w.getnframes())])
10+
w.close()
11+
output = wave.open(output_path, "wb")
12+
output.setparams(data[0][0])
13+
for i in range(len(data)):
14+
output.writeframes(data[i][1])
15+
output.close()
16+
17+
18+
if __name__ == "__main__":
19+
import argparse
20+
parser = argparse.ArgumentParser(description="Simple Audio file combiner using wave module in Python")
21+
parser.add_argument("-c", "--clips", nargs="+",
22+
help="List of audio clip paths")
23+
parser.add_argument("-o", "--output", help="The output audio file, extension (wav) must be included")
24+
args = parser.parse_args()
25+
concatenate_audio_wave(args.clips, args.output)
667 KB
Binary file not shown.

Diff for: python-for-multimedia/combine-audio/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
moviepy
2+
pydub

Diff for: python-for-multimedia/combine-audio/zoo.mp3

149 KB
Binary file not shown.

0 commit comments

Comments
 (0)