diff --git a/main.go b/main.go index e970a91..2b0822c 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,7 @@ type CLI struct { Hotkey HotkeyCmd `help:"Manage hotkeys." cmd:"" aliases:"hk"` Filter FilterCmd `help:"Manage filters." cmd:"" aliases:"f"` Projector ProjectorCmd `help:"Manage projectors." cmd:"" aliases:"prj"` + Screenshot ScreenshotCmd `help:"Take screenshots." cmd:"" aliases:"ss"` } type context struct { diff --git a/screenshot.go b/screenshot.go new file mode 100644 index 0000000..5472dd3 --- /dev/null +++ b/screenshot.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "path/filepath" + + "github.com/andreykaipov/goobs/api/requests/sources" +) + +// ScreenshotCmd provides commands to manage screenshots in OBS Studio. +type ScreenshotCmd struct { + Save ScreenshotSaveCmd `cmd:"" help:"Take a screenshot and save it to a file." aliases:"sv"` +} + +// ScreenshotSaveCmd represents the command to save a screenshot of a source in OBS. +type ScreenshotSaveCmd struct { + SourceName string `arg:"" help:"Name of the source to take a screenshot of."` + FilePath string `arg:"" help:"Path to the file where the screenshot will be saved."` + Width float64 ` help:"Width of the screenshot in pixels." flag:"" default:"1920"` + Height float64 ` help:"Height of the screenshot in pixels." flag:"" default:"1080"` + Quality float64 ` help:"Quality of the screenshot (1-100)." flag:"" default:"-1"` +} + +// Run executes the command to take a screenshot and save it to a file. +func (cmd *ScreenshotSaveCmd) Run(ctx *context) error { + _, err := ctx.Client.Sources.SaveSourceScreenshot( + sources.NewSaveSourceScreenshotParams(). + WithSourceName(cmd.SourceName). + WithImageFormat(trimPrefix(filepath.Ext(cmd.FilePath), ".")). + WithImageFilePath(cmd.FilePath). + WithImageWidth(cmd.Width). + WithImageHeight(cmd.Height). + WithImageCompressionQuality(cmd.Quality), + ) + if err != nil { + return fmt.Errorf("failed to take screenshot: %w", err) + } + + fmt.Fprintf(ctx.Out, "Screenshot saved to %s.\n", cmd.FilePath) + return nil +} diff --git a/util.go b/util.go index 11717b7..b9ee5e7 100644 --- a/util.go +++ b/util.go @@ -20,3 +20,10 @@ func getEnabledMark(enabled bool) string { } return "\u274c" // red cross mark } + +func trimPrefix(s, prefix string) string { + if strings.HasPrefix(s, prefix) { + return s[len(prefix):] + } + return s +}