Skip to content

Commit

Permalink
Add the reading of the frames and duration to track play time
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinmartian committed Nov 12, 2023
1 parent 3789d9c commit 209ce22
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions player/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.3
require (
github.com/bogem/id3v2 v1.2.0
github.com/prometheus/client_golang v1.17.0
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300
)

require (
Expand Down
2 changes: 2 additions & 0 deletions player/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300 h1:XQdibLKagjdevRB6vAjVY4qbSr8rQ610YzTkWcxzxSI=
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300/go.mod h1:FNa/dfN95vAYCNFrIKRrlRo+MBLbwmR9Asa5f2ljmBI=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
60 changes: 59 additions & 1 deletion player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
Expand All @@ -13,11 +14,13 @@ import (
"github.com/bogem/id3v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tcolgate/mp3"
)

var (
mu sync.Mutex
lastPlayedID string
lastStartTime time.Time
timeout = 3500 * time.Millisecond
timeoutTimer *time.Timer
jsonData map[string]map[string]interface{}
Expand Down Expand Up @@ -65,6 +68,49 @@ func init() {
)
}

// Function to set the start time
func setStartTime() time.Time {
startTime := time.Now()
fmt.Println("Start time set:", startTime)
return startTime
}

// Function to get the duration since the start time
func durationSinceStart(startTime time.Time) time.Duration {
return time.Since(startTime)
}

func getFramecount(track string) (float64, int) {
t := 0.0
frameCount := 0

r, err := os.Open(track)
if err != nil {
fmt.Println(err)
return 0, 0
}

d := mp3.NewDecoder(r)
var f mp3.Frame
skipped := 0

for {

if err := d.Decode(&f, &skipped); err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return 0, 0
}
// fmt.Println(f.Header().BitRate())
t = t + f.Duration().Seconds()
frameCount++
}

return t, frameCount
}

func readID3(filepath string) mp3Data {
fmt.Println(filepath)
tag, err := id3v2.Open(filepath, id3v2.Options{Parse: true})
Expand Down Expand Up @@ -152,7 +198,9 @@ func playHandler(w http.ResponseWriter, r *http.Request) {
lastPlayedID = currentID
} else {
// If the same ID is requested again
fmt.Printf("Received the same ID again (ID: %s). Current track remains unchanged.\n", currentID)
// fmt.Printf("Received the same ID again (ID: %s). Current track remains unchanged.\n", currentID)
durationSince := durationSinceStart(lastStartTime)
fmt.Println("durationSinceStart:", durationSince.Seconds())
// Reset the timer
if timeoutTimer != nil {
timeoutTimer.Stop()
Expand Down Expand Up @@ -211,6 +259,16 @@ func playHandler(w http.ResponseWriter, r *http.Request) {
}
trackPath := "../" + filePath
id3_info := readID3(trackPath)
lastStartTime = setStartTime()
go func() {
// Call the function with the track path
duration, count := getFramecount(trackPath)

// Print the results
fmt.Printf("Duration=%.2f seconds, Frame count=%d\n", duration, count)
}()
// duration, frames := getFramecount(trackPath)
// fmt.Printf("frames: %d - duration %f seconds", frames, duration)
// Increment the playsCounter metric for the current track ID.
playsCounter.WithLabelValues(currentID, id3_info.EpisodeTitle).Inc()
podcastCounter.WithLabelValues(id3_info.PodcastTitle).Inc()
Expand Down

0 comments on commit 209ce22

Please sign in to comment.