Skip to content

Commit 6a50810

Browse files
gferrarogferraro
andauthored
Add wav handler (#50)
* pick up .wav files * add wav handler * only stop trying if get a url error --------- Co-authored-by: gferraro <g.ferraro22@gmail.com>
1 parent f900eaa commit 6a50810

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

cmd/thermal-uploader/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/rand"
23+
"net/url"
2224
"os"
2325
"path/filepath"
2426
"time"
@@ -43,7 +45,7 @@ const (
4345

4446
var log = logging.NewLogger("info")
4547
var version = "No version provided"
46-
var globs = [4]string{"*.cptv", "*.avi", "*.mp4", "*.aac"}
48+
var globs = [5]string{"*.cptv", "*.avi", "*.mp4", "*.aac", "*.wav"}
4749

4850
type Args struct {
4951
ConfigDir string `arg:"-c,--config" help:"path to configuration directory"`
@@ -214,6 +216,8 @@ func retryFailedUploads(apiClient *api.CacophonyAPI, directory string) bool {
214216
}
215217
// start at a random index to avoid always failing on the same file
216218
startIndex := rand.Intn(len(matches))
219+
var urlError *url.Error
220+
217221
for i := 0; i < len(matches); i++ {
218222
index := (startIndex + i) % len(matches)
219223
filename := matches[index]
@@ -222,7 +226,11 @@ func retryFailedUploads(apiClient *api.CacophonyAPI, directory string) bool {
222226

223227
if err != nil {
224228
log.Printf("Uploading still failing to upload %v: %v", filename, err)
225-
return false
229+
230+
// any http requrest error will be caught here, i think if not http error should try all other files
231+
if errors.As(err, &urlError) {
232+
return false
233+
}
226234
}
227235
log.Print("success uploading failed items")
228236
}

cmd/thermal-uploader/uploadjob.go

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ import (
1414
api "github.com/TheCacophonyProject/go-api"
1515
)
1616

17+
var timeLayouts = [3]string{"2006-01-02--15-04-05", "20060102-150405.000000", "20060102-150405"}
18+
1719
type uploadJob struct {
1820
filename string
1921
metafile string
2022
recID int
2123
hasMetaData bool
2224
duration int
25+
dateParsed bool
26+
recDate time.Time
2327
}
2428

2529
func (u *uploadJob) requiresConversion() bool {
26-
return filepath.Ext(u.filename) == ".avi"
30+
return filepath.Ext(u.filename) == ".avi" || filepath.Ext(u.filename) == ".wav"
2731
}
2832

2933
func (u *uploadJob) isIR() bool {
3034
return filepath.Ext(u.filename) == ".avi" || filepath.Ext(u.filename) == ".mp4"
3135
}
3236

3337
func (u *uploadJob) isAudio() bool {
34-
return filepath.Ext(u.filename) == ".aac"
38+
return filepath.Ext(u.filename) == ".wav" || filepath.Ext(u.filename) == ".aac"
3539
}
3640

3741
func (u *uploadJob) isThermal() bool {
@@ -51,12 +55,61 @@ func (u *uploadJob) fileType() string {
5155
func (u *uploadJob) convert() error {
5256
if !u.requiresConversion() {
5357
return nil
58+
} else if u.isAudio() {
59+
return u.convertAudio()
5460
} else if u.isIR() {
5561
return u.convertMp4()
5662
}
5763
return nil
5864
}
5965

66+
func (u *uploadJob) convertAudio() error {
67+
var extension = filepath.Ext(u.filename)
68+
69+
var name = u.filename[0:len(u.filename)-len(extension)] + ".aac"
70+
var duration = fmt.Sprintf("duration=%d", u.duration)
71+
args := []string{"-y", // Yes to all
72+
"-i", u.filename,
73+
"-codec:a", "aac",
74+
"-b:a", "128k",
75+
"-q:a",
76+
"1.2",
77+
"-aac_coder",
78+
"fast",
79+
"-movflags",
80+
"faststart",
81+
"-movflags",
82+
"+use_metadata_tags",
83+
"-map_metadata",
84+
"0"}
85+
86+
if u.dateParsed {
87+
var recDateTime = fmt.Sprintf("recordingDateTime=%s", u.recDate.Format(time.RFC3339))
88+
args = append(args,
89+
"-metadata",
90+
recDateTime)
91+
}
92+
args = append(args,
93+
"-metadata",
94+
duration,
95+
"-f",
96+
"mp4", name)
97+
98+
cmd := exec.Command("ffmpeg", args...)
99+
cmd.Stderr = os.Stderr
100+
cmd.Stdout = os.Stdout
101+
102+
err := cmd.Run()
103+
if err != nil {
104+
return err
105+
}
106+
if err := os.Remove(u.filename); err != nil {
107+
log.Printf("warning: failed to delete %s: %v", u.filename, err)
108+
}
109+
110+
u.filename = name
111+
return nil
112+
}
60113
func metaFileExists(filename string) (bool, string) {
61114
metafile := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".txt"
62115
if _, err := os.Stat(metafile); err != nil {
@@ -67,7 +120,7 @@ func metaFileExists(filename string) (bool, string) {
67120

68121
func newUploadJob(filename string) *uploadJob {
69122
exists, name := metaFileExists(filename)
70-
u := &uploadJob{filename: filename, metafile: name, hasMetaData: exists, duration: 0}
123+
u := &uploadJob{filename: filename, metafile: name, hasMetaData: exists, duration: 0, dateParsed: false}
71124
return u
72125
}
73126

@@ -82,8 +135,25 @@ func (u *uploadJob) delete() {
82135
}
83136
}
84137
}
138+
139+
func (u *uploadJob) parseDateTime() error {
140+
for _, layout := range timeLayouts {
141+
dt, err := parseDateTime(u.filename, layout, false)
142+
if err == nil {
143+
u.recDate = dt
144+
u.dateParsed = true
145+
break
146+
}
147+
}
148+
return fmt.Errorf("could not parse date time")
149+
}
150+
85151
func (u *uploadJob) preprocess() error {
86-
err := u.setDuration()
152+
err := u.parseDateTime()
153+
if err != nil {
154+
log.Printf("Error getting datetime %v\n", err)
155+
}
156+
err = u.setDuration()
87157
if err != nil {
88158
log.Printf("Error getting duration %v\n", err)
89159
}
@@ -119,7 +189,8 @@ func (u *uploadJob) setDuration() error {
119189
if u.isThermal() {
120190
return nil
121191
}
122-
if u.isAudio() {
192+
//duration already set if converted
193+
if u.isAudio() && !u.requiresConversion() {
123194
return nil
124195
}
125196

@@ -171,11 +242,9 @@ func (u *uploadJob) uploadFile(apiClient *api.CacophonyAPI) (int, error) {
171242
data := map[string]interface{}{
172243
"type": u.fileType(),
173244
}
174-
if u.isIR() {
175-
const layout = "20060102-150405.000000"
176-
dt, err := parseDateTime(u.filename, layout, false)
177-
if err == nil {
178-
data["recordingDateTime"] = dt.Format(time.RFC3339)
245+
if u.isIR() || u.isAudio() {
246+
if u.dateParsed {
247+
data["recordingDateTime"] = u.recDate.Format(time.RFC3339)
179248
}
180249
}
181250
if u.duration > 0 {

0 commit comments

Comments
 (0)