dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

ffmpeg-split (1354B)


      1 #!/bin/sh
      2 
      3 usage() {
      4 	printf 'usage: %s [-aA] TIMESTAMPSFILE MEDIAFILE\n' "${0##*/}" >&2
      5 	printf 'OPTIONS:\n'
      6 	printf '  -a ARTIST\n'
      7 	printf '  -A ALBUM\n'
      8 	exit 1
      9 }
     10 
     11 die() {
     12 	printf 'error: %s\n' >&2
     13 	exit 1
     14 }
     15 
     16 artist=""
     17 album=""
     18 while :; do
     19 	case "$1" in
     20 		-a) artist="$2"; shift;;
     21 		-A) album="$2"; shift;;
     22 		*) break
     23 	esac
     24 	shift
     25 done
     26 
     27 if [ $# -ne 2 ]; then
     28 	usage
     29 fi
     30 
     31 if [ "$(file -ib "$1")" != "text/plain" ]; then
     32 	usage
     33 fi
     34 
     35 if [ ! -e "$1" ] || [ ! -e "$2" ]; then
     36 	die "could not read $1 or $2"
     37 fi
     38 
     39 i=1
     40 tracks="$(awk 'END{ print NR }' "$1")"
     41 while [ "$i" -le "$tracks" ]; do
     42 	printf 'track %d: ' "$i"
     43 	t_start="$(awk -v i="$i" '{ 
     44 		if (NR==i) {
     45 			match($0, /[0-9]*:*[0-9]+:[0-5][0-9]/)
     46 			print(substr($0, RSTART, RLENGTH))
     47 		}}' "$1")"
     48 	t_end="$(awk -v i="$i" '{ 
     49 		if (NR==i+1) {
     50 			match($0, /[0-9]+:[0-5][0-9]/)
     51 			print(substr($0, RSTART, RLENGTH))
     52 		}}' "$1")"
     53 	title="$(sed -n "s/ *(*[0-9]*:*[0-9]*[0-9]:[0-9][0-9])*//;
     54 					 s/^ *//;s/ *$//;${i}p" "$1")"
     55 	if [ -z "$t_end" ]; then
     56 		t_end="$(ffprobe "$2" 2>&1 | grep 'Duration: ' |\
     57 				 sed 's/,.*//;s/.* //')"
     58 	fi
     59 	printf '%s to %s\n' "$t_start" "$t_end"
     60 	out="$(printf '%02d %s - %s.%s' "$i" "${2%.*}" "$title" "${2##*.}")"
     61 	ffmpeg -y -ss "$t_start" -to "$t_end" -i "$2" "$out"
     62 	id3tag --track="$i" --song="$title" --artist="$artist" --album="$album"\
     63 		"$out"
     64 	i=$(( i + 1 ))
     65 done