mirror of
https://github.com/levogevo/add-synced-lyrics.git
synced 2026-07-21 21:45:21 +00:00
Compare commits
5
Commits
3fec74f772
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc27d4493c | ||
|
|
3427522cb6 | ||
|
|
c0c7364e4a | ||
|
|
ffb5cafaf3 | ||
|
|
5ecfd07ad2 |
@@ -0,0 +1,35 @@
|
|||||||
|
# add-synced-lyrics
|
||||||
|
A script that automates adding lyrics to your songs.
|
||||||
|
```
|
||||||
|
add-synced-lyrics.sh [options]
|
||||||
|
|
||||||
|
OPTIONS:
|
||||||
|
-d, --dir process files in directory
|
||||||
|
-r, --recurse recursively process directory
|
||||||
|
-f, --file process one file
|
||||||
|
-e, --embed embed lyrics directly into the song file
|
||||||
|
-n, --dry-run do not add synced lyrics,
|
||||||
|
just show the files that would be processed
|
||||||
|
-i, --ignore ignore errors, and continue attempting to find lyrics
|
||||||
|
even in the case of errors
|
||||||
|
-h, --help show this output
|
||||||
|
--debug extra information useful for debugging
|
||||||
|
--eval output bash functions that can be directly processed
|
||||||
|
by eval for use with testing this script.
|
||||||
|
```
|
||||||
|
|
||||||
|
# Requirements
|
||||||
|
- CLI tools:
|
||||||
|
- coreutils: `mktemp cat sort base64 readlink`
|
||||||
|
- `ffmpeg ffprobe`
|
||||||
|
- `jq`
|
||||||
|
- `node` (any version)
|
||||||
|
- [`librelyrics`](https://github.com/libre-lyrics/librelyrics):
|
||||||
|
- uses the [deezer](https://github.com/libre-lyrics/librelyrics-deezer) and [spotify](https://github.com/libre-lyrics/librelyrics-spotify) plugins
|
||||||
|
- [`spotify`](https://github.com/ledesmablt/spotify-cli) most easily installed with `uv tool install --python 3.8 spotify-cli`
|
||||||
|
- Music that has the appropriate ISRC tag.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
- Uses the ISRC tag to search for synced lyrics across the download providers: lrclib, spotify, and deezer (in that order).
|
||||||
|
- Caches all networked-calls to the download providers.
|
||||||
|
- Supports separate (dedicated .lrc file) or adding the lyrics directly to the song file with the LYRICS tag.
|
||||||
+31
-22
@@ -39,9 +39,9 @@ bash_dirname() {
|
|||||||
printf '%s\n' "${tmp:-/}"
|
printf '%s\n' "${tmp:-/}"
|
||||||
}
|
}
|
||||||
|
|
||||||
PROGPATH="$(readlink -f "$0")"
|
PROGPATH="$(readlink -f "$0")" || exit 1
|
||||||
PROGDIR="$(bash_dirname "${PROGPATH}")"
|
PROGDIR="$(bash_dirname "${PROGPATH}")" || exit 1
|
||||||
PROGNAME="$(bash_basename "${PROGPATH}")"
|
PROGNAME="$(bash_basename "${PROGPATH}")" || exit 1
|
||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
readonly PROGPATH PROGDIR PROGNAME || exit 1
|
readonly PROGPATH PROGDIR PROGNAME || exit 1
|
||||||
|
|
||||||
@@ -56,6 +56,7 @@ process_inputs() {
|
|||||||
download_with_lrclib
|
download_with_lrclib
|
||||||
download_with_spotify
|
download_with_spotify
|
||||||
download_with_deezer
|
download_with_deezer
|
||||||
|
download_with_lyricsify
|
||||||
)
|
)
|
||||||
|
|
||||||
local numInputs="${#INPUTS[@]}"
|
local numInputs="${#INPUTS[@]}"
|
||||||
@@ -74,15 +75,15 @@ process_inputs() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
output=''
|
output=''
|
||||||
output="$(set_lyric_file_name "${input}")"
|
if ! output="$(set_lyric_file_name "${input}")"; then
|
||||||
|
echo_fail "could not determine lyric file name for ${input}"
|
||||||
|
ret=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ -f "${output}" ]]; then
|
if validate_song_lyrics "${input}" "${output}"; then
|
||||||
if is_valid_lrc "${output}"; then
|
|
||||||
echo_pass "${input} already has lyrics, skipping"
|
echo_pass "${input} already has lyrics, skipping"
|
||||||
continue
|
continue
|
||||||
else
|
|
||||||
rm "${output}" || return 1
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
isrc=''
|
isrc=''
|
||||||
@@ -91,26 +92,34 @@ process_inputs() {
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local tmpOutput="${TMPDIR}/tmp.lrc"
|
local tmpOutput="${TMPDIR}/$(bash_basename "${output}")"
|
||||||
for func in "${downloadFuncs[@]}"; do
|
for func in "${downloadFuncs[@]}"; do
|
||||||
"${func}" "${isrc}" "${tmpOutput}" && break
|
# try to download
|
||||||
|
if ! "${func}" "${isrc}" "${tmpOutput}"; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# validate
|
||||||
|
if validate_lrc "${tmpOutput}"; then
|
||||||
|
# passed download and validation, stop processing
|
||||||
|
break
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ -f "${tmpOutput}" ]]; then
|
# skip processing since no files should be modified
|
||||||
if is_valid_lrc "${tmpOutput}"; then
|
[[ ${DRY_RUN} == true ]] && continue
|
||||||
mv "${tmpOutput}" "${output}" &>/dev/null || return 1
|
|
||||||
else
|
|
||||||
rm "${tmpOutput}" || return 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ! void test -f "${output}"; then
|
if [[ -f "${tmpOutput}" ]]; then
|
||||||
|
add_lyrics_to_song \
|
||||||
|
"${input}" \
|
||||||
|
"${tmpOutput}" \
|
||||||
|
"${output}" &&
|
||||||
|
echo_pass "added lyrics for ${input}"
|
||||||
|
else
|
||||||
echo_fail "could not find lyrics for ${input}"
|
echo_fail "could not find lyrics for ${input}"
|
||||||
ret=1
|
ret=1
|
||||||
continue
|
|
||||||
else
|
|
||||||
void echo_pass "added lyrics for ${input}"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
return ${ret}
|
return ${ret}
|
||||||
|
|||||||
+159
-16
@@ -32,7 +32,7 @@ stderr() {
|
|||||||
local word="${WORD:-}"
|
local word="${WORD:-}"
|
||||||
local color="${COLOR:-}"
|
local color="${COLOR:-}"
|
||||||
local function="${FUNCNAME[2]}"
|
local function="${FUNCNAME[2]}"
|
||||||
if [[ "${function}" =~ void ]]; then
|
if [[ "${function}" =~ debug|void ]]; then
|
||||||
function="${FUNCNAME[3]}"
|
function="${FUNCNAME[3]}"
|
||||||
fi
|
fi
|
||||||
echo \
|
echo \
|
||||||
@@ -44,7 +44,7 @@ stderr() {
|
|||||||
echo_info() { WORD=INFO COLOR="${CYAN}" stderr "$@"; }
|
echo_info() { WORD=INFO COLOR="${CYAN}" stderr "$@"; }
|
||||||
echo_fail() { WORD=FAIL COLOR="${RED}" stderr "$@"; }
|
echo_fail() { WORD=FAIL COLOR="${RED}" stderr "$@"; }
|
||||||
echo_pass() { WORD=PASS COLOR="${GREEN}" stderr "$@"; }
|
echo_pass() { WORD=PASS COLOR="${GREEN}" stderr "$@"; }
|
||||||
echo_dbug() { WORD=DBUG COLOR="${PURPLE}" stderr "$@"; }
|
echo_debug() { WORD=DBUG COLOR="${PURPLE}" stderr "$@"; }
|
||||||
|
|
||||||
# only perform the command given if ${DEBUG} is enabled
|
# only perform the command given if ${DEBUG} is enabled
|
||||||
debug() {
|
debug() {
|
||||||
@@ -55,7 +55,7 @@ debug() {
|
|||||||
"$@"
|
"$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# only perform the command given if ${DRY_RUN} is enabled
|
# only perform the command given if ${DRY_RUN} is disabled
|
||||||
void() {
|
void() {
|
||||||
if [[ ${DRY_RUN} == true ]]; then
|
if [[ ${DRY_RUN} == true ]]; then
|
||||||
return 0
|
return 0
|
||||||
@@ -89,13 +89,29 @@ dry() {
|
|||||||
# check required utilities for this project
|
# check required utilities for this project
|
||||||
check_required_utils() {
|
check_required_utils() {
|
||||||
local utils=(
|
local utils=(
|
||||||
base64
|
# coreutils
|
||||||
|
bash
|
||||||
cat
|
cat
|
||||||
ffprobe
|
sort
|
||||||
|
base64
|
||||||
|
mktemp
|
||||||
|
readlink
|
||||||
|
|
||||||
jq
|
jq
|
||||||
|
node
|
||||||
|
docker
|
||||||
|
ffmpeg
|
||||||
|
ffprobe
|
||||||
|
|
||||||
librelyrics
|
librelyrics
|
||||||
spotify # uv tool install --python 3.8 spotify-cli
|
spotify
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if [[ "${1:-}" == '--print-required' ]]; then
|
||||||
|
echo "utils='${utils[*]}'"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
local missing=false
|
local missing=false
|
||||||
for util in "${utils[@]}"; do
|
for util in "${utils[@]}"; do
|
||||||
if ! have_cmd "${util}"; then
|
if ! have_cmd "${util}"; then
|
||||||
@@ -111,17 +127,33 @@ check_required_utils() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echo_if_fail() {
|
||||||
|
local cmd=("$@")
|
||||||
|
local output
|
||||||
|
output="$("${cmd[@]}" 2>&1)"
|
||||||
|
local ret=$?
|
||||||
|
|
||||||
|
if [[ ${ret} -ne 0 ]]; then
|
||||||
|
echo_fail "${cmd[*]}"
|
||||||
|
echo_fail "${output}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
return ${ret}
|
||||||
|
}
|
||||||
|
|
||||||
cache_command() {
|
cache_command() {
|
||||||
local cmd=("$@")
|
local cmd=("$@")
|
||||||
local hash cacheFile cmdOut contents ret
|
local hash cacheFile cmdOut contents ret
|
||||||
hash="$(sha256sum <<<"${cmd[*]}")" || return 1
|
hash="$(sha256sum <<<"${cmd[*]}")" || return 1
|
||||||
read -r cacheFile _ <<<"${hash}"
|
read -r cacheFile _ <<<"${hash}"
|
||||||
cacheFilePath="${CACHE_DIR}/${cacheFile}"
|
cacheFilePath="${CACHE_DIR}/${cacheFile}"
|
||||||
if [[ -f "${cacheFilePath}" && ${DRY_RUN} == false ]]; then
|
if [[ -f "${cacheFilePath}" ]]; then
|
||||||
|
mapfile -t cmdArr <<<"${cmd[*]}"
|
||||||
|
local skipLines="${#cmdArr[@]}"
|
||||||
mapfile -t contents <"${cacheFilePath}"
|
mapfile -t contents <"${cacheFilePath}"
|
||||||
ret=$?
|
ret=$?
|
||||||
# skip first line since it is the command
|
# skip N lines respective to the command length
|
||||||
printf '%s\n' "${contents[@]:1}"
|
printf '%s\n' "${contents[@]:${skipLines}}"
|
||||||
else
|
else
|
||||||
cmdOut="$(dry "${cmd[@]}")"
|
cmdOut="$(dry "${cmd[@]}")"
|
||||||
ret=$?
|
ret=$?
|
||||||
@@ -138,6 +170,8 @@ cache_command() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create_tmp_dir() {
|
create_tmp_dir() {
|
||||||
|
[[ -n "${TMPDIR:-}" ]] && return
|
||||||
|
|
||||||
TMPDIR="$(mktemp -d)" || return 1
|
TMPDIR="$(mktemp -d)" || return 1
|
||||||
readonly TMPDIR
|
readonly TMPDIR
|
||||||
trap 'rm -rf ${TMPDIR}' EXIT
|
trap 'rm -rf ${TMPDIR}' EXIT
|
||||||
@@ -147,8 +181,10 @@ check_tmp_dir_empty() {
|
|||||||
for f in "${TMPDIR}"/*; do
|
for f in "${TMPDIR}"/*; do
|
||||||
if [[ -f "${f}" ]]; then
|
if [[ -f "${f}" ]]; then
|
||||||
echo_fail "tempdir ${TMPDIR} is not empty!"
|
echo_fail "tempdir ${TMPDIR} is not empty!"
|
||||||
|
echo_fail "contains ${f}"
|
||||||
echo_fail "aborting operation"
|
echo_fail "aborting operation"
|
||||||
return 1
|
# execution becomes undefined if TMPDIR is not empty
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -228,6 +264,7 @@ output_eval_functions() {
|
|||||||
"${function}" == set_default_options ]] || continue
|
"${function}" == set_default_options ]] || continue
|
||||||
${line}
|
${line}
|
||||||
done <<<"$(declare -F)"
|
done <<<"$(declare -F)"
|
||||||
|
check_required_utils --print-required
|
||||||
}
|
}
|
||||||
|
|
||||||
get_file_isrc() {
|
get_file_isrc() {
|
||||||
@@ -253,28 +290,134 @@ get_file_isrc() {
|
|||||||
echo "${jqResult}"
|
echo "${jqResult}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_file_lyrics() {
|
||||||
|
local file="$1"
|
||||||
|
local probe='' jqResult=''
|
||||||
|
|
||||||
|
local probe=''
|
||||||
|
probe="$(
|
||||||
|
ffprobe \
|
||||||
|
-v error \
|
||||||
|
-show_entries stream \
|
||||||
|
-of json \
|
||||||
|
"${file}"
|
||||||
|
)" || return 1
|
||||||
|
|
||||||
|
local jqFilter='.streams[] | select(.codec_type == "audio") | .tags | with_entries(.key |= ascii_downcase) | .lyrics'
|
||||||
|
jqResult="$(jq -r "${jqFilter}" <<<"${probe}")"
|
||||||
|
|
||||||
|
if [[ "${jqResult}" == 'null' ]]; then
|
||||||
|
debug echo_fail "could not find lyrics for ${file}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo "${jqResult}"
|
||||||
|
}
|
||||||
|
|
||||||
|
is_music_file() {
|
||||||
|
local file="$1"
|
||||||
|
[[ "${file}" =~ ${MUSIC_EXTENSION_REGEX} ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
is_lrc_file() {
|
||||||
|
local file="$1"
|
||||||
|
[[ "${file}" == *'.lrc' ]]
|
||||||
|
}
|
||||||
|
|
||||||
set_lyric_file_name() {
|
set_lyric_file_name() {
|
||||||
local file="$1"
|
local file="$1"
|
||||||
if [[ ! "${file}" =~ ${MUSIC_EXTENSION_REGEX} ]]; then
|
if ! is_music_file "${file}"; then
|
||||||
echo "${file} is not a music file"
|
echo_fail "${file} is not a music file"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "${file%.opus}.lrc"
|
echo "${file%.opus}.lrc"
|
||||||
}
|
}
|
||||||
|
|
||||||
is_valid_lrc() {
|
validate_lrc() {
|
||||||
local file="$1"
|
local file="$1"
|
||||||
|
|
||||||
|
# file does not exist, invalid
|
||||||
|
if [[ ! -f "${file}" ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# file is not lyric file, invalid
|
||||||
|
if ! is_lrc_file "${file}"; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mapfile -t fileContents <"${file}"
|
||||||
|
|
||||||
local valid=1
|
local valid=1
|
||||||
while read -r line; do
|
for line in "${fileContents[@]}"; do
|
||||||
[[ "${#line}" -eq 0 ]] && continue
|
[[ "${#line}" -eq 0 ]] && continue
|
||||||
if [[ "${line}" != '['* ]]; then
|
if [[ "${line}" != '['* && "${line}" != '#'* ]]; then
|
||||||
valid=1
|
valid=1
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
valid=0
|
valid=0
|
||||||
done <"${file}"
|
done
|
||||||
|
|
||||||
|
if [[ ${valid} -eq 1 ]]; then
|
||||||
|
debug echo_fail "${file} is not valid lrc file"
|
||||||
|
dry rm "${file}" || return 1
|
||||||
|
fi
|
||||||
|
|
||||||
return ${valid}
|
return ${valid}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validate_song_lyrics() {
|
||||||
|
local songFile="$1"
|
||||||
|
local lyricsDestination="$2"
|
||||||
|
|
||||||
|
# embed or not both get validated
|
||||||
|
validate_lrc "${lyricsDestination}"
|
||||||
|
local validateDestination=$?
|
||||||
|
|
||||||
|
# only continue to validate for embed
|
||||||
|
if [[ ${EMBED} == false ]]; then
|
||||||
|
return ${validateDestination}
|
||||||
|
fi
|
||||||
|
|
||||||
|
local tmpLyricsFile="${TMPDIR}/$(bash_basename "${lyricsDestination}")"
|
||||||
|
get_file_lyrics "${songFile}" >"${tmpLyricsFile}"
|
||||||
|
validate_lrc "${tmpLyricsFile}"
|
||||||
|
local validateRet=$?
|
||||||
|
[[ ${validateRet} -eq 0 ]] && rm "${tmpLyricsFile}"
|
||||||
|
|
||||||
|
return ${validateRet}
|
||||||
|
}
|
||||||
|
|
||||||
|
add_lyrics_to_song() {
|
||||||
|
local songFile="$1"
|
||||||
|
local lyricsToAdd="$2"
|
||||||
|
local lyricsDestination="$3"
|
||||||
|
|
||||||
|
# this function removes file so be extra certain
|
||||||
|
is_music_file "${songFile}" &&
|
||||||
|
is_lrc_file "${lyricsToAdd}" &&
|
||||||
|
is_lrc_file "${lyricsDestination}" || return 1
|
||||||
|
|
||||||
|
if [[ ${EMBED} == false ]]; then
|
||||||
|
mv "${lyricsToAdd}" "${lyricsDestination}" &>/dev/null
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
local tmpSongFile="${TMPDIR}/$(bash_basename "${songFile}")"
|
||||||
|
echo_if_fail \
|
||||||
|
ffmpeg \
|
||||||
|
-hide_banner \
|
||||||
|
-i "${songFile}" \
|
||||||
|
-metadata "LYRICS=$(<"${lyricsToAdd}")" \
|
||||||
|
-c copy \
|
||||||
|
"${tmpSongFile}"
|
||||||
|
local ffmpegRet=$?
|
||||||
|
rm "${lyricsToAdd}"
|
||||||
|
|
||||||
|
if [[ ${ffmpegRet} -eq 0 ]]; then
|
||||||
|
mv "${tmpSongFile}" "${songFile}" &>/dev/null || return 1
|
||||||
|
if [[ -f "${lyricsDestination}" ]]; then
|
||||||
|
rm "${lyricsDestination}" || return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
FLARESOLVERR_PORT="${FLARESOLVERR_PORT:-8191}"
|
||||||
|
validate_flaresolverr() {
|
||||||
|
local checkMsg="$(curl --connect-timeout 1 -s localhost:${FLARESOLVERR_PORT} | jq -r .msg)"
|
||||||
|
|
||||||
|
if [[ "${checkMsg}" == 'FlareSolverr is ready!' ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
debug echo_debug 'flaresolverr not running, attempting to start with docker'
|
||||||
|
local containerName='flaresolverr'
|
||||||
|
docker inspect ${containerName} &>/dev/null && docker container rm ${containerName}
|
||||||
|
|
||||||
|
echo_if_fail docker run -d \
|
||||||
|
--name=flaresolverr \
|
||||||
|
-p ${FLARESOLVERR_PORT}:${FLARESOLVERR_PORT} \
|
||||||
|
-e LOG_LEVEL=info \
|
||||||
|
--restart unless-stopped \
|
||||||
|
ghcr.io/flaresolverr/flaresolverr:latest
|
||||||
|
local startupRet=$?
|
||||||
|
|
||||||
|
[[ ${startupRet} -ne 0 ]] && return ${startupRet}
|
||||||
|
|
||||||
|
# wait a little to make sure the container is ready
|
||||||
|
sleep 3
|
||||||
|
return ${startupRet}
|
||||||
|
}
|
||||||
|
|
||||||
|
get_lyricsify_response() {
|
||||||
|
local isrc="$1"
|
||||||
|
|
||||||
|
validate_flaresolverr || return 1
|
||||||
|
|
||||||
|
local deezerResponse=''
|
||||||
|
deezerResponse="$(get_deezer_isrc_response "${isrc}")" || return 1
|
||||||
|
|
||||||
|
# lyricsify uses artist - song name for URL
|
||||||
|
# so obtain data from deezer
|
||||||
|
local deezerMetadata=(
|
||||||
|
"track .title"
|
||||||
|
"artist .artist.name"
|
||||||
|
)
|
||||||
|
for data in "${deezerMetadata[@]}"; do
|
||||||
|
IFS=' ' read -r env jqFilter <<<"${data}"
|
||||||
|
jqResult="$(jq -r "${jqFilter}" <<<"${deezerResponse}")"
|
||||||
|
if ! jq_result_valid "${jqResult}"; then
|
||||||
|
echo_fail "could not determine lyricsify ${env} for ${isrc}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
declare "${env}=${jqResult}"
|
||||||
|
debug echo_debug "${env}=${jqResult}"
|
||||||
|
done
|
||||||
|
|
||||||
|
# variables assigned above
|
||||||
|
# shellcheck disable=SC2154
|
||||||
|
artist="${artist,,}"
|
||||||
|
track="${track,,}"
|
||||||
|
artist="${artist// /-}"
|
||||||
|
track="${track// /-}"
|
||||||
|
local lyricsifyUrl="https://www.lyricsify.com/lyrics/${artist}/${track}"
|
||||||
|
debug echo_debug "using ${lyricsifyUrl}"
|
||||||
|
|
||||||
|
local jsonPayload
|
||||||
|
jsonPayload="$(
|
||||||
|
jq -n \
|
||||||
|
--arg cmd 'request.get' \
|
||||||
|
--arg url "${lyricsifyUrl}" \
|
||||||
|
--argjson timeout 60000 \
|
||||||
|
'{cmd: $cmd, url: $url, maxTimeout: $timeout}'
|
||||||
|
)" || return 1
|
||||||
|
|
||||||
|
cache_command \
|
||||||
|
curl \
|
||||||
|
-L \
|
||||||
|
-X POST \
|
||||||
|
"http://localhost:${FLARESOLVERR_PORT}/v1" \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
--data-raw "${jsonPayload}"
|
||||||
|
}
|
||||||
|
|
||||||
|
process_lyricsify_response() {
|
||||||
|
local response="$1"
|
||||||
|
|
||||||
|
local responseMessage="$(jq -r '.message' <<<"${response}")"
|
||||||
|
if [[ "${responseMessage}" != 'Challenge solved!' ]]; then
|
||||||
|
debug echo_fail "could not solve challenge"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local solution="$(jq -r '.solution.response' <<<"${response}")"
|
||||||
|
|
||||||
|
local variableSearch='lrcText'
|
||||||
|
while read -r line; do
|
||||||
|
if [[ "${line}" == *"var ${variableSearch} = "* ]]; then
|
||||||
|
node -e "${line} ; console.log(${variableSearch})"
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
done <<<"${solution}"
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
DOWNLOAD_FUNCTIONS+=(download_with_lyricsify)
|
||||||
|
download_with_lyricsify() {
|
||||||
|
local isrc="$1"
|
||||||
|
local output="$2"
|
||||||
|
|
||||||
|
local lyricsifyResponse syncedLyrics
|
||||||
|
lyricsifyResponse="$(get_lyricsify_response "${isrc}")" || return 1
|
||||||
|
syncedLyrics="$(process_lyricsify_response "${lyricsifyResponse}")" || return 1
|
||||||
|
|
||||||
|
echo "${syncedLyrics}" >"${output}"
|
||||||
|
}
|
||||||
+16
-10
@@ -10,14 +10,15 @@ OPTIONS:
|
|||||||
-d, --dir process files in directory
|
-d, --dir process files in directory
|
||||||
-r, --recurse recursively process directory
|
-r, --recurse recursively process directory
|
||||||
-f, --file process one file
|
-f, --file process one file
|
||||||
-n, --dry-run do not actually add synced lyrics,
|
-e, --embed embed lyrics directly into the song file
|
||||||
|
-n, --dry-run do not add synced lyrics,
|
||||||
just show the files that would be processed
|
just show the files that would be processed
|
||||||
-e, --eval output bash functions that can be directly processed
|
|
||||||
by eval for use with testing this script.
|
|
||||||
-i, --ignore ignore errors, and continue attempting to find lyrics
|
-i, --ignore ignore errors, and continue attempting to find lyrics
|
||||||
even in the case of errors
|
even in the case of errors
|
||||||
-h, --help show this output
|
-h, --help show this output
|
||||||
--debug extra information useful for debugging"
|
--debug extra information useful for debugging
|
||||||
|
--eval output bash functions that can be directly processed
|
||||||
|
by eval for use with testing this script."
|
||||||
exit "${EXIT_CODE:-1}"
|
exit "${EXIT_CODE:-1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +28,7 @@ set_default_options() {
|
|||||||
RECURSE=${RECURSE:-false}
|
RECURSE=${RECURSE:-false}
|
||||||
DRY_RUN=${DRY_RUN:-false}
|
DRY_RUN=${DRY_RUN:-false}
|
||||||
IGNORE=${IGNORE:-false}
|
IGNORE=${IGNORE:-false}
|
||||||
|
EMBED=${EMBED:-false}
|
||||||
DEBUG=${DEBUG:-false}
|
DEBUG=${DEBUG:-false}
|
||||||
|
|
||||||
# cache
|
# cache
|
||||||
@@ -64,14 +66,10 @@ process_options() {
|
|||||||
readonly IGNORE=true
|
readonly IGNORE=true
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
--debug)
|
-e | --embed)
|
||||||
readonly DEBUG=true
|
readonly EMBED=true
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-e | --eval)
|
|
||||||
output_eval_functions
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
-f | --file)
|
-f | --file)
|
||||||
if [[ ! -f "${value}" ]]; then
|
if [[ ! -f "${value}" ]]; then
|
||||||
echo "file ${value} does not exist"
|
echo "file ${value} does not exist"
|
||||||
@@ -80,6 +78,14 @@ process_options() {
|
|||||||
readonly FILE="${value}"
|
readonly FILE="${value}"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
|
--debug)
|
||||||
|
readonly DEBUG=true
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
--eval)
|
||||||
|
output_eval_functions
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
-h | --help)
|
-h | --help)
|
||||||
EXIT_CODE=0 usage
|
EXIT_CODE=0 usage
|
||||||
;;
|
;;
|
||||||
|
|||||||
+114
-10
@@ -39,20 +39,24 @@ bash_dirname() {
|
|||||||
printf '%s\n' "${tmp:-/}"
|
printf '%s\n' "${tmp:-/}"
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly PROGPATH="$(readlink -f "$0")"
|
PROGPATH="$(readlink -f "$0")" || exit 1
|
||||||
readonly PROGDIR="$(bash_dirname "${PROGPATH}")"
|
PROGDIR="$(bash_dirname "${PROGPATH}")" || exit 1
|
||||||
readonly PROGNAME="$(bash_basename "${PROGPATH}")"
|
PROGNAME="$(bash_basename "${PROGPATH}")" || exit 1
|
||||||
|
readonly PROGPATH PROGDIR PROGNAME || exit 1
|
||||||
|
|
||||||
# to normalize paths
|
# to normalize paths
|
||||||
cd "${PROGDIR}"
|
cd "${PROGDIR}"
|
||||||
|
|
||||||
readonly TEST_DIR="test-dir"
|
readonly TEST_DIR="test-dir"
|
||||||
# lyrics exist for this song
|
# lyrics exist for this song
|
||||||
readonly GOOD_SONG="${TEST_DIR}/back burner.opus"
|
readonly GOOD_SONG="${TEST_DIR}/the cold sun.opus"
|
||||||
readonly GOOD_LYRICS="${GOOD_SONG//.opus/.lrc}"
|
readonly GOOD_LYRICS="${GOOD_SONG//.opus/.lrc}"
|
||||||
|
readonly GOOD_ISRC='TCJPF1733509'
|
||||||
# lyrics do not exist for this song
|
# lyrics do not exist for this song
|
||||||
readonly BAD_SONG="${TEST_DIR}/subdir/avalanche.opus"
|
readonly BAD_SONG="${TEST_DIR}/subdir/avalanche.opus"
|
||||||
readonly BAD_LYRICS="${BAD_SONG//.opus/.lrc}"
|
readonly BAD_LYRICS="${BAD_SONG//.opus/.lrc}"
|
||||||
|
readonly BAD_ISRC='US5261822978'
|
||||||
|
# program to test
|
||||||
readonly PROG="add-synced-lyrics.sh"
|
readonly PROG="add-synced-lyrics.sh"
|
||||||
# coverage
|
# coverage
|
||||||
readonly COVERAGE_DIR="coverage"
|
readonly COVERAGE_DIR="coverage"
|
||||||
@@ -74,7 +78,7 @@ run_test_cmd() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
local output
|
local output
|
||||||
output="$("${cmd[@]}")"
|
output="$("${cmd[@]}" 2>&1)"
|
||||||
local actualRetval=$?
|
local actualRetval=$?
|
||||||
|
|
||||||
declare -g CMD_OUTPUT="${output}"
|
declare -g CMD_OUTPUT="${output}"
|
||||||
@@ -85,7 +89,10 @@ run_test_cmd() {
|
|||||||
else
|
else
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_help_option() {
|
||||||
|
add-synced-lyrics --help
|
||||||
}
|
}
|
||||||
|
|
||||||
test_bad_input() {
|
test_bad_input() {
|
||||||
@@ -114,6 +121,11 @@ test_bad_input() {
|
|||||||
1 \
|
1 \
|
||||||
add-synced-lyrics --file "${PROGPATH}" --dir "${PROGDIR}" --recurse
|
add-synced-lyrics --file "${PROGPATH}" --dir "${PROGDIR}" --recurse
|
||||||
|
|
||||||
|
# non-existent flag
|
||||||
|
run_test_cmd \
|
||||||
|
1 \
|
||||||
|
add-synced-lyrics --this-flag-does-not-exist
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,27 +161,81 @@ test_determine_inputs_recurse() {
|
|||||||
RECURSE=true test_determine_inputs
|
RECURSE=true test_determine_inputs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_determine_inputs_bash() {
|
||||||
|
eval "$(add-synced-lyrics --eval)"
|
||||||
|
# TEST_ADD_BIN guaranteed to be single word
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local utils=(
|
||||||
|
${utils}
|
||||||
|
${TEST_ADD_BIN:-}
|
||||||
|
)
|
||||||
|
|
||||||
|
for util in "${utils[@]}"; do
|
||||||
|
test -f "${TEST_DIR}/${util}" && rm "${TEST_DIR}/${util}"
|
||||||
|
ln -s "$(command -v "${util}")" "${TEST_DIR}/${util}"
|
||||||
|
done
|
||||||
|
|
||||||
|
PATH="${TEST_DIR}" \
|
||||||
|
add-synced-lyrics \
|
||||||
|
--dir "${TEST_DIR}" \
|
||||||
|
--dry-run "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
test_determine_inputs_bash_recurse() {
|
||||||
|
test_determine_inputs_bash --recurse
|
||||||
|
}
|
||||||
|
|
||||||
|
test_determine_inputs_find() {
|
||||||
|
TEST_ADD_BIN='find' test_determine_inputs_bash
|
||||||
|
}
|
||||||
|
|
||||||
|
test_determine_inputs_find_recurse() {
|
||||||
|
TEST_ADD_BIN='find' test_determine_inputs_bash_recurse
|
||||||
|
}
|
||||||
|
|
||||||
test_file_dry() {
|
test_file_dry() {
|
||||||
|
setup_test_data
|
||||||
add-synced-lyrics --file "${GOOD_SONG}" --dry-run
|
add-synced-lyrics --file "${GOOD_SONG}" --dry-run
|
||||||
}
|
}
|
||||||
|
|
||||||
test_directory_dry() {
|
test_directory_dry() {
|
||||||
|
setup_test_data
|
||||||
add-synced-lyrics --dir "${TEST_DIR}" --dry-run
|
add-synced-lyrics --dir "${TEST_DIR}" --dry-run
|
||||||
}
|
}
|
||||||
|
|
||||||
test_directory_recurse_dry() {
|
test_directory_recurse_dry() {
|
||||||
|
setup_test_data
|
||||||
add-synced-lyrics --dir "${TEST_DIR}" --recurse --dry-run
|
add-synced-lyrics --dir "${TEST_DIR}" --recurse --dry-run
|
||||||
}
|
}
|
||||||
|
|
||||||
test_good_file() {
|
test_good_file() {
|
||||||
add-synced-lyrics --file "${GOOD_SONG}"
|
setup_test_data
|
||||||
|
add-synced-lyrics --file "${GOOD_SONG}" --debug
|
||||||
test -f "${GOOD_LYRICS}"
|
test -f "${GOOD_LYRICS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
test_bad_file() {
|
test_good_file_embed() {
|
||||||
|
test_good_file
|
||||||
|
# rm "${GOOD_LYRICS}"
|
||||||
|
add-synced-lyrics --file "${GOOD_SONG}" --embed
|
||||||
run_test_cmd \
|
run_test_cmd \
|
||||||
1 \
|
1 \
|
||||||
add-synced-lyrics --file "${BAD_SONG}"
|
test -f "${GOOD_LYRICS}"
|
||||||
|
|
||||||
|
# do it again to test that embedded is picked up and valid
|
||||||
|
run_test_cmd \
|
||||||
|
0 \
|
||||||
|
add-synced-lyrics --file "${GOOD_SONG}" --embed
|
||||||
|
[[ "${CMD_OUTPUT}" == *'already has lyrics, skipping'* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
test_bad_file() {
|
||||||
|
setup_test_data
|
||||||
|
run_test_cmd \
|
||||||
|
1 \
|
||||||
|
add-synced-lyrics \
|
||||||
|
--file "${BAD_SONG}" \
|
||||||
|
--debug
|
||||||
run_test_cmd \
|
run_test_cmd \
|
||||||
1 \
|
1 \
|
||||||
test -f "${BAD_LYRICS}"
|
test -f "${BAD_LYRICS}"
|
||||||
@@ -196,6 +262,33 @@ test_directory_recurse_ignore() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_not_music_file() {
|
||||||
|
local notMusicFile="${TEST_DIR}/dummy.txt"
|
||||||
|
test -f "${notMusicFile}" || touch "${notMusicFile}"
|
||||||
|
|
||||||
|
run_test_cmd \
|
||||||
|
1 \
|
||||||
|
add-synced-lyrics --file "${notMusicFile}"
|
||||||
|
|
||||||
|
[[ "${CMD_OUTPUT}" == *'is not a music file'* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
test_missing_utils() {
|
||||||
|
test -d "${TEST_DIR}" || setup_test_data
|
||||||
|
|
||||||
|
for bin in bash readlink; do
|
||||||
|
test -f "${TEST_DIR}/${bin}" && rm "${TEST_DIR}/${bin}"
|
||||||
|
ln -s "$(command -v ${bin})" "${TEST_DIR}/${bin}"
|
||||||
|
done
|
||||||
|
|
||||||
|
# chmod +x "${TEST_DIR}/bash"
|
||||||
|
PATH="${TEST_DIR}" run_test_cmd \
|
||||||
|
1 \
|
||||||
|
add-synced-lyrics
|
||||||
|
|
||||||
|
[[ "${CMD_OUTPUT}" == *'missing node'* ]]
|
||||||
|
}
|
||||||
|
|
||||||
setup_test_data() {
|
setup_test_data() {
|
||||||
test -d "${TEST_DIR}" && rm -rf "${TEST_DIR}"
|
test -d "${TEST_DIR}" && rm -rf "${TEST_DIR}"
|
||||||
mkdir -p "${TEST_DIR}/subdir"
|
mkdir -p "${TEST_DIR}/subdir"
|
||||||
@@ -208,24 +301,32 @@ setup_test_data() {
|
|||||||
-t 5
|
-t 5
|
||||||
)
|
)
|
||||||
"${ffmpegCmd[@]}" \
|
"${ffmpegCmd[@]}" \
|
||||||
-metadata ISRC=USTN10700139 \
|
-metadata ISRC=${GOOD_ISRC} \
|
||||||
"${GOOD_SONG}"
|
"${GOOD_SONG}"
|
||||||
"${ffmpegCmd[@]}" \
|
"${ffmpegCmd[@]}" \
|
||||||
-metadata ISRC=US5261822978 \
|
-metadata ISRC=${BAD_ISRC} \
|
||||||
"${BAD_SONG}"
|
"${BAD_SONG}"
|
||||||
}
|
}
|
||||||
|
|
||||||
TESTS=(
|
TESTS=(
|
||||||
|
test_help_option
|
||||||
test_determine_inputs
|
test_determine_inputs
|
||||||
test_determine_inputs_recurse
|
test_determine_inputs_recurse
|
||||||
|
test_determine_inputs_bash
|
||||||
|
test_determine_inputs_bash_recurse
|
||||||
|
test_determine_inputs_find
|
||||||
|
test_determine_inputs_find_recurse
|
||||||
|
test_not_music_file
|
||||||
test_bad_input
|
test_bad_input
|
||||||
test_directory_dry
|
test_directory_dry
|
||||||
test_directory_recurse_dry
|
test_directory_recurse_dry
|
||||||
test_file_dry
|
test_file_dry
|
||||||
test_good_file
|
test_good_file
|
||||||
|
test_good_file_embed
|
||||||
test_bad_file
|
test_bad_file
|
||||||
test_directory_recurse
|
test_directory_recurse
|
||||||
test_directory_recurse_ignore
|
test_directory_recurse_ignore
|
||||||
|
test_missing_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
# make sure no test is accidentally missed
|
# make sure no test is accidentally missed
|
||||||
@@ -338,7 +439,10 @@ if [[ $# -eq 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echo
|
||||||
analyze_coverage
|
analyze_coverage
|
||||||
else
|
else
|
||||||
|
setup_coverage
|
||||||
"$@"
|
"$@"
|
||||||
|
exit $?
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user