Skip to content

Instantly share code, notes, and snippets.

@charliebritton
Last active May 3, 2019 21:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliebritton/034c80e2e0bb671f6cf070d79b4dd809 to your computer and use it in GitHub Desktop.
Save charliebritton/034c80e2e0bb671f6cf070d79b4dd809 to your computer and use it in GitHub Desktop.
Ad Processor V2 - Trims songs down to the perfect length for a 2 min advert and also updates the metadata. Requires ffmpeg & mp3info
#!/bin/zsh
# (c) Copyright 2018 Charlie Britton (chza.me). All rights reserved.
# PLEASE NOTE: You will need ffmpeg and mp3info for this script to work properly!
# On MacOS, install Homebrew and then run `brew install mp3info` and `brew install ffmpeg`
# This script has only been tested on MacOS, there is no guarantee that it will work on any other OS (especially Windows).
# Set the directory to work from, this should have the child folder ./original/ with all of your music.
WORKINGDIRECTORY="/Users/someone/process-songs"
# Don't process songs shorter than x seconds. Default set to a few more seconds than advert length.
SHORT=125
# Don't process songs longer than x seconds (default of 5 minutes).
# This is because it could be processing a compilation of songs.
LONG=300
# The actual length of the adverts you'll want.
# Default is 2 seconds over default SC advert length for fading with automation software.
LENGTH=122
# Standard streaming bitrate. Also SC won't run ad stitching on some bitrates. Best left at 128k.
BITRATE=128000
# Default title and artist the SC ad server recognises. Probably best left as is.
TITLE="Advert:"
ARTIST="Advert:"
# PLEASE DO NOT EDIT BELOW THIS LINE! ------------------------------------------
CA=$(tput setaf 5)
CB=$(tput setaf 3)
CC=$(tput setaf 242)
# cd to Music Folder
cd $WORKINGDIRECTORY
# Welcome Message
clear
printf $CA"\n %%$CB Welcome to Ad Processor V2\n$CA %% $CB(c) Copyright 2018 Charlie Britton (chza.me)\n\n$CA %%$CB Please set the directory of the folder to process by editing this script, and then place your original media in a subfolder /original/.\n$CA %%$CB If have done that, please$CA press enter to continue$CB, or$CA CTRL+C to stop and set the process folder$CB.\n"
read keypress
# Setup Working Directories
printf $CA" i$CB Creating Folders... "
mkdir long > /dev/null 2>&1
mkdir short > /dev/null 2>&1
printf $CA"Done!\n"
# Don't want to be processing anything longet than 5 mins...
printf $CA" i$CB Moving long albums/compilations (>=$LONG seconds)... "
find original/ -name "*mp3" |
while IFS= read -r f; do
songlength=$(mp3info -p "%S" "$f");
if [[ "$songlength" -ge $LONG ]]; then
mv $f long/;
fi;
done
printf $CA"Done!\n"
# Don't want to pad a song with silence either!
printf $CA" i$CB Moving short songs (<=$SHORT seconds)..."
find original/ -name "*mp3" |
while IFS= read -r f; do
songlength=$(mp3info -p "%S" "$f");
if [[ "$songlength" -le $SHORT ]]; then
mv $f short/;
fi;
done
printf $CA"Done!\n\n"
printf $CA" %%$CB I am just about to begin processing the songs to: \n $CA|\n $CA|-$CB Length (s): $CA$LENGTH\n $CA|-$CB Bitrate (bits/s): $CA$BITRATE\n $CA|-$CB Title: $CA$TITLE\n $CA\`-$CB Artist: $CA$ARTIST\n\n$CA %% PLEASE NOTE:$CB This will take a while to process, so please leave your computer running whilst this operation completes.$CA\n %%$CB Please$CA press enter to continue$CB, or$CA CTRL+C to stop$CB.\n"
read keypress
printf $CA" i$CB Clearing out old adverts... "
rm -rf adverts
mkdir adverts
printf $CA"Done!\n"
sleep 0.5
TOTAL=$(find original/*.mp3 | wc -l | tr -d '[:space:]')
COUNTER=0
printf $CA" i$CB Starting processing of $TOTAL songs$CA NOW!\n\n"
cd original
for i in *.mp3; do
((COUNTER++))
printf $CB" ($COUNTER/$TOTAL)$CC Processing $i... ";
ffmpeg -loglevel panic -i "$i" -acodec libmp3lame -ab $BITRATE -af 'afade=t=out:st=117:d=5' -metadata title=$TITLE -metadata artist=$ARTIST -write_id3v1 1 -t $LENGTH "../adverts/ADVERT_$i";
printf $CA"Done!\n"
done
printf $CA"\n i$CB Finished processing songs!\n\n$CA %%$CB Thanks for using Ad Processor V2.\n\n"
exit 0
@charliebritton
Copy link
Author

charliebritton commented May 3, 2019

If you need to pick some random files from a list of music in a directory, cd into it and then use the following command to save 100 random file paths to a text file:

find . | grep -e ".mp3$" | gsort -R | tail -100 > ~/Desktop/process-songs.txt

Then, I use regexr to wrap the filenames in quotes and replace the newlines with spaces. I then copy and paste that into a cp command cp <paste here> ~/Desktop/music/original after making the directories.

Once done, point the script to the ~/Desktop/music and voila!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment