-
Notifications
You must be signed in to change notification settings - Fork 1
/
fingerprint.go
58 lines (46 loc) · 1.4 KB
/
fingerprint.go
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
// Copyright © 2013-2018 Pierre Neidhardt <ambrevar@gmail.com>
// Use of this file is governed by the license that can be found in LICENSE.
package main
// TODO: Use "github.com/go-fingerprint/fingerprint"?
// Package seems broken as of 2015.12.01.
// This would be more resilient to upstream library change, e.g. when
// chromaprint 1.4 removed the filename from its output.
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strconv"
)
func fingerprint(file string) (fingerprint string, duration int, err error) {
if _, err := exec.LookPath("fpcalc"); err != nil {
return "", 0, errors.New("fpcalc not found")
}
cmd := exec.Command("fpcalc", file)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return "", 0, fmt.Errorf("fingerprint: %s", stderr.String())
}
// 'out' must of the form:
// ...
// DURATION=
// FINGERPRINT=
// ...
for !bytes.HasPrefix(out, []byte("DURATION")) {
out = out[bytes.IndexByte(out, '\n')+1:]
}
var durationOutput []byte = out[bytes.IndexByte(out, '=')+1:]
durationOutput = durationOutput[:bytes.IndexByte(durationOutput, '\n')]
for !bytes.HasPrefix(out, []byte("FINGERPRINT")) {
out = out[bytes.IndexByte(out, '\n')+1:]
}
out = out[bytes.IndexByte(out, '=')+1:]
out = out[:bytes.IndexByte(out, '\n')]
duration, err = strconv.Atoi(string(durationOutput))
if err != nil {
return "", 0, err
}
return string(out), duration, nil
}