golang 使用ffmpeg获取rtsp流截图,并生成缩略图
-
golang 调用命令公共方法
func CallCommandRun(cmdName string, args []string) (string, error) { cmd := exec.Command(cmdName, args...) fmt.Println("CallCommand Run 参数=> ", args) fmt.Println("CallCommand Run 执行命令=> ", cmd) bytes, err := cmd.Output() if err != nil { fmt.Println("CallCommand Run 出错了.....", err.Error()) fmt.Println(err) return "", err } resp := string(bytes) fmt.Println(resp) fmt.Println("CallCommand Run 调用完成.....") return resp, nil }
-
golang直接使用命令调用ffmpeg 获取rtsp码流截图
-
ffmpeg命令
./ffmpeg -rtsp_transport tcp -timeout 10000000 -y -i rtsp://username:password@ip:554/chID=00000001-0000-0000-0000-000000000000 -ss 00:00:01 -vframes 1 -f image2 -vcodec png image.png
-
go 程序取流截图
// 根据URL调用ffmpeg 获取截图 func GetIpcScreenShot(ffmpegPath string, url string, screenShotPath string) string { var params []string params = append(params, "-rtsp_transport") params = append(params, "tcp") params = append(params, "-timeout") // 10s params = append(params, "10000000") params = append(params, "-y") params = append(params, "-i") params = append(params, url) params = append(params, "-ss") params = append(params, "00:00:02") params = append(params, "-vframes") params = append(params, "1") params = append(params, "-f") params = append(params, "image2") params = append(params, "-vcodec") params = append(params, "png") params = append(params, screenShotPath) resp, err := CallCommandRun(ffmpegPath, params) if err != nil { fmt.Println(err.Error()) fmt.Println("获取截图出错,url为--->", url) } return resp }
-
-
使用imaging包将第二步获取的截图生成缩略图
github.com/disintegration/imaging
// 将截图生成缩略图 func GetThumbnail(screenShotPath string) error { src, err := imaging.Open(screenShotPath) if err != nil { fmt.Println("生成缩略图,打开截图文件失败--->", err) return err } // 220 * 140 宽220px, 高140px dsc := imaging.Resize(src, 220, 140, imaging.Lanczos) err = imaging.Save(dsc, screenShotPath) if err != nil { fmt.Println("生成缩略图失败--->", err) return err } return nil }