Created
March 29, 2023 23:02
-
-
Save Shubhang/ca0489d939307413f945240ddf3deff4 to your computer and use it in GitHub Desktop.
Visualizing Audio with Python: Music Visualizer using NumPy, Matplotlib, MoviePy, and PyDub | GitHub Gist
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
# Author: Shubhang, 2023 | |
# Description: This Python script demonstrates how to visualize audio using NumPy, Matplotlib, and MoviePy. | |
# It reads an audio file in WAV format, converts the audio samples to a NumPy array, | |
# and creates a video animation from a plot of the audio samples. | |
# The resulting video file shows the amplitude of the audio samples over time. | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from moviepy.editor import VideoClip | |
from moviepy.video.io.bindings import mplfig_to_npimage | |
from pydub import AudioSegment | |
def process_audio(file): | |
audio = AudioSegment.from_wav(file) | |
samples = np.array(audio.get_array_of_samples()) | |
return samples, audio.frame_rate | |
def visualize_music(samples, frame_rate, duration, output_file): | |
fig, ax = plt.subplots() | |
def make_frame(t): | |
ax.clear() | |
ax.set_title("Music Visualizer", fontsize=16) | |
ax.set_xlabel("Time (s)") | |
ax.set_ylabel("Amplitude") | |
start = int(t * frame_rate) | |
end = int((t + 1) * frame_rate) | |
x = np.linspace(t, t + 1, end - start) | |
y = samples[start:end] | |
ax.plot(x, y, linewidth=0.5) | |
ax.set_xlim(t, t + 1) | |
ax.set_ylim(-2**15, 2**15 - 1) | |
return mplfig_to_npimage(fig) | |
animation = VideoClip(make_frame, duration=duration) | |
animation.write_videofile(output_file, fps=30) | |
if __name__ == "__main__": | |
input_file = "melody.wav" | |
output_file = "music_visualizer.mp4" | |
samples, frame_rate = process_audio(input_file) | |
duration = len(samples) / frame_rate | |
visualize_music(samples, frame_rate, duration, output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment