-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (60 loc) · 2.65 KB
/
main.py
File metadata and controls
75 lines (60 loc) · 2.65 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from pydub import AudioSegment, effects
def normalize_audio(audio_segment):
change_in_dBFS = -audio_segment.max_dBFS
return audio_segment.apply_gain(change_in_dBFS)
def add_fade_effects(audio_segment, fade_duration=1000):
return audio_segment.fade_in(fade_duration).fade_out(fade_duration)
def ensure_mono(audio_segment):
if audio_segment.channels > 1:
return audio_segment.set_channels(1)
return audio_segment
def set_sample_rate(audio_segment, sample_rate=44100):
return audio_segment.set_frame_rate(sample_rate)
def apply_compression(audio_segment):
return effects.compress_dynamic_range(audio_segment)
def remove_dc_offset(audio_segment):
return effects.normalize(audio_segment)
def set_bit_depth(audio_segment, bit_depth=16):
return audio_segment.set_sample_width(bit_depth // 8)
def process_audio_files(input_folder, output_folder, fade_duration=1000):
# Ensure output folder exists
os.makedirs(output_folder, exist_ok=True)
# Process each file in the input folder
for filename in os.listdir(input_folder):
if filename.endswith((".mp3", ".wav")):
filepath = os.path.join(input_folder, filename)
audio = AudioSegment.from_file(filepath)
# # Ensure audio is mono
# mono_audio = ensure_mono(audio)
#
# # Set sample rate
# audio_with_fixed_sample_rate = set_sample_rate(mono_audio)
#
# # Remove DC offset
# dc_corrected_audio = remove_dc_offset(audio_with_fixed_sample_rate)
#
# # Apply compression
# compressed_audio = apply_compression(dc_corrected_audio)
#
# # Normalize the audio
# normalized_audio = normalize_audio(compressed_audio)
#
# # Set bit depth
# bit_depth_audio = set_bit_depth(normalized_audio)
bit_depth_audio = audio
# Calculate start and end for 7 second clip around the middle
half_duration = len(bit_depth_audio) / 2
start = half_duration - 3500
end = half_duration + 3500
clipped_audio = bit_depth_audio[start:end]
# Apply fade effects
final_audio = add_fade_effects(clipped_audio, fade_duration)
# Export the processed audio
output_filepath = os.path.join(output_folder, filename)
final_audio.export(output_filepath, format="mp3")
# Define input and output folders
input_folder = "Sounds/Processed sounds/classic"
output_folder = "Sounds/Cut sounds 5 sec/classic"
# Process the audio files
process_audio_files(input_folder, output_folder)