scripts

Random scripts
git clone git://git.margiolis.net/scripts.git
Log | Files | Refs | README | LICENSE

ausplit (1388B)


      1 #!/bin/sh
      2 
      3 main()
      4 {
      5 	test ${#} -ne 2 && usage
      6 
      7 	local tfile="${2}"
      8 
      9 	auin="${1}"
     10 	total="$(wc -l < "${tfile}" | awk '{print $1}')"
     11 	ext="${1#*.}"
     12 
     13 	read -erp "Album/book title: " album
     14 	read -erp "Artist/author: " author
     15 	read -erp "Publication year: " year
     16 
     17 	albumdir="$(fmt_name "${album}")"
     18 	mkdir -p "${albumdir}" || exit 1
     19 
     20 	while read -r line; do
     21 		end="$(echo "${line}" | cut -d' ' -f1)"
     22 		test -n "${start}" && split_file
     23 		title="$(echo "${line}" | cut -d' ' -f2-)"
     24 		titlefmt="$(fmt_name "${title}")"
     25 		track="$((track + 1))"
     26 		start="${end}"
     27 	done < "${tfile}"
     28 
     29 	# The last track must be done outside the loop.
     30 	split_file
     31 }
     32 
     33 usage()
     34 {
     35 	echo "usage: ${0##*/} audio timecode_file" 1>&2
     36 	exit 1
     37 }
     38 
     39 fmt_name()
     40 {
     41 	echo "${1}" | iconv -cf UTF-8 -t ASCII//TRANSLIT |
     42 	tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' |
     43 	sed "s/-\+/-/g;s/\(^-\|-\$\)//g"
     44 }
     45 
     46 split_file()
     47 {
     48 	echo "From ${start} to ${end}; ${track} ${title}"
     49 	echo "Splitting \"${title}\"..."
     50 	ofile="${albumdir}/$(printf "%.2d" "${track}")-${titlefmt}.${ext}"
     51 	ffmpeg \
     52 		-nostdin -y -loglevel -8 \
     53 		-i "${auin}" \
     54 		-ss "${start}" \
     55 		`test ${track} -eq ${total} || echo "-to ${end}"` \
     56 		-vn -c copy \
     57 		-map 0 \
     58 		-metadata title="${title}" \
     59 		-metadata album="${album}" \
     60 		-metadata artist="${author}" \
     61 		-metadata track="${track}/${total}" \
     62 		-metadata date="${date}" \
     63 		"${ofile}"
     64 }
     65 
     66 main "${@}"