Skip to content

Instantly share code, notes, and snippets.

@tyleretters
Created April 22, 2024 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyleretters/d581ea0a3968437cd938d2b456a7ca67 to your computer and use it in GitHub Desktop.
Save tyleretters/d581ea0a3968437cd938d2b456a7ca67 to your computer and use it in GitHub Desktop.
#!/Users/tyleretters/Desktop/generateWaveform/myenv/bin/python
import os
import sys
from pydub import AudioSegment
import numpy as np
import matplotlib.pyplot as plt
def generate_waveform(audio_path, image_path, color, dimensions):
# Load the audio file
audio = AudioSegment.from_file(audio_path)
# Get stereo or mono data
if audio.channels == 2:
left = audio.split_to_mono()[0].get_array_of_samples()
right = audio.split_to_mono()[1].get_array_of_samples()
data = np.array([left, right])
else:
data = np.array(audio.get_array_of_samples())
# Normalize the data
data = data / np.max(np.abs(data))
# Prepare dimensions
width, height = map(int, dimensions.split('x'))
# Setup the plot
fig, ax = plt.subplots(figsize=(width / 100, height / 100))
ax.set_facecolor('black')
ax.fill_between(np.arange(len(data[0])), data[0], color=color)
if audio.channels == 2:
ax.fill_between(np.arange(len(data[1])), -data[1], color=color)
# Remove axes
ax.axis('off')
# Save the image
plt.savefig(image_path, dpi=100, bbox_inches='tight', pad_inches=0)
plt.close()
def main(audio_input_dir, image_output_dir, waveform_color, png_dimensions):
# Ensure output directory exists
if not os.path.exists(image_output_dir):
os.makedirs(image_output_dir)
# Process each file in the directory
for filename in os.listdir(audio_input_dir):
if filename.endswith('.wav') or filename.endswith('.mp3'):
audio_path = os.path.join(audio_input_dir, filename)
image_filename = filename.rsplit('.', 1)[0] + '.png'
image_path = os.path.join(image_output_dir, image_filename)
generate_waveform(audio_path, image_path, waveform_color, png_dimensions)
print(f"Generated waveform for {filename}")
if __name__ == "__main__":
if len(sys.argv) < 5:
print("Usage: python script.py <audio_input_dir> <image_output_dir> <waveform_color> <png_dimensions>")
else:
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment