Recently Written · git

sh

Small shell and Python scripts

git clone https://github.com/equwal/sh

Log | Files | Refs


tag-music (1621 bytes)

1 #!/bin/sh
2 # Set audio tags via ffmpeg stream copy. Usage: tag-music [OPTIONS] file
3 # Based on "tag" from Luke Smith's voidrice (GPL-3.0): https://github.com/LukeSmithxyz/voidrice
4 
5 err() { echo "Usage:
6 	tag-music [OPTIONS] file
7 Options:
8 	-a: artist/author
9 	-t: song/chapter title
10 	-A: album/book title
11 	-n: track/chapter number
12 	-N: total number of tracks/chapters
13 	-d: year of publication
14 	-g: genre
15 	-c: comment
16 You will be prompted for title, artist, album and track if not given." && exit 1 ;}
17 
18 while getopts "a:t:A:n:N:d:g:c:" o; do case "${o}" in
19 	a) artist="${OPTARG}" ;;
20 	t) title="${OPTARG}" ;;
21 	A) album="${OPTARG}" ;;
22 	n) track="${OPTARG}" ;;
23 	N) total="${OPTARG}" ;;
24 	d) date="${OPTARG}" ;;
25 	g) genre="${OPTARG}" ;;
26 	c) comment="${OPTARG}" ;;
27 	*) printf "Invalid option: -%s\\n" "$OPTARG" && err ;;
28 esac done
29 
30 shift $((OPTIND - 1))
31 
32 file="$1"
33 
34 [ ! -f "$file" ] && echo 'Provide file to tag.' && err
35 
36 [ -z "$title" ] && echo 'Enter a title.' && read -r title
37 [ -z "$artist" ] && echo 'Enter an artist.' && read -r artist
38 [ -z "$album" ] && echo 'Enter an album.' && read -r album
39 [ -z "$track" ] && echo 'Enter a track number.' && read -r track
40 
41 temp="$(mktemp -d "$(dirname "$file")/.tag-music.XXXXXX")" || exit 1
42 trap 'rm -rf "$temp"' HUP INT QUIT TERM EXIT
43 
44 ffmpeg -i "$file" -map 0 -y -codec copy \
45 	-metadata title="$title" \
46 	-metadata album="$album" \
47 	-metadata artist="$artist" \
48 	-metadata track="${track}${total:+/"$total"}" \
49 	${date:+-metadata date="$date"} \
50 	${genre:+-metadata genre="$genre"} \
51 	${comment:+-metadata comment="$comment"} "$temp/${file##*/}" &&
52 	mv -f "$temp/${file##*/}" "$file"