diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 8a804ef3..faa2b3bd 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -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 { @@ -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 { @@ -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 } diff --git a/drivers/local/util.go b/drivers/local/util.go index d2fbd097..802f60cf 100644 --- a/drivers/local/util.go +++ b/drivers/local/util.go @@ -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) } }