Skip to content

Commit

Permalink
Merge pull request #47 from TheCacophonyProject/remove-wav-handling
Browse files Browse the repository at this point in the history
- Remove functionality for converting .wav files, and rely on .aac fi…
  • Loading branch information
hardiesoft authored Oct 3, 2024
2 parents 747c562 + 5c0e08e commit cc9684c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
40 changes: 31 additions & 9 deletions cmd/thermal-uploader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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.
}
}
}
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 6 additions & 34 deletions cmd/thermal-uploader/uploadjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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 {
return filepath.Ext(u.filename) == ".avi" || filepath.Ext(u.filename) == ".mp4"
}

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 {
Expand All @@ -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()
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -200,19 +177,14 @@ 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
}
if meta != nil {
data["metadata"] = meta
}
data["filename"] = u.filename

err = u.convert()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ 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=
github.com/TheCacophonyProject/go-config v1.9.1 h1:TCeogtNYg5eHx2q97DQ1B+RsbjacW+jr7h1TCv1FpAk=
Expand Down

0 comments on commit cc9684c

Please sign in to comment.