Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll
Last active April 18, 2018 15:23
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 chrisfcarroll/1a94850260af2c8778d391b2cd34f168 to your computer and use it in GitHub Desktop.
Save chrisfcarroll/1a94850260af2c8778d391b2cd34f168 to your computer and use it in GitHub Desktop.
MacOs command line script : use VLC to transcode to mp3
#! /usr/bin/env bash
outputdirectory="`dirname ~/Music`/Music/VLCout"
albumname=""
acodec="mpga"
mux="mpeg1"
ext=".mp3"
directoryAsAlbumName=""
foldDirectoryNames=""
function showhelp {
echo "Batch convert to mp3 or other codec using VLC.
Usage:
vlc [-a AlbumNamePrefix] [-d | -D] [-o OutputDirectory] [-m mux] [e .extension] inputfile ...
OutputDirectory defaults to $outputdirectory/
If -a, -d or -D are specified, then output will go to $outputdirectory/<AlbumName>/
Where AlbumName will be :
if -a is specified without -d or -D, then \"AlbumNamePrefix\"
if -d is specified then \"AlbumNamePrefix inputfileParentFolderName\"
if -D is specified then \"AlbumNamePrefix inputfileRelativePathWithSlashesReplacedBySpaces\"
mux defaults to mpeg1
ext defaults to mp3
e.g.
vlc -a "MP3" -d -o ~ "Prefab Sprout/Steve McQueen/*.m*"
will transcode to mp3 files in to the directory "~/MP3 Prefab Sprout Steve McQueen/"
"
}
if [ -z "$*" ]; then showhelp ; exit 0 ; fi
# --------------- Parse options -----------------
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "h?a:dDo:m:e:" opt; do
case "$opt" in
h|\?)
showhelp
exit 0
;;
a) albumname=$OPTARG
;;
d) directoryAsAlbumName=Y
;;
D) directoryAsAlbumName=Y
foldDirectoryNames=Y
;;
o) outputdirectory=$OPTARG
;;
e) ext=$OPTARG
didSpecifyExt=Y
;;
m)
mux=$OPTARG
if [ -z "$didSpecifyExt" ] ; then ext=".$OPTARG" ; fi
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [[ -z "$directoryAsAlbumName" && ! -z "$albumname" ]] ; then outputdirectory="$outputdirectory/$albumname" ; fi
if [[ ! -z "$directoryAsAlbumName" && ! -z "$albumname" ]] ; then albumname="$albumname " ; fi
# --------------- End Parse options -----------------
vlc="/Applications/VLC.app/Contents/MacOS/VLC"
if [ ! -e "$vlc" ]; then echo "Command '$vlc' does not exist" ; exit 1 ; fi
OIFS=$IFS
IFS=$'\n'
for file in "$@"; do
new="$(basename "$file" | sed -E 's@\.[0-9A-Za-z]+$@@' | sed -E "s@[:?/\\]@-@g" | sed -E "s@['\"]@@g" | sed -E 's@[ ]@\ @')$ext"
if [ ! -z "$directoryAsAlbumName" ] ; then
fromdir="$(dirname $file | sed -E 's@[:?\\]@-@g' | sed -E "s@['\"]@@g")"
if [ ! -z "$foldDirectoryNames" ] ; then
fromdir="$(echo "$fromdir" | sed -E 's@/@\ @g')"
fi
dst="$outputdirectory/$albumname$fromdir/$new"
mkdir -p "$outputdirectory/$albumname$fromdir"
else
dst="$outputdirectory/$new"
mkdir -p $outputdirectory
fi
echo "=> Transcoding '$file' to $dst ... "
$vlc -v -I dummy $file \
--sout "#transcode{acodec=$acodec}:standard{mux=$mux,dst=$dst,access=file}" \
vlc://quit
ls -lh "$file" "$dst/$new"
echo
done
IFS=$OIFS
@chrisfcarroll
Copy link
Author

chrisfcarroll commented Apr 18, 2018

Instructions for non-techies

  1. Find a techie to help you.
  2. Download the raw file by pressing the Raw button and saving the file.
  • If using Safari, don't save a 'Web archive' save as source text.
  • If using Safari, don't save with a .txt on the end, save with just the name vlc
  1. Open a command line (see point 1)
mv ~/Download/vlc /usr/local/bin/
chmod a+x /usr/local/bin/vlc
vlc -?

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