-
-
Save chrisfcarroll/1a94850260af2c8778d391b2cd34f168 to your computer and use it in GitHub Desktop.
MacOs command line script : use VLC to transcode to mp3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions for non-techies
.txt
on the end, save with just the namevlc