Skip to content

Instantly share code, notes, and snippets.

@henryjcee
Last active September 20, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henryjcee/6ce23182b5763f86b36897feb61054e5 to your computer and use it in GitHub Desktop.
Save henryjcee/6ce23182b5763f86b36897feb61054e5 to your computer and use it in GitHub Desktop.
Fish mp3 scripts

mp3name.fish - Rename audio files from ID3 tags. Accepts a glob as input e.g. > mp3name **.mp3 and requires ffprobe and mutagen to run (both brewable iirc)

function mp3name --description 'Rename mp3 from tags'

	if test (count $argv) = 0
		echo 'No files provided, exiting'
		return 1
	end

	for file_name in $argv

	    mid3iconv -e UTF-8 $file_name
		ffprobe -show_entries format_tags=album,artist,title -of default=noprint_wrappers=1:nokey=0 -hide_banner -loglevel warning $file_name | read -z result

		if test -z "$result"
			echo "ffprobe failed for $file_name, skipping"
			continue
		end

		set title (echo $result | grep 'title' | sed -e 's/TAG:title=//' | sed -e 's/\// /')
		set artist (echo $result | grep 'artist' | sed -e 's/TAG:artist=//' | sed -e 's/\// /')
		set album (echo $result | grep 'album' | sed -e 's/TAG:album=//' | sed -e 's/\// /')

		if test -z "$title"; or test -z "$artist"
			echo 'Title or artist tags missing, exiting...'
			continue
		end

		if test -n "$album"
			set album ", $album"
		end

		set extension (string split -r -m1 . $file_name)[2]
		set output_file_name "$artist - $title$album.$extension"
		set output_file (dirname $file_name)"/$output_file_name"

		mv "$file_name" "$output_file"
		echo "$output_file"
	end

	return 0
end

mp320.fish - Convert flac to mp3 320 CBR. Accepts a glob as input as above. Requires mutagen and sox to run (again, both brewable iirc)

function mp320 --description 'Convert file to mp3'

	if test (count $argv) = 0
		echo 'No files provided, exiting'
		return 1
	end

	for file_name in $argv
		set file_title (string split -r -m1 . "$file_name")[1]

		sox "$file_name" -C 320 "$file_title.mp3"
		mid3iconv -e UTF-8 "$file_title.mp3"

		if test $status -eq 0
			rm "$file_name"
			echo "$file_title.mp3"
			continue
		end
		echo "Unable to process $file_name"
	end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment