-
Notifications
You must be signed in to change notification settings - Fork 6
/
mlc
executable file
·103 lines (90 loc) · 1.97 KB
/
mlc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env zsh
# mlc - Media Length Cache
# store length with timestamps in a cache
#
# functions that end in _ are dangerous and
# intended for internal use only
#
# normally, the only thing you need to do is
# import,
# . mlc
# then run,
# mlcLookup $path
# which yields the duration in seconds of a
# media file.
#
# media files can be in any format supported
# by mplayer; any file that mplayer cannot
# determine the length of will appear as zero
MLC_POLL_DUR="0.1"
[ -e ~/.mlc ] || touch ~/.mlc
function mlcGetLength() {
(mplayer -vo null -ao null -identify -frames 0 "$@" | (grep '^ID_LENGTH=[0-9\.]*$' || echo 0) | sed 's/^ID_LENGTH*=//' )2>/dev/null
}
function mlcPath2Key_() {
path="$@"
echo $path | md5sum | cut -d ' ' -f 1
}
function mlcLockEngage_() {
while [ -e ~/.mlc.lock ] ; do
sleep $MLC_POLL_DUR
done
touch ~/.mlc.lock
}
function mlcLockDisengage_() {
rm ~/.mlc.lock
}
function mlcReplaceDB_() {
cat > ~/.mlc.bak
mv ~/.mlc{.bak,}
}
function mlcEvict_() {
mlcLockEngage_
grep -v "^$2 " ~/.mlc | mlcReplaceDB_
mlcLockDisengage_
}
function mlcEvict() {
path=$(realpath "$@")
mlcEvict_ "$path" `mlcPath2Pat "$path"`
}
function mlcLookupCache_() {
grep "^$1 " ~/.mlc
}
function mlcEnter_() {
path="$1"
pat="$2"
mlcLockEngage_
date=`stat -c '%Y' "$path"`
length=`mlcGetLength "$path"`
(
echo "$pat $date $length"
cat ~/.mlc
) | mlcReplaceDB_
mlcLockDisengage_
}
function mlcLookup_() {
path="$1"
pat="$2"
cached="$(mlcLookupCache_ $pat)"
[ -z "$cached" ] || {
date=$(echo "$cached" | cut -d\ -f 2)
date2=`stat -c '%Y' "$path"`
([ -z "$date" ] || [ $date -lt $date2 ] ) && {
mlcEvict_ "$path" "$pat"
cached="$(mlcLookupCache_ $pat)"
}
}
[ -z "$cached" ] && {
mlcEnter_ "$path" "$pat"
cached="$(mlcLookupCache_ $pat)"
}
echo "$cached" | cut -d\ -f 3
}
function mlcLookup() {
[ -e ~/.mlc ] || touch ~/.mlc
path="$@"
[ -e "$path" ] && {
path=`realpath "$path"`
mlcLookup_ "$path" "$(mlcPath2Key_ $path)"
} || echo 0
}