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
pull/8417/head
Lin Tianchuan 2025-04-19 14:27:13 +08:00 committed by GitHub
parent b449312da8
commit 8f89c55aca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 12 deletions

View File

@ -35,6 +35,10 @@ type Local struct {
// zero means no limit // zero means no limit
thumbConcurrency int thumbConcurrency int
thumbTokenBucket TokenBucket thumbTokenBucket TokenBucket
// video thumb position
videoThumbPos float64
videoThumbPosIsPercentage bool
} }
func (d *Local) Config() driver.Config { func (d *Local) Config() driver.Config {
@ -92,6 +96,8 @@ func (d *Local) Init(ctx context.Context) error {
if val < 0 || val > 100 { 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) 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 { } else {
val, err := strconv.ParseFloat(d.VideoThumbPos, 64) val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
if err != nil { if err != nil {
@ -100,6 +106,8 @@ func (d *Local) Init(ctx context.Context) error {
if val < 0 { if val < 0 {
return fmt.Errorf("invalid video_thumb_pos value: %s, the time must be a positive number", d.VideoThumbPos) 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 return nil
} }

View File

@ -61,22 +61,14 @@ func (d *Local) GetSnapshot(videoPath string) (imgData *bytes.Buffer, err error)
} }
var ss string var ss string
if strings.HasSuffix(d.VideoThumbPos, "%") { if d.videoThumbPosIsPercentage {
percentage, err := strconv.ParseFloat(strings.TrimSuffix(d.VideoThumbPos, "%"), 64) ss = fmt.Sprintf("%f", totalDuration*d.videoThumbPos)
if err != nil {
return nil, err
}
ss = fmt.Sprintf("%f", totalDuration*percentage/100)
} else { } 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 the value is greater than the total duration, use the total duration
if val > totalDuration { if d.videoThumbPos > totalDuration {
ss = fmt.Sprintf("%f", totalDuration) ss = fmt.Sprintf("%f", totalDuration)
} else { } else {
ss = d.VideoThumbPos ss = fmt.Sprintf("%f", d.videoThumbPos)
} }
} }