From 8c064caf1654bafa1cd57cf7b3c9fc8823ff948e Mon Sep 17 00:00:00 2001 From: jon Date: Thu, 3 Oct 2024 17:39:58 +1300 Subject: [PATCH 1/3] - Remove functionality for converting .wav files, and rely on .aac files having embedded metadata rather than the presence of an associated metadata.txt file. - Pass filename to upload api. - Handle some unhandled errors. --- cmd/thermal-uploader/main.go | 40 ++++++++++++++++++++------ cmd/thermal-uploader/uploadjob.go | 47 ++++++++----------------------- go.mod | 2 +- go.sum | 1 + 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/cmd/thermal-uploader/main.go b/cmd/thermal-uploader/main.go index 8c847f3..e837f0a 100644 --- a/cmd/thermal-uploader/main.go +++ b/cmd/thermal-uploader/main.go @@ -42,7 +42,7 @@ const ( var log = logging.NewLogger("info") var version = "No version provided" -var globs = [5]string{"*.cptv", "*.avi", "*.mp4", "*.wav", "*.aac"} +var globs = [5]string{"*.cptv", "*.avi", "*.mp4", "*.aac"} type Args struct { ConfigDir string `arg:"-c,--config" help:"path to configuration directory"` @@ -77,7 +77,10 @@ func runMain() error { cr := connrequester.NewConnectionRequester() log.Println("requesting internet connection") cr.Start() - cr.WaitUntilUpLoop(connectionTimeout, connectionRetryInterval, -1) + err := cr.WaitUntilUpLoop(connectionTimeout, connectionRetryInterval, -1) + if err != nil { + return err + } log.Println("internet connection made") apiClient, err := api.New() @@ -95,7 +98,12 @@ func runMain() error { } log.Println("Making failed uploads directory") - os.MkdirAll(filepath.Join(conf.Directory, failedUploadsDir), 0755) + { + err := os.MkdirAll(filepath.Join(conf.Directory, failedUploadsDir), 0755) + if err != nil { + return err + } + } log.Println("Watching", conf.Directory) fsEvents := make(chan notify.EventInfo, 1) @@ -108,11 +116,19 @@ func runMain() error { failedRetryAttempts := 0 for { - sendOnRequest(60) + err := sendOnRequest(60) + if err != nil { + return err + } // Check for files to upload first in case there are CPTV // files around when the uploader starts. cr.Start() - cr.WaitUntilUpLoop(connectionTimeout, connectionRetryInterval, -1) + { + err := cr.WaitUntilUpLoop(connectionTimeout, connectionRetryInterval, -1) + if err != nil { + return err + } + } if err = uploadFiles(apiClient, conf.Directory); err != nil { return err } @@ -136,9 +152,12 @@ func runMain() error { // A new file was added during the last iteration, loop again. case <-time.After(time.Second): // No new file was added, then: - sendFinished() // Tell tc2-hat-attiny that we are all done. - cr.Stop() // Stop requesting an internet connection. - <-fsEvents // Wait for a new file to be added. + err := sendFinished() + if err != nil { + return err + } // Tell tc2-hat-attiny that we are all done. + cr.Stop() // Stop requesting an internet connection. + <-fsEvents // Wait for a new file to be added. } } } @@ -168,7 +187,10 @@ func uploadFiles(apiClient *api.CacophonyAPI, directory string) error { err = job.preprocess() if err != nil { log.Printf("Failed to preprocess %v: %v", filename, err) - job.moveToFailed() + err := job.moveToFailed() + if err != nil { + return err + } continue } err = uploadFileWithRetries(apiClient, job) diff --git a/cmd/thermal-uploader/uploadjob.go b/cmd/thermal-uploader/uploadjob.go index 56d6edf..0c5ddeb 100644 --- a/cmd/thermal-uploader/uploadjob.go +++ b/cmd/thermal-uploader/uploadjob.go @@ -23,7 +23,7 @@ type uploadJob struct { } func (u *uploadJob) requiresConversion() bool { - return filepath.Ext(u.filename) == ".avi" || filepath.Ext(u.filename) == ".wav" + return filepath.Ext(u.filename) == ".avi" } func (u *uploadJob) isIR() bool { @@ -31,7 +31,7 @@ func (u *uploadJob) isIR() bool { } func (u *uploadJob) isAudio() bool { - return filepath.Ext(u.filename) == ".wav" || filepath.Ext(u.filename) == ".aac" + return filepath.Ext(u.filename) == ".aac" } func (u *uploadJob) isThermal() bool { @@ -48,35 +48,9 @@ func (u *uploadJob) fileType() string { return fileType } -func (u *uploadJob) convertAudio() error { - var extension = filepath.Ext(u.filename) - var name = u.filename[0:len(u.filename)-len(extension)] + ".aac" - cmd := exec.Command("ffmpeg", "-y", // Yes to all - "-i", u.filename, - "-codec:a", "aac", - "-b:a", "128k", - name, - ) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - - err := cmd.Run() - if err != nil { - return err - } - if err := os.Remove(u.filename); err != nil { - log.Printf("warning: failed to delete %s: %v", u.filename, err) - } - - u.filename = name - return nil -} - func (u *uploadJob) convert() error { if !u.requiresConversion() { return nil - } else if u.isAudio() { - return u.convertAudio() } else if u.isIR() { return u.convertMp4() } @@ -145,6 +119,9 @@ func (u *uploadJob) setDuration() error { if u.isThermal() { return nil } + if u.isAudio() { + return nil + } // ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4 cmd := exec.Command("ffprobe", @@ -200,12 +177,6 @@ func (u *uploadJob) uploadFile(apiClient *api.CacophonyAPI) (int, error) { if err == nil { data["recordingDateTime"] = dt.Format(time.RFC3339) } - } else if u.isAudio() { - const layout = "20060102-150405" - dt, err := parseDateTime(u.filename, layout, true) - if err == nil { - data["recordingDateTime"] = dt.Format(time.RFC3339) - } } if u.duration > 0 { data["duration"] = u.duration @@ -213,6 +184,7 @@ func (u *uploadJob) uploadFile(apiClient *api.CacophonyAPI) (int, error) { if meta != nil { data["metadata"] = meta } + data["filename"] = u.filename err = u.convert() if err != nil { @@ -222,7 +194,12 @@ func (u *uploadJob) uploadFile(apiClient *api.CacophonyAPI) (int, error) { if err != nil { return 0, err } - defer f.Close() + defer func(f *os.File) { + err := f.Close() + if err != nil { + + } + }(f) return apiClient.UploadVideo(bufio.NewReader(f), data) } diff --git a/go.mod b/go.mod index 46af89e..0200767 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/TheCacophonyProject/thermal-uploader go 1.22.3 require ( - github.com/TheCacophonyProject/go-api v1.0.4 + github.com/TheCacophonyProject/go-api v1.2.3 github.com/TheCacophonyProject/go-config v1.9.1 github.com/TheCacophonyProject/go-utils v0.1.3 github.com/TheCacophonyProject/modemd v1.11.0-tc2 diff --git a/go.sum b/go.sum index 6235963..676872f 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,7 @@ github.com/TheCacophonyProject/event-reporter/v3 v3.8.0/go.mod h1:WTppJtTBxduasM github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c/go.mod h1:FfMpa4cFhNXQ9tuKG18HO6yLExezcJhzjUjBOFocrQw= github.com/TheCacophonyProject/go-api v1.0.4 h1:eJoqy9xw1O5Ks9MyH2cEehobkj/HgB4ZrYSrvic60D0= github.com/TheCacophonyProject/go-api v1.0.4/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8= +github.com/TheCacophonyProject/go-api v1.2.3/go.mod h1:innR3kf5xnua2wbnLvOudI13j2TU1sGY1dxJQJLRZkI= github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88/go.mod h1:gPUJLVu408NRz9/P3BrsxzOzLc+KJLrv+jVdDw3RI0Y= github.com/TheCacophonyProject/go-config v1.9.0/go.mod h1:+y80PSRZudMYuVrYTGOvzc66NxVJWKS4TuU442vmvhY= github.com/TheCacophonyProject/go-config v1.9.1 h1:TCeogtNYg5eHx2q97DQ1B+RsbjacW+jr7h1TCv1FpAk= From a1eb50deec7d428bcd0d1882e74135d5e07dff84 Mon Sep 17 00:00:00 2001 From: jon Date: Thu, 3 Oct 2024 17:45:22 +1300 Subject: [PATCH 2/3] - Remove functionality for converting .wav files, and rely on .aac files having embedded metadata rather than the presence of an associated metadata.txt file. - Pass filename to upload api. - Handle some unhandled errors. --- cmd/thermal-uploader/uploadjob.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/thermal-uploader/uploadjob.go b/cmd/thermal-uploader/uploadjob.go index 0c5ddeb..c723a80 100644 --- a/cmd/thermal-uploader/uploadjob.go +++ b/cmd/thermal-uploader/uploadjob.go @@ -194,12 +194,7 @@ func (u *uploadJob) uploadFile(apiClient *api.CacophonyAPI) (int, error) { if err != nil { return 0, err } - defer func(f *os.File) { - err := f.Close() - if err != nil { - - } - }(f) + defer f.Close() return apiClient.UploadVideo(bufio.NewReader(f), data) } From 5c0e08e48b4066882bd35f72204fde44a2b26f55 Mon Sep 17 00:00:00 2001 From: jon Date: Thu, 3 Oct 2024 17:46:41 +1300 Subject: [PATCH 3/3] - Tidy --- go.sum | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 676872f..56171a7 100644 --- a/go.sum +++ b/go.sum @@ -55,8 +55,8 @@ github.com/TheCacophonyProject/event-reporter/v3 v3.3.0/go.mod h1:dGIYfhABsJHKjc github.com/TheCacophonyProject/event-reporter/v3 v3.8.0 h1:FYNR+MX5ypyG7UWpogDnDqsTV+23WS1sIr9o6rhjf/8= github.com/TheCacophonyProject/event-reporter/v3 v3.8.0/go.mod h1:WTppJtTBxduasM1Or5SAh4Mm0YrTDnprOChjnGYgyEI= github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c/go.mod h1:FfMpa4cFhNXQ9tuKG18HO6yLExezcJhzjUjBOFocrQw= -github.com/TheCacophonyProject/go-api v1.0.4 h1:eJoqy9xw1O5Ks9MyH2cEehobkj/HgB4ZrYSrvic60D0= github.com/TheCacophonyProject/go-api v1.0.4/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8= +github.com/TheCacophonyProject/go-api v1.2.3 h1:m+UpgU2/gYOPr80sCOi72ybnHHgVgfdIOIvkydU80TM= github.com/TheCacophonyProject/go-api v1.2.3/go.mod h1:innR3kf5xnua2wbnLvOudI13j2TU1sGY1dxJQJLRZkI= github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88/go.mod h1:gPUJLVu408NRz9/P3BrsxzOzLc+KJLrv+jVdDw3RI0Y= github.com/TheCacophonyProject/go-config v1.9.0/go.mod h1:+y80PSRZudMYuVrYTGOvzc66NxVJWKS4TuU442vmvhY=