diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d7376e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +artwork +songs +metadata diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..72d5b2d --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +INSTALL_DIR = ./install + +SONGS_DIR = ./data/raw/songs +ARTWORK_DIR = ./data/raw/artwork +METADATA_DIR = ./data/processed/01__metadata +ENCODE_DIR = ./data/processed/02__encode + +METADATA_FILE = ./data/raw/metadata.tsv +SONG_FILES = $(wildcard $(SONGS_DIR)/*) +METADATA_FILES = $(addsuffix .txt, $(addprefix $(METADATA_DIR)/, $(basename $(notdir $(SONG_FILES))))) +ENCODE_FILES = $(addsuffix .flac, $(addprefix $(ENCODE_DIR)/, $(basename $(notdir $(SONG_FILES))))) + +.PHONY: install all encode metadata clean + +# **** Step 01 - Metadata Generation **** + +metadata: $(METADATA_FILES) + +$(METADATA_FILES): $(METADATA_FILE) ./src/01__metadata.sh + @./src/01__metadata.sh \ + -i $(METADATA_FILE) \ + -o $(METADATA_DIR) + +# **** Step 02 - Encode **** + +encode: $(ENCODE_FILES) + +$(ENCODE_FILES): $(ENCODE_DIR)/%.flac: $(SONGS_DIR)/% $(METADATA_FILES) ./src/02__encode.sh + @./src/02__encode.sh \ + -i $< \ + -m $(addsuffix .txt, $(addprefix $(METADATA_DIR)/, $(basename $(notdir $<)))) \ + -a $(ARTWORK_DIR) \ + -o $@ + +# **** Step 03 - Install **** + +install: + @./src/install.sh \ + -r $(ENCODE_DIR) \ + -m $(METADATA_DIR) \ + -i "$(INSTALL_DIR)" + +all: metadata encode + +clean: + rm -rf $(ENCODE_DIR) + rm -rf $(METADATA_DIR) + mkdir $(ENCODE_DIR) + mkdir $(METADATA_DIR) diff --git a/scripts/flac.sh b/scripts/flac.sh new file mode 100644 index 0000000..cf65d45 --- /dev/null +++ b/scripts/flac.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Variables for color print. +GREEN="\e[32m" +CYAN="\e[36m" +END="\e[0m" + +# Get filename and extension. +name="${snakemake_input[0]##*/}" +ext="${name##*.}" + +printf "%b: %s\r" "${CYAN}[ i ]${END} Encoding file" "${name}" + +if [[ "${ext}" != ".flac" ]] +then + ffmpeg \ + -i "${snakemake_input[0]}" \ + -vn \ + -c:a flac \ + -hide_banner \ + -loglevel error \ + "${snakemake_output[0]}" +else + cp "${snakemake_input[0]}" "${snakemake_output[0]}" +fi + +printf "%b: %s\r" "${GREEN}[ ✓ ]${END} Finished encoding file" "${name}" diff --git a/src/01__metadata.sh b/src/01__metadata.sh new file mode 100755 index 0000000..9dbbe9d --- /dev/null +++ b/src/01__metadata.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Variables for color print. +GREEN="\e[32m" +RED="\e[31m" +CYAN="\e[36m" +END="\e[0m" + +# Get command line arguments. +while getopts i:o: flag +do + case "${flag}" in + i) input_metadata=${OPTARG};; + o) output_dir=${OPTARG};; + *) printf "%b\n" "${RED}[ ! ]${END} Invalid Argument" + exit 1;; + esac +done + +{ + # Skip header line of .tsv. + read -r + + while IFS=$'\t' read -r code extension track title artist album year disc artwork + do + file_path="${output_dir}/${code}.txt" + + printf "%b: %s\r" "${CYAN}[ i ]${END} Processing file" "${code}${extension}" + + # Create metadata file. + touch "${file_path}" + + # Write metadata to file. + { + printf "%s\t" "${extension}" + printf "%s\t" "${track}" + printf "%s\t" "${title}" + printf "%s\t" "${artist}" + printf "%s\t" "${album}" + printf "%s\t" "${year}" + printf "%s\t" "${disc}" + printf "%s" "${artwork}" + } >> "${file_path}" + + printf "%b: %s\n" "${GREEN}[ ✓ ]${END} Generated metadata file for" "${code}${extension}" + done +} < "${input_metadata}" diff --git a/src/02__encode.sh b/src/02__encode.sh new file mode 100755 index 0000000..0c99b96 --- /dev/null +++ b/src/02__encode.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Variables for color print. +GREEN="\e[32m" +RED="\e[31m" +CYAN="\e[36m" +END="\e[0m" + +# Get command line arguments. +while getopts i:m:a:o: flag +do + case "${flag}" in + i) input_file=${OPTARG};; + m) input_metadata=${OPTARG};; + a) artwork_dir=${OPTARG};; + o) output_file=${OPTARG};; + *) printf "%b\n" "${RED}[ ! ]${END} Invalid Argument" + exit 1;; + esac +done + +printf "%b: %s\r" "${CYAN}[ i ]${END} Encoding file" "${input_file}" + +# Read file metadata. +IFS=$'\t' read -r extension track title artist album year disc artwork < "${input_metadata}" + +if [[ "${extension}" != ".flac" ]] +then + ffmpeg \ + -i "${input_file}" \ + -vn\ + -c:a flac \ + -hide_banner \ + -loglevel error \ + "${output_file}" +else + cp "${input_file}" "${output_file}" +fi + +# Remove any exsiting metadata. +metaflac \ + --remove-all-tags \ + "${output_file}" + +# Remove any album artwork. +metaflac \ + --remove \ + --block-type=PICTURE,PADDING \ + --dont-use-padding \ + "${output_file}" + +metaflac \ + --remove-tag=COVERART \ + --dont-use-padding \ + "${output_file}" + +# Write new metadata. +metaflac \ + --set-tag=ALBUM="${album}" \ + --set-tag=ARTIST="${artist}" \ + --set-tag=TITLE="${title}" \ + --set-tag=TRACKNUMBER="${track}" \ + --set-tag=DATE="${year}" \ + --set-tag=DISCNUMBER="${disc}" \ + --import-picture-from="${artwork_dir}/${artwork}" \ + "${output_file}" + +printf "%b: %s\n" "${GREEN}[ ✓ ]${END} Succesfully encoded file" "${input_file}" diff --git a/src/install.sh b/src/install.sh new file mode 100755 index 0000000..21c96dc --- /dev/null +++ b/src/install.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Variables for color print. +GREEN="\e[32m" +RED="\e[31m" +CYAN="\e[36m" +END="\e[0m" + +# Get command line arguments. +while getopts r:m:i: flag +do + case "${flag}" in + r) encode_dir=${OPTARG};; + m) metadata_dir=${OPTARG};; + i) install_dir=${OPTARG};; + *) printf "%b\n" "${RED}[ ! ]${END} Invalid Argument" + exit 1;; + esac +done + +# Create install directory (if it does not exist). +mkdir -p "${install_dir}" + +for file in "${metadata_dir}"/*.txt +do + # Read file metadata. + IFS=$'\t' read -r extension track title artist album year disc artwork < "${file}" + + # Get filename without extension. + filename=$(basename "${file}" ".txt") + + # Replace any '/' with '-' in the song title. + title="${title//\//-}" + + # Add leading zeroes to disc number. + disc_num=$(printf "%02d" "${disc}") + + # Add leading zeroes to track number. + track_num=$(printf "%02d" "${track}") + + # Create album directory (if it does not exist). + mkdir -p "${install_dir}/${album}" + + # Copy file to installation directory and rename. + cp "${encode_dir}/${filename}.flac" \ + "${install_dir}/${album}/${disc_num}-${track_num} - ${title}.flac" + + printf "%b: %s\n" "${GREEN}[ ✓ ]${END} Succesfully installed file" "${filename}" +done