golang 摄像头截图命令版本

编程入门 行业动态 更新时间:2024-10-27 12:35:48

golang 摄像头<a href=https://www.elefans.com/category/jswz/34/1769444.html style=截图命令版本"/>

golang 摄像头截图命令版本

需要安装ffmpeg 使用ffmpeg命令进行画面生成对应的jpg图片

package commonimport ("context""errors""fmt""net""net/url""os/exec""time"
)func GetRtspImage(rtsp string) (string, error) {tmpFile := fmt.Sprintf("/tmp/%d.jpg", time.Now().Unix())var params []stringparams = append(params, "-rtsp_transport")params = append(params, "tcp")params = append(params, "-y")params = append(params, "-i")params = append(params, rtsp)params = append(params, "-vframes")params = append(params, "1")params = append(params, "-f")params = append(params, "image2")params = append(params, tmpFile)_, err := Command(time.Second*3, "ffmpeg", params...)if err != nil {return "", fmt.Errorf("commond err:%w", err)}return tmpFile, nil
}// 执行命令并添加超时检测
func Command(timeout time.Duration, name string, arg ...string) (string, error) {ctx, cancel := context.WithTimeout(context.Background(), timeout)cmd := exec.CommandContext(ctx, name, arg...)defer func() {cancel()}()// 当经过Timeout时间后,程序依然没有运行完,则会杀掉进程,ctx也会有err信息if out, err := cmd.Output(); err != nil {// 检测报错是否是因为超时引起的if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {return "", errors.New("command timeout")}return string(out), err} else {return string(out), nil}
}

更多推荐

golang 摄像头截图命令版本

本文发布于:2023-12-03 20:44:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1657458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:截图   摄像头   命令   版本   golang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!