Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (105 loc) · 3.55 KB
/
Copy pathmain.go
File metadata and controls
114 lines (105 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"github.com/christopher-dG/go-obs-websocket"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
)
type config struct {
sceneName string
itemName string
endpoints string
password string
port int
width int
height int
}
func parseFlags() (config, error) {
c := config{}
flag.StringVar(&c.endpoints, "endpoints", "", "The OBS endpoints to poll")
flag.StringVar(&c.password, "password", "", "The OBS password")
flag.StringVar(&c.sceneName, "scene-name", "Panel", "Name of the scene")
flag.StringVar(&c.itemName, "item-name", "RTMP stream", "Name of the item")
flag.IntVar(&c.port, "port", 4444, "OBS port number")
var resolution string
flag.StringVar(&resolution, "resolution", "1920x1080", "resolution to scale to")
flag.Parse()
if c.endpoints == "" {
return c, fmt.Errorf("you must specify --endpoints")
}
resolutionParts := strings.Split(resolution, "x")
if len(resolutionParts) != 2 {
return c, fmt.Errorf("--resolution must be of the form WIDTHxHEIGHT, e.g. 1920x1080")
}
var err error
if c.width, err = strconv.Atoi(resolutionParts[0]); err != nil {
return c, fmt.Errorf("couldn't parse resolution width: %v", err)
}
if c.height, err = strconv.Atoi(resolutionParts[1]); err != nil {
return c, fmt.Errorf("couldn't parse resolution height: %v", err)
}
return c, nil
}
func poll(endpoint string, port int, password, scene, item string) {
// Connect a client.
for {
func() {
log.Printf("Connecting to %s:%d...\n", endpoint, port)
c := obsws.Client{Host: endpoint, Port: port, Password: password}
if err := c.Connect(); err != nil {
log.Printf("Couldn't connect to %s:%d, retrying later...\n", endpoint, port)
time.Sleep(20 * time.Second)
return
}
log.Printf("Connected to %s:%d\n", endpoint, port)
defer c.Disconnect()
for {
req := obsws.NewGetSceneItemPropertiesRequest(scene, item)
resp, err := req.SendReceive(c)
if err != nil {
log.Printf("Couldn't get properties of %s/%s: %v\n", scene, item, err)
return
}
if (resp.SourceHeight > 0 && resp.SourceWidth > 0) && (resp.Width < 1919 || resp.Width > 1921) && (resp.Height < 1079 || resp.Height > 1081) {
scaleW := 1920.0 / float64(resp.SourceWidth)
scaleH := 1080.0 / float64(resp.SourceHeight)
scale := scaleH
if scaleW < scaleH {
scale = scaleW
}
// This sleep prevents us from actually crashing OBS completely. Yes, really.
time.Sleep(3 * time.Second)
log.Printf("Scaling %s/%s by %f (current size: %fx%f; source size: %dx%d)\n", scene, item, scale, resp.Width, resp.Height, resp.SourceWidth, resp.SourceHeight)
//obsws.NewSetSceneItemPropertiesRequest(scene, item, resp.PositionX, resp.PositionY, resp.PositionAlignment, resp.Rotation, scale, scale, resp.CropTop, resp.CropBottom, resp.CropLeft, resp.CropRight, resp.Visible, resp.Locked, resp.BoundsType, resp.BoundsAlignment, resp.BoundsX, resp.BoundsY)
req := obsws.NewSetSceneItemTransformRequest(scene, item, scale, scale, 0)
_, err := req.SendReceive(c)
if err != nil {
log.Printf("Couldn't set transform of %s/%s: %v\n", scene, item, err)
return
}
}
time.Sleep(1 * time.Second)
}
}()
time.Sleep(10 * time.Second)
}
}
func main() {
c, err := parseFlags()
obsws.Logger.SetOutput(ioutil.Discard)
obsws.SetReceiveTimeout(5 * time.Second)
if err != nil {
log.Println(err)
os.Exit(2)
}
endpoints := strings.Split(c.endpoints, ",")
for _, endpoint := range endpoints {
go poll(endpoint, c.port, c.password, c.sceneName, c.itemName)
}
select {}
}