-
Notifications
You must be signed in to change notification settings - Fork 0
/
quarter.py
56 lines (38 loc) · 1.84 KB
/
quarter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from pydub import AudioSegment
import math
import os
# Had to make sure ffmpeg package was added and also had to
# put 'pkgs.ffmpeg' into the hidden replit.nix file dependencies
'''
This sprogram currently splits the target audio into quarters as mp3 files, lowers the bitrate to 64kbps, and saves them into the segmented folder.
'''
def split_audio(input_file, output_folder):
audio = AudioSegment.from_file(input_file)
# make sure directory exists for storing audio segments
if not os.path.exists(output_folder):
os.makedirs(output_folder)
#reduce the bitrate and compression to reduce file size
split_length = len(audio) // 4
# Split the audio into 4 parts
first_quarter = audio[:split_length]
second_quarter = audio[split_length:split_length*2]
third_quarter = audio[split_length*2:split_length*3]
fourth_quarter = audio[split_length*3:split_length*4]
# Save the first quarter
first_quarter_filename = os.path.join(output_folder, "1-segment.mp3")
first_quarter.export(first_quarter_filename, format="mp3", bitrate="64k")
# Save the second quarter
second_quarter_filename = os.path.join(output_folder, "2-segment.mp3")
second_quarter.export(second_quarter_filename, format="mp3", bitrate="64k")
# Save the third quarter
third_quarter_filename = os.path.join(output_folder, "3-segment.mp3")
third_quarter.export(third_quarter_filename, format="mp3", bitrate="64k")
# Save the fourth quarter
fourth_quarter_filename = os.path.join(output_folder, "4-segment.mp3")
fourth_quarter.export(fourth_quarter_filename, format="mp3", bitrate="64k")
if __name__ == "__main__":
input_file_path = "audio-file-to-segment.mp3" #input the mp3 file to segment
output_folder_path = "segmented"
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
split_audio(input_file_path, output_folder_path)