Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Last active June 30, 2022 22:22
Show Gist options
  • Select an option

  • Save jeffehobbs/8284de4b2d1442f44ef6bfe43d25446b to your computer and use it in GitHub Desktop.

Select an option

Save jeffehobbs/8284de4b2d1442f44ef6bfe43d25446b to your computer and use it in GitHub Desktop.
waveform.py: make cool lookin' twitter-sharable video from .wav file
# waveform.py | jeffehobbs@gmail.com
# Process an .wav audio file, make an .mp4 waveform video suitable for TWTR
# usage: waveform.py audiofile.wav
import subprocess, random, argparse, os
# get input
parser = argparse.ArgumentParser(description='Process an .wav audio file, make an .mp4 waveform video.')
parser.add_argument('file', type=str, help='filename')
args = parser.parse_args()
filename = args.file
# convert wav to mp3
ffmpeg_command = "ffmpeg -i " + filename + " -vn -ar 44100 -ac 2 -b:a 192k /tmp/" + filename.replace(".wav",".mp3")
print(ffmpeg_command)
subprocess.call(ffmpeg_command, shell=True)
# make waveform video
print("\n...FFMPEG making waveform video...\n")
mode = "cline"
colors = "Random"
ffmpeg_command = "ffmpeg -hide_banner -loglevel warning -y -i /tmp/" + filename.replace(".wav",".mp3") + " -filter_complex '[0:a]showwaves=size=1280x720:mode=" + mode + ":colors=" + colors + ",format=yuv420p[v]' -map '[v]' -map 0:a -c:v libx264 -c:a copy /tmp/" + filename.replace(".wav","_waveform.mp4")
print(ffmpeg_command)
subprocess.call(ffmpeg_command, shell=True)
# add audio to waveform video
print("\n...FFMPEG adding audio to waveform video...\n")
ffmpeg_command = "ffmpeg -hide_banner -loglevel warning -y -i /tmp/" + filename.replace(".wav","_waveform.mp4") + " -i /tmp/" + filename.replace(".wav",".mp3") + " -c:v copy -c:a aac /tmp/" + filename.replace(".wav","_output_long.mp4")
print(ffmpeg_command)
subprocess.call(ffmpeg_command, shell=True)
# trim video to fit on TWTR
print("\n trimming video to 2min 20sec...\n")
ffmpeg_command = "ffmpeg -i /tmp/" + filename.replace(".wav","_output_long.mp4") + " -ss 00:00:00 -to 00:02:20 " + filename.replace(".wav","_output.mp4")
print(ffmpeg_command)
subprocess.call(ffmpeg_command, shell=True)
# clean up, you filthy animal
os.remove("/tmp/" + filename.replace(".wav",".mp3"))
os.remove("/tmp/" + filename.replace(".wav","_waveform.mp4"))
os.remove("/tmp/" + filename.replace(".wav","_output_long.mp4"))
# fin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment