~voidraven/mp3me

b8c31bc9d9921adfd84abebcd58abcf34218830e — voidraven a month ago 847fb6c
Fixed shellcheck issues. File extensions are longer case sensitive. Version bump. Updated README and manpage.
3 files changed, 38 insertions(+), 24 deletions(-)

M README.md
M mp3me.1
M mp3me.sh
M README.md => README.md +1 -1
@@ 27,7 27,7 @@ In order to use this tool, you will need a few things installed:
### Usage
```bash
mp3me ./musicdir                   # convert music files
mp3me -j 8 ./musicdir              # convert music files, starting 8 jobs (i.e. threads)
mp3me -j 8 ./musicdir              # start 8 jobs, i.e. threads (default is half your available cores)
mp3me -b 192 ./musicdir            # convert music files @ 192kbps
mp3me -h                           # show this help message
mp3me -v                           # show the version info

M mp3me.1 => mp3me.1 +1 -1
@@ 14,7 14,7 @@ a portable shell script for converting directories of audio files to mp3.
.SH OPTIONS
.TP
.BR -j " " \fInum\fR
The number of jobs (i.e. threads) to start and process files with.
The number of jobs (i.e. threads) to start and process files with. The default is half of the cores available to the system.
.TP
.BR -b " " \fInum\fR
The bitrate you wish to encode at (i.e. 192kbps)

M mp3me.sh => mp3me.sh +36 -22
@@ 9,11 9,14 @@
#
# Dependencies: ffmpeg, gnu parallel
#
VERSION="0.3"
VERSION="0.3.1"

# default bitrate and jobs
BITRATE=320000
JOBS=4
# Portable way to get the number of cores available to the system (Linux, BSD, and MacOS)
CORES_AVAIL=$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu || echo 2)
# Default number of threads to use (half available cores)
JOBS=$(( CORES_AVAIL / 2 ))

SHIFT=0



@@ 24,8 27,11 @@ fun_is_music () {
    L_FILE="$1"
    # ignore directories
    if [ -f "$L_FILE" ] ; then
	L_FILE_EXT=$(echo "${L_FILE##*.}")
	if [ $L_FILE_EXT = "flac" ] || [ $L_FILE_EXT = "aac" ] || [ $L_FILE_EXT = "ogg" ] || [ $L_FILE_EXT = "wav" ] || [ $L_FILE_EXT = "m4a" ] ; then
	# get extension
	L_FILE_EXT="${L_FILE##*.}"
	# get lowercase version
	L_FILE_EXT=$(echo "$L_FILE_EXT" | tr '[:upper:]' '[:lower:]')
	if [ "$L_FILE_EXT" = "flac" ] || [ "$L_FILE_EXT" = "aac" ] || [ "$L_FILE_EXT" = "ogg" ] || [ "$L_FILE_EXT" = "wav" ] || [ "$L_FILE_EXT" = "m4a" ] ; then
	    unset L_FILE L_FILE_EXT
	    return 0    
	fi


@@ 39,40 45,47 @@ fun_is_music () {
# echoes out all the valid music files in a directory
fun_echo_files () {
    L_LWD=$(pwd)
    cd "$1"
    cd "$1" || fun_error "failed to enter directory: $1"
    for FILE in * ; do
	if fun_is_music "$FILE" ; then
	    echo "$FILE"
	fi
    done
    cd "$L_LWD"
    cd "$L_LWD" || fun_error "failed to enter directory: $1"
    unset L_LWD
}


# take user input as kbps and return bps
fun_get_bitrate () {
    if [ $1 -gt 0 ] && [ $1 -lt 321 ] ; then
	BITRATE=$(($1*1000))
	SHIFT=$(($SHIFT+2))
	return 0
    L_BR="$1"
    if [ "$L_BR" -gt 0 ] && [ "$L_BR" -lt 321 ] ; then
	BITRATE=$((L_BR*1000))
	SHIFT=$((SHIFT+2))
    else
	unset L_BR
	fun_error "bad bitrate value passed in"
    fi
    return 1
    unset L_BR
}


fun_check_jobs () {
    if [ $1 -gt 0 ] && [ $1 -lt 100 ] ; then
	JOBS=$1
	SHIFT=$(($SHIFT+2))
	return 0
    if [ "$1" -gt 0 ] && [ "$1" -lt 256 ] ; then
	JOBS="$1"
	SHIFT=$((SHIFT+2))
    else
	fun_error "bad number of jobs passed in (0 > j < 256)"
    fi
    return 1
}


# call this function to start the script
fun_run () {
    echo "mp3me - beginning conversion $1 with $JOBS jobs."
    # SC2086 - yes, we want to split on spaces since we are building a command line
    # SC1083 - yes, the '{}' char are literal parallel uses them
    # shellcheck disable=SC2086,SC1083
    fun_echo_files "$1" | parallel -j $JOBS ffmpeg -hide_banner -nostats -loglevel fatal -i "'$1'"/{} -b:a $BITRATE "'$1'"/{.}.mp3
}



@@ 89,7 102,7 @@ fun_print_info () {
    echo "mp3me - parallel mp3 converter script"
    fun_print_ver
    echo "license: bsd 3-clause"
    echo "author: lotus"
    echo "author: voidraven"
    fun_print_usage
    exit 0
}


@@ 114,7 127,8 @@ fun_print_ver () {

# print error message
fun_error () {
    echo "Please use one of the following flags:"
    echo "mp3me - error detected"
    echo "$1"
    fun_print_usage
    exit 1
}


@@ 122,17 136,17 @@ fun_error () {

while getopts 'b:j:hvi:' OPT ; do
	case "$OPT" in
	b)	fun_get_bitrate $OPTARG;;
	j)	fun_check_jobs $OPTARG;;
	b)	fun_get_bitrate "$OPTARG";;
	j)	fun_check_jobs "$OPTARG";;
	h)      fun_print_info;;
	v)      fun_print_ver && exit 0;;
	[?])	fun_error;;
	[?])	fun_error "invalid argument passed in by user";;
	esac
done

shift $SHIFT

DIR=$@
DIR=$*
if [ -d "$DIR" ] ; then
    fun_run "$DIR"
else