由于项目需要,需要去获取视频的某一帧画面,研究过opencv后,团队决定利用GOCV(https://gocv.io)去截取视频图片的某一帧图像:
func GetVideoMoment(filePath string, time float64) (i image.Image, err error) {
//load video
vc, err := gocv.VideoCaptureFile(filePath)
if err != nil {
return i, err
}
frames := vc.Get(gocv.VideoCaptureFrameCount)
fps := vc.Get(gocv.VideoCaptureFPS)
duration := frames / fps
frames = (time / duration) * frames
// Set Video frames
vc.Set(gocv.VideoCapturePosFrames, frames)
img := gocv.NewMat()
vc.Read(&img)
imageObject, err := img.ToImage()
if err != nil {
return i, err
}
return imageObject, err
}
这里的VideoCaptureFile函数,支持本地文件和url两种途径。
BOW!!!完成!