scripts

unixus scriptus
git clone https://s.sonu.ch/~roket1428/scripts.git
Log | Files | Refs

fmc (3598B)


      1 #!/bin/sh
      2 
      3 # fmc - ffmpeg music converter
      4 
      5 # this tool converts multiple music files at once using ffmpeg
      6 
      7 # error handling
      8 die()
      9 {
     10 	printf "%s\\n" "$1" >&2
     11 	exit 1
     12 }
     13 
     14 
     15 # check input and change directory
     16 ckchdir() 
     17 {
     18 
     19 if [ -z "$1" ]
     20 then
     21         die "error: input is blank"
     22 else
     23         if [ -f "$1" ]; then
     24 		die "error: input must be a directory"
     25 	fi
     26 
     27 	cd "$1" || die "error: can't change directory, exiting"
     28 
     29 fi
     30 }
     31 
     32 
     33 # convert files from directory
     34 cvdir() 
     35 {
     36 
     37 list=$(find . -name "*.$inputext" -maxdepth 1 | wc -l)
     38 line=1
     39 
     40 while [ $line -le "$list" ] 
     41 do
     42 	
     43 	ffmpeg -hide_banner -loglevel error -i \
     44 		"$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e 's/\.\///')" \
     45 		"$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")"
     46 
     47 	printf "converting %s\\n" "$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e 's/\.\///')"
     48 
     49 	[ "$fillblanks" -eq "1" ] \
     50 		&& mv \
     51 		"$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \
     52 		"$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')"
     53 
     54 	line=$((line + 1))
     55 done
     56 
     57 rmline=1
     58 if [ "$removeold" -eq "1" ]; then
     59 	
     60 	while [ $rmline -le "$list" ]
     61 	do 		
     62 		printf "removing %s\\n" "$(find . -name "*.$inputext" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')"
     63 
     64 		rm "$(find . -name "*.$inputext" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')"
     65 		rmline=$((rmline + 1))
     66 	done
     67 fi
     68 
     69 }
     70 
     71 
     72 # convert files from specified path recursively
     73 cvrec() 
     74 {		
     75 
     76 list=$(find . -name "*.$inputext" | wc -l)
     77 line=1
     78 
     79 while [ $line -le "$list" ] 
     80 do
     81 	
     82 	ffmpeg -hide_banner -loglevel error -i \
     83 		"$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e 's/\.\///')" \
     84 		"$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")"
     85 
     86 	printf "converting %s\\n" "$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e 's/\.\///')"
     87 
     88 	if [ "$fillblanks" -eq "1" ]
     89 	then
     90 		cd "$(find . -name "*.$outputext" | sed -e 's/\.\///;s#[^/]*$##g' | head -$line | tail +$line)" || exit 1
     91 
     92 		mv "$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \
     93 			"$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')"
     94 
     95 		cd "$rootdir" || die "error: can't change directory, exiting"
     96 	fi
     97 	line=$((line + 1))
     98 done
     99 
    100 rmline=1
    101 if [ "$removeold" -eq "1" ]; then
    102 	
    103 	while [ $rmline -le "$list" ]
    104 	do 		
    105 		printf "removing %s\\n" "$(find . -name "*.$inputext" | head -1 | tail +1 | sed -e 's/\.\///')"
    106 
    107 		rm "$(find . -name "*.$inputext" | head -1 | tail +1 | sed -e 's/\.\///')"
    108 		rmline=$((rmline + 1))
    109 	done
    110 fi
    111 }
    112 
    113 
    114 usage() 
    115 {
    116 cat <<EOF
    117 Usage: $0 -rb [ -i FILETYPE ] [ -o FILETYPE ] [ -R PATH ] -h PATH
    118 
    119 	-r	remove old unconverted files
    120 
    121 	-b	replace blanks(spaces) in filenames with underscore
    122 
    123 	-i	file extension to convert from ex.(mp3, m4a)
    124 
    125 	-o	file extension to convert to ex.(flac, ogg)
    126 
    127 	-R	convert files from specified directory recursively
    128 
    129 	-h	display this text
    130 EOF
    131 exit
    132 
    133 }
    134 
    135 
    136 unset fillblanks inputext outputext recursive rcdir removeold
    137 
    138 while getopts 'rbi:o:R:h' opt
    139 do
    140 	case $opt in
    141 	r) removeold=1 ;;
    142 	b) fillblanks=1 ;;
    143 	i) inputext=$OPTARG ;;
    144 	o) outputext=$OPTARG ;;
    145 	R) recursive=1; rcdir=$OPTARG;;
    146 	h|*) usage ;;
    147 	esac
    148 done
    149 
    150 
    151 if [ "$recursive" -eq "1" ]
    152 then
    153 
    154 	ckchdir "$rcdir"
    155 	rootdir="$(pwd)"
    156 	cvrec "$rcdir"
    157 else	
    158 	shift $((OPTIND-1))
    159 	ckchdir "$@"
    160 	cvdir "$@"
    161 fi
    162 
    163