Created
February 15, 2022 21:11
-
-
Save tg-bomze/12ee3c12c29ed84ffd23684e244b02d9 to your computer and use it in GitHub Desktop.
Get an array of audio file volumes at a given framerate
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
import numpy as np | |
from scipy.io import wavfile | |
fps = 24 | |
path_to_audio = 'path_to_audio.wav' | |
try: | |
rate, signal = wavfile.read(path_to_audio) | |
signal = np.mean(signal, axis=1) | |
except: | |
import moviepy.editor as mpy | |
audio_clip = mpy.AudioFileClip(path_to_audio) | |
audio_clip.write_audiofile('./temp_audio.wav', fps=44100, nbytes=2, codec='pcm_s16le') | |
rate, signal = wavfile.read('./temp_audio.wav') | |
signal = np.mean(signal, axis=1) | |
signal = np.abs(signal) | |
seed = signal.shape[0] | |
duration = signal.shape[0] / rate | |
frames = int(np.ceil(duration * fps)) | |
samples_per_frame = signal.shape[0] / frames | |
volume = np.zeros(frames, dtype=signal.dtype) | |
for frame in range(frames): | |
start = int(round(frame * samples_per_frame)) | |
stop = int(round((frame + 1) * samples_per_frame)) | |
volume[frame] = np.mean(signal[start:stop], axis=0) | |
volume /= max(volume) | |
print(volume) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment