-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlf-mplayer-wrapper.sh
executable file
·49 lines (44 loc) · 1.36 KB
/
lf-mplayer-wrapper.sh
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
#!/bin/bash
# Copyright 2010, 2011 Yu-Jie Lin
# BSD License
# Find real mplayer command
mplayer_cmd="$(which -a mplayer | grep 'usr.*bin' | head -1)"
if [[ -z "$mplayer_cmd" ]]; then
echo "Can not find mplayer!" >&2
exit 1
fi
# a file is played repeatly, happened with SMPlayer single file repeat, need
# msglevel 6 to know when new start happens.
[[ "${@:$#-2}" =~ '-loop '[0-9]+ ]] && is_loop=1 && msglevel='-msglevel all=6'
{ "$mplayer_cmd" "$@" $msglevel ; echo -n $'\r' ; } | while read -d $'\r' block; do
# Finding newly played file
while read line; do
if [[ $line =~ Playing.* ]]; then
new_file=${line:8:${#line}-9} && new_ts=$(date +%s)
elif [[ $is_loop -eq 1 && $line =~ 'loop_times = '.* ]]; then
# It's a new round
new_file="$last_file"
new_ts=$(date +%s)
elif [[ $line =~ Exiting.* ]]; then
[[ $line =~ .*'End of file'.* ]] && lf-scrobble.sh -s $last_ts "$last_file" &
echo
fi
done <<< "$block"
if [[ ! -z "$new_file" ]]; then
{
# Scrobbling last_file
# FIXME: If user skip file, the file is still submitted, it
# shouldn't be.
[[ ! -z "$last_file" ]] && lf-scrobble.sh -s $last_ts "$last_file"
# Submitting now playing on new_file
lf-scrobble.sh -n $new_ts "$new_file"
} &
last_file="$new_file"
last_ts="$new_ts"
new_file=
new_ts=
fi
echo -n "$block"$'\r'
done
rm "/tmp/lf-submit.sh.currentsong" &>/dev/null
echo