-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplaylist
executable file
·65 lines (55 loc) · 2.24 KB
/
playlist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash -u
# Concatenates all the songs in one of my plaintext playlists (https://github.com/seanbreckenridge/plaintext-playlist)
# and syncs the one file to my server.
#
# I often use this to sync a playlist Im listening to before leaving the house
# so I can listen to it on my phone.
#
# Requires: ./remsync, ffmpeg, plainplay, realpath
VPS_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
# declare file locations
PLAYLIST_DIR="$(plainplay playlistdir)" || exit $?
readonly PLAYLIST_MP3="${HOME}/.cache/playlist.mp3"
readonly PLAYLIST_TMP_DIR="${HOME}/.cache/playlist_tmp/"
readonly PLAYLIST_RAW='/tmp/raw_playlist.txt'
readonly PLAYLIST_TXT='/tmp/playlist.txt'
# clear previous runs
rm -f "$PLAYLIST_TXT" "$PLAYLIST_MP3" "$PLAYLIST_RAW"
[[ -d "$PLAYLIST_TMP_DIR" ]] && rm -rf "$PLAYLIST_TMP_DIR"
mkdir -p "$PLAYLIST_TMP_DIR"
# get playlists to sync from user
cd "$PLAYLIST_DIR" || exit $? # so xargs cat works
CHOSEN_SONGS="$(find "$PLAYLIST_DIR" -type f -printf '%f\n' | fzf -m -i --prompt="Select the playlists to concatenate..." | xargs cat)" || exit $?
[[ -z "$CHOSEN_SONGS" ]] && {
echo "You didn't choose any playlists" >&2
exit 1
}
# dont have to check if PLAINTEXT_PLAYLIST_MUSIC_DIR is set because the plainplay call
# above would have exited if it wasn't set
# shuffle songs, make the files absolute paths
shuf <<<"$CHOSEN_SONGS" | sed -e "s|^|${PLAINTEXT_PLAYLIST_MUSIC_DIR}/|" >"$PLAYLIST_RAW"
# copy all those files to simple filenames, as to not break ffmpeg quoting
# convert any files that aren't mp3 to mp3
declare i=0
while IFS= read -r song; do
((i++))
ext="${song##*.}"
newfp="${PLAYLIST_TMP_DIR}${i}.${ext}"
# if its not mp3, convert it to mp3
if [[ "$ext" != "mp3" ]]; then
newfp="${PLAYLIST_TMP_DIR}${i}.mp3"
echo "Converting non-mp3 file: ${song} -> ${newfp}"
ffmpeg -loglevel warning -i "$song" -f mp3 -hide_banner "$newfp" </dev/null || exit $?
else
echo "File is already an mp3: ${song}"
cp -v "$song" "$newfp"
fi
printf "file '%s'\n" "$newfp" >>"$PLAYLIST_TXT"
done <"$PLAYLIST_RAW"
set -e
# use PLAYLIST_TXT to combine all the mp3's together
ffmpeg -hide_banner -f concat -safe 0 -i "$PLAYLIST_TXT" -c copy "$PLAYLIST_MP3"
# cd to this directory
cd "$VPS_DIR"
# call remsync to sync it to the server
./remsync "$PLAYLIST_MP3"