dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

playlist-cp (1810B)


      1 #!/bin/sh
      2 
      3 # takes input filenames from stdin or file (-f), converts to a
      4 # reasonable format, or not (-o), and copies them to the same directory
      5 # structure prefixed with specified outdir
      6 # for ex: echo "a/b/foo.flac" | playlist-cp mus
      7 # produces mus/a/b/foo.ogg, it also checks a/b/ for any covers and cps them
      8 
      9 OGGENC="-q 8"
     10 prefix=
     11 
     12 usage() {
     13 	echo \
     14 "usage: playlist-cp [-f playlist] [-o] [-p prefix] [-s name] outdir
     15 	-f: read from playlist instead of stdin
     16 	-o: don't transcode
     17 	-p: prefix input lines
     18 	-s: write playist to outdir/playlists/name.m3u"
     19 
     20 	exit 1
     21 }
     22 
     23 while getopts "f:p:s:o" arg; do
     24 	case "${arg}" in
     25 	f) input="${OPTARG}" ;;
     26 	o) no_transcode=1 ;;
     27 	p) prefix="${OPTARG}" ;;
     28 	s) save_playlist="${OPTARG}" ;;
     29 	*) usage ;;
     30 	esac
     31 done
     32 shift $((OPTIND - 1))
     33 
     34 input="${input:-/dev/stdin}"
     35 outdir="$(realpath $1)"
     36 [ "$outdir" ] || usage
     37 [ "$prefix" ] && prefix="$(realpath $prefix)/"
     38 [ "$save_playlist" ] && tmplist=$(mktemp)
     39 
     40 while read -r line; do
     41 	filedir=$(dirname "$line")
     42 	filename=$(basename "$line")
     43 	odir="$outdir/$filedir"
     44 	oname="$filename"
     45 	[ -d "$odir" ] || mkdir -p "$odir"
     46 
     47 	# if cover doesn't exist in odir copy it over
     48 	[ -f "$odir"/[Cc]over.* ] || cp "${prefix}$filedir"/[Cc]over.* "$odir"
     49 
     50 	case "${line}" in
     51 	*.flac)
     52 		if [ $no_transcode ]; then
     53 			[ -f "$odir/$oname" ] || cp "${prefix}$line" "$odir/$oname"
     54 		else
     55 			oname="$(echo $filename | sed 's:flac:ogg:')"
     56 			[ -f "$odir/$oname" ] ||
     57 			oggenc $OGGENC -o "$odir/$oname" "${prefix}$line"
     58 		fi
     59 		;;
     60 	*) [ -f "$odir/$oname" ] || cp "${prefix}$line" "$odir/$oname" ;;
     61 	esac
     62 
     63 	[ $save_playlist ] && echo "../$filedir/$oname" >> "$tmplist"
     64 done < $input
     65 
     66 if [ $save_playlist ]; then
     67 	[ -d "${outdir}/playlists" ] || mkdir "${outdir}/playlists"
     68 	sort "$tmplist" >| "${outdir}/playlists/${save_playlist}.m3u"
     69 	rm "$tmplist"
     70 fi