-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaudio_shortener.py
62 lines (47 loc) · 1.99 KB
/
audio_shortener.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
57
58
59
60
61
62
import os
from scipy.io import wavfile
from tkinter import filedialog
from tkinter import *
def run_audiosplitter():
# Create the Tkinter root window
root = root_audiosplitter
root.withdraw()
# Ask the user to select the audio file directory using the file explorer
input_directory = filedialog.askdirectory(title="Select Audio File Directory", parent=root_audiosplitter)
# Check if a directory was selected
if input_directory:
# Iterate over the files in the directory
for filename in os.listdir(input_directory):
if filename.endswith(".wav"):
file_path = os.path.join(input_directory, filename)
split_audio_file(file_path)
else:
print("No directory selected.")
# Close the Tkinter root window
root.destroy()
def split_audio_file(file_path, segment_duration=10):
# Load the audio file
sample_rate, audio_data = wavfile.read(file_path)
# Calculate the number of segments
num_segments = int(len(audio_data) / (sample_rate * segment_duration))
remainder = len(audio_data) % (sample_rate * segment_duration)
if remainder > 0:
num_segments += 1
# Create the output directory for segments
output_dir = os.path.dirname(file_path)
base_filename = os.path.splitext(os.path.basename(file_path))[0]
# Split the audio file into segments
for i in range(num_segments):
start = i * sample_rate * segment_duration
end = min((i + 1) * sample_rate * segment_duration, len(audio_data))
segment = audio_data[start:end]
# Create the output file name
segment_filename = f"{base_filename}_{i+1}.wav"
segment_path = os.path.join(output_dir, segment_filename)
# Save the segment as a new WAV file
wavfile.write(segment_path, sample_rate, segment)
print(f"Segment {i+1}/{num_segments} saved: {segment_filename}")
os.remove(file_path)
root_audiosplitter = Tk()
root_audiosplitter.withdraw()
run_audiosplitter()