Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions impd
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ filter_tracks() {
}

preferred_languages() {
local pref_langs=${langs,,}
local pref_langs=$(echo "${langs}" | tr '[:upper:]' '[:lower:]')
echo "${pref_langs//,/$'\n'}"
}

guess_track_priority() {
# Sets some numeric weight for track. Used when sorting tracks.
local -r track_lang=${1,,} track_title=${2,,}
local -r track_lang=$(echo "${1}" | tr '[:upper:]' '[:lower:]')
local -r track_title=$(echo "${2}" | tr '[:upper:]' '[:lower:]')
local weight=0

# impd wants to use full subtitle tracks and avoid other tracks if possible.
Expand Down Expand Up @@ -161,7 +162,7 @@ best_track() {
if [[ -n $tracks ]]; then
while IFS=$'\t' read -r track_num track_lang track_title; do
printf -- '%d\t%d\n' "$track_num" "$(guess_track_priority "$track_lang" "$track_title")"
done <<<"$tracks" | sort --stable -g -k 2 -t $'\t' | cut -f1 | head -1
done <<<"$tracks" | sort -g -k 2 -t $'\t' | cut -f1 | head -1
fi
}

Expand Down Expand Up @@ -239,18 +240,25 @@ fetch_title() {
echo "$title"
}

_grep() {
if command -v ggrep >/dev/null 2>&1; then
ggrep "$@"
else
grep "$@"
fi
}

fetch_episode_number() {
local -r file=${1:?}
{
echo 01
grep -Pio '\[\K\d+(?=\])' <<<"${file%.*}"
sed \
-Ee 's/(\[|\()[^])]*(\]|\))//g' \
-Ee 's/[0-9]{3,4}[pP]//g' \
-Ee 's/(19|20)[0-9]{2}//g' \
-Ee 's/v[.0-9-]{1,4}//g' \
-e 's/_/ /g' <<<"${file%.*}" |
grep -Pio '(?<=[##. pe-])[[:digit:]]{1,3}\b'
_grep -Eio '[##. pe-][[:digit:]]{1,3}\b' | _grep -Eo '[[:digit:]]{1,3}'
} | tail -1
}

Expand Down Expand Up @@ -373,7 +381,7 @@ make_output_basename() {
make_uncondensed() {
local -r input=${1:?}
local -r base=$( make_output_basename "$input" )
local -r job_dir=$(mktemp -d --tmpdir="$tmp_dir" -t "make_uncondensed.job-XXXX")
local -r job_dir=$(mktemp -d "$tmp_dir/make_uncondensed.job-XXXX")
local -r temp_audio=$job_dir/$base.ogg
local -r output=$immersionpod_dir/$current/$base.ogg

Expand Down Expand Up @@ -421,7 +429,7 @@ find_external_subtitles() {

if [[ -z $dir ]] || [[ ! -d $dir ]]; then
echo "Error: video directory '$dir' is invalid." >&2
exit 1
return 1
fi

find "$dir" \
Expand All @@ -430,7 +438,7 @@ find_external_subtitles() {
-regextype posix-extended \
-iregex '.*\.(ass|srt)$' \
-print0 |
grep -Fiz --max-count=1 "$video" |
_grep -Fiz --max-count=1 "$video" |
tr '\0' '\n' # fix no newline at the end
}

Expand Down Expand Up @@ -629,12 +637,12 @@ make_condensed() {
local -r video=$( canonicalize "${1:?Video path not set.}" ) # convert to full path
local -r base=$( make_output_basename "$video" )

local -r job_dir=$(mktemp -d --tmpdir="$tmp_dir" -t "$base.job-XXXX")
local -r job_dir=$(mktemp -d "$tmp_dir/$base.job-XXXX")

local -r temp_audio=$job_dir/$base.ogg
local -r output=$( canonicalize "${2:-$immersionpod_dir/$current/$base.ogg}" )

local -r chunks_dir=$(mktemp -d --tmpdir="$job_dir" -t "chunks-XXXX")
local -r chunks_dir=$(mktemp -d "$job_dir/chunks-XXXX")
local -r chunks_file=$job_dir/chunks.list

local -r subs_out=$job_dir/$base.srt
Expand Down Expand Up @@ -754,7 +762,7 @@ add_file() {

add_remote() {
local -r source_url=${1:?}
local -r job_dir=$(mktemp -d --tmpdir="$tmp_dir" -t "download.job-XXXX")
local -r job_dir=$(mktemp -d "$tmp_dir/download.job-XXXX")
(
cd -- "$job_dir" && curl -L -O "$source_url"
for file in "$job_dir"/*; do
Expand Down Expand Up @@ -837,7 +845,7 @@ add() {

file_can_be_added() {
local -r file=$1
grep -Pqv "$filename_skip_pattern" <<<"$file" && is_media "$file"
_grep -Eqv "$filename_skip_pattern" <<<"$file" && is_media "$file"
}

add_stdin() {
Expand Down Expand Up @@ -890,7 +898,7 @@ download_yt() {

add_yt() {
local -r source_url=${1:?}
local -r job_dir=$(mktemp -d --tmpdir="$tmp_dir" -t "youtube-dl.job-XXXX")
local -r job_dir=$(mktemp -d "$tmp_dir/youtube-dl.job-XXXX")

download_yt -o "$job_dir/%(uploader)s - %(title).60s.%(ext)s" "$source_url"

Expand Down Expand Up @@ -950,16 +958,23 @@ mkplaylist() {

find "$immersionpod_dir/$current" \
-type f \
-printf '%P\n' \
-regextype posix-extended \
-iregex '.*\.(mp3|opus|ogg|m4a|wav|wma)$' |
-iname '*.mp3' -o -iname '*.opus' -o -iname '*.ogg' -o -iname '*.m4a' -o -iname '*.wav' -o -iname '*.wma' |
shuf > "$m3u_path"
echo "created file '$m3u_path'"
}

grep_mpd_dir() {
# https://wiki.archlinux.org/index.php/Music_Player_Daemon#Configuration
grep -Pos 'music_directory\s*"?\K[^"]*(?="?)' -- ~/.config/mpd/mpd.conf
_grep -Pos 'music_directory\s*"?\K[^"]*(?="?)' -- ~/.config/mpd/mpd.conf 2>/dev/null
}

find_xdg_music_dir() {
# macOS doesn't have xdg-user-dir, so we'll use ~/Music as the default
if command -v xdg-user-dir >/dev/null 2>&1; then
xdg-user-dir MUSIC 2>/dev/null
else
echo "$HOME/Music"
fi
}

find_xdg_music_dir() {
Expand Down Expand Up @@ -1063,7 +1078,7 @@ version() {
read_config_file() {
if [[ -f $config_filepath ]]; then
# shellcheck source=/dev/null
source -- <(grep -xP '^[a-z_]+=.+$' -- "$config_filepath")
source -- <(_grep -xP '^[a-z_]*=.*$' -- "$config_filepath")
fi
}

Expand All @@ -1087,7 +1102,16 @@ ensure_dirs() {
"$tmp_dir"
}

check_macos_grep() {
# if running macOS, ggrep must be installed to support Perl-compatible regular expressions.
if [[ "$(uname)" == "Darwin" ]] && ! command -v ggrep >/dev/null 2>&1; then
echo "ggrep is not installed. install it with brew." >&2
exit 1
fi
}

main() {
check_macos_grep
# Load configuration file, then apply settings omitted in the config file.
read_config_file

Expand Down