1 回答

TA贡献1898条经验 获得超8个赞
根据评论的要求。
基本思想是使用全局存储来存储您的活动命令。它不一定是全局的,但您需要有更大的范围,以便您的函数可以访问它。
var commands = map[string]*exec.Cmd{}
func startRecording(fileName string) error {
ffmpeg := exec.Command("ffmpeg",
"-f", "gdigrab",
"-framerate", "30",
"-i", "desktop",
"-f", "mp4",
fileName,
)
commands[fileName] = ffmpeg
...
}
func stopRecording(fileName string) error {
cmd, ok := commands[fileName]
if !ok {
return errors.New("command not found")
}
defer func() {
delete(commands, fileName)
}()
return cmd.Process.Kill()
}
您可能想使用 sync.Mutex或sync.RWMutex来避免并发映射写入。
所以你的commands云看起来像:
type Commands struct {
sync.RWMutex
items map[string]*exec.Cmd
}
// use Commands.Lock() for writing, Commands.RLock() for reading
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报