Skip to content

Instantly share code, notes, and snippets.

@peterkappus
Created March 7, 2020 22:11
Show Gist options
  • Save peterkappus/3cb58b694eb06b7b16972718c74f26b6 to your computer and use it in GitHub Desktop.
Save peterkappus/3cb58b694eb06b7b16972718c74f26b6 to your computer and use it in GitHub Desktop.
Record, compress, index, and upload audio to an S3 bucket

Peter's Musical Notebook

Record audio, compress to Flac & MP3 files, create an index.html with a link to each MP3 and synch the new index and the MP3s to an S3 bucket.

Pre-requisites

  • Ruby
  • Sox
  • docker

Setup

  1. Create an S3 bucket.

  2. Create a CNAME to point to it.

  3. Create a "secrets.env" file and add

  • your AWS key
  • AWS Secret
  • S3 Bucket name where you want to upload the files
  1. Copy the below into a script and run it. Read the comments to understand how it works.
  2. Record some audio. When the audio stops, the script will compress it, generate a new index, and upload the MP3 and the new index to the S3 bucket.
#load secrets into env 
source secrets.env

#record
# https://gist.github.com/peterkappus/c1b8dfc621077c42019d21cf6f9ab624
file=$(date +%Y-%m-%dT%H%M%S%Z) && rec $file.flac silence 1 0:01 0.00599% 1 0:03 0.00599% norm compand 0.3,1 -5,-5 && sox $file.flac $file.mp3

#move mp3 to public folder
mv $file.mp3 public/$file.mp3

#build index
ruby mkindex.rb

#sync 'public' folder to bucket
docker run -v "$(pwd)"/public:/data --env AWS_ACCESS_KEY_ID=$AWS_ID --env AWS_SECRET_ACCESS_KEY=$AWS_SECRET garland/aws-cli-docker aws s3 sync . s3://$AWS_S3_BUCKET_NAME --delete --size-only --acl=public-read --exclude=".git*"
links = Dir.glob("public/*.mp3").map {|f| basename = File.basename(f); %(<a href="#{basename}">#{basename}</a>\n)}.join("<br>")
IO.write "public/index.html", %(<html>#{links}</html>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment