Last active
February 17, 2023 03:23
-
-
Save xyb/acb56a1033a4ee395efb5f2daadc8230 to your computer and use it in GitHub Desktop.
get audio volumes samples, assited by ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydub import AudioSegment | |
import numpy as np | |
def get_volumes(audio_path, format, chunks_number=100): | |
audio = AudioSegment.from_file(audio_path, format=format) | |
segment_length = len(audio) // chunks_number | |
volumes = [] | |
for i in range(chunks_number): | |
start_time = i * segment_length | |
end_time = (i + 1) * segment_length | |
segment = audio[start_time:end_time] | |
samples = np.array(segment.get_array_of_samples()) | |
volume = np.abs(samples).mean() | |
volumes.append(volume) | |
# Set the maximum mean as 100 | |
max_mean = max(volumes) | |
part_means = [int(100 * mean / max_mean) for mean in volumes] | |
return part_means | |
if __name__ == '__main__': | |
print(' '.join([str(i) for i in get_volumes("file.mp3", "mp3", 100)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: