Skip to content

Commit 8f89c55

Browse files
authored
perf(local): avoid duplicate parsing of VideoThumbPos (#7812)
* feat(local): support percent for video thumbnail The percentage determines the point in the video (as a percentage of the total duration) at which the thumbnail will be generated. * feat(local): support both time and percent for video thumbnail * refactor(local): avoid duplicate parsing of VideoThumbPos
1 parent b449312 commit 8f89c55

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

drivers/local/driver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type Local struct {
3535
// zero means no limit
3636
thumbConcurrency int
3737
thumbTokenBucket TokenBucket
38+
39+
// video thumb position
40+
videoThumbPos float64
41+
videoThumbPosIsPercentage bool
3842
}
3943

4044
func (d *Local) Config() driver.Config {
@@ -92,6 +96,8 @@ func (d *Local) Init(ctx context.Context) error {
9296
if val < 0 || val > 100 {
9397
return fmt.Errorf("invalid video_thumb_pos value: %s, the precentage must be a number between 0 and 100", d.VideoThumbPos)
9498
}
99+
d.videoThumbPosIsPercentage = true
100+
d.videoThumbPos = val / 100
95101
} else {
96102
val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
97103
if err != nil {
@@ -100,6 +106,8 @@ func (d *Local) Init(ctx context.Context) error {
100106
if val < 0 {
101107
return fmt.Errorf("invalid video_thumb_pos value: %s, the time must be a positive number", d.VideoThumbPos)
102108
}
109+
d.videoThumbPosIsPercentage = false
110+
d.videoThumbPos = val
103111
}
104112
return nil
105113
}

drivers/local/util.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,14 @@ func (d *Local) GetSnapshot(videoPath string) (imgData *bytes.Buffer, err error)
6161
}
6262

6363
var ss string
64-
if strings.HasSuffix(d.VideoThumbPos, "%") {
65-
percentage, err := strconv.ParseFloat(strings.TrimSuffix(d.VideoThumbPos, "%"), 64)
66-
if err != nil {
67-
return nil, err
68-
}
69-
ss = fmt.Sprintf("%f", totalDuration*percentage/100)
64+
if d.videoThumbPosIsPercentage {
65+
ss = fmt.Sprintf("%f", totalDuration*d.videoThumbPos)
7066
} else {
71-
val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
72-
if err != nil {
73-
return nil, err
74-
}
7567
// If the value is greater than the total duration, use the total duration
76-
if val > totalDuration {
68+
if d.videoThumbPos > totalDuration {
7769
ss = fmt.Sprintf("%f", totalDuration)
7870
} else {
79-
ss = d.VideoThumbPos
71+
ss = fmt.Sprintf("%f", d.videoThumbPos)
8072
}
8173
}
8274

0 commit comments

Comments
 (0)