Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(local): avoid duplicate parsing of VideoThumbPos #7812

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Local struct {
// zero means no limit
thumbConcurrency int
thumbTokenBucket TokenBucket

// video thumb position
videoThumbPos float64
videoThumbPosIsPercentage bool
}

func (d *Local) Config() driver.Config {
Expand Down Expand Up @@ -92,6 +96,8 @@ func (d *Local) Init(ctx context.Context) error {
if val < 0 || val > 100 {
return fmt.Errorf("invalid video_thumb_pos value: %s, the precentage must be a number between 0 and 100", d.VideoThumbPos)
}
d.videoThumbPosIsPercentage = true
d.videoThumbPos = val / 100
} else {
val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
if err != nil {
Expand All @@ -100,6 +106,8 @@ func (d *Local) Init(ctx context.Context) error {
if val < 0 {
return fmt.Errorf("invalid video_thumb_pos value: %s, the time must be a positive number", d.VideoThumbPos)
}
d.videoThumbPosIsPercentage = false
d.videoThumbPos = val
}
return nil
}
Expand Down
16 changes: 4 additions & 12 deletions drivers/local/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,14 @@ func (d *Local) GetSnapshot(videoPath string) (imgData *bytes.Buffer, err error)
}

var ss string
if strings.HasSuffix(d.VideoThumbPos, "%") {
percentage, err := strconv.ParseFloat(strings.TrimSuffix(d.VideoThumbPos, "%"), 64)
if err != nil {
return nil, err
}
ss = fmt.Sprintf("%f", totalDuration*percentage/100)
if d.videoThumbPosIsPercentage {
ss = fmt.Sprintf("%f", totalDuration*d.videoThumbPos)
} else {
val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
if err != nil {
return nil, err
}
// If the value is greater than the total duration, use the total duration
if val > totalDuration {
if d.videoThumbPos > totalDuration {
ss = fmt.Sprintf("%f", totalDuration)
} else {
ss = d.VideoThumbPos
ss = fmt.Sprintf("%f", d.videoThumbPos)
}
}

Expand Down
Loading