|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/go-vgo/robotgo" |
| 13 | + "github.com/manifoldco/promptui" |
| 14 | + "golang.design/x/hotkey" |
| 15 | +) |
| 16 | + |
| 17 | +type RecordingOptions struct { |
| 18 | + FPS int `json:"fps"` |
| 19 | + CaptureMouse bool `json:"capture_mouse"` |
| 20 | + AudioDevice string `json:"audio_device"` |
| 21 | + Width int `json:"width"` |
| 22 | + Height int `json:"height"` |
| 23 | +} |
| 24 | + |
| 25 | +var defaultOpts = map[string]any{ |
| 26 | + "fps": 30, |
| 27 | + "capture_mouse": true, |
| 28 | + "audio_device": "", |
| 29 | +} |
| 30 | + |
| 31 | +func mergeRecordingDefaults() { |
| 32 | + data, err := os.ReadFile(configFilePath) |
| 33 | + if err != nil { |
| 34 | + initConfig() |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + var config map[string]any |
| 39 | + if err := json.Unmarshal(data, &config); err != nil { |
| 40 | + fmt.Println("Error reading config:", err) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + recRaw, ok := config["recording_options"] |
| 45 | + if !ok { |
| 46 | + config["recording_options"] = defaultOpts |
| 47 | + } else { |
| 48 | + recMap, ok := recRaw.(map[string]any) |
| 49 | + if !ok { |
| 50 | + fmt.Println("Error reading recording options") |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + changed := false |
| 55 | + for k, v := range defaultOpts { |
| 56 | + if _, exists := recMap[k]; !exists { |
| 57 | + recMap[k] = v |
| 58 | + changed = true |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if changed { |
| 63 | + config["recording_options"] = recMap |
| 64 | + } else { |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + out, _ := json.MarshalIndent(config, "", " ") |
| 70 | + os.WriteFile(configFilePath, out, 0644) |
| 71 | +} |
| 72 | + |
| 73 | +func ternary(cond bool, a, b any) any { |
| 74 | + if cond { |
| 75 | + return a |
| 76 | + } |
| 77 | + return b |
| 78 | +} |
| 79 | + |
| 80 | +func RecordDisplay() { |
| 81 | + active_displays := robotgo.DisplaysNum() |
| 82 | + displays := []string{"Display 1 (Primary)"} |
| 83 | + for i := 2; i < active_displays; i++ { |
| 84 | + displays = append(displays, fmt.Sprintf("Display %d", i)) |
| 85 | + } |
| 86 | + prompt := promptui.Select{ |
| 87 | + Label: "Select Display", |
| 88 | + Items: displays, |
| 89 | + } |
| 90 | + |
| 91 | + display, _, err := prompt.Run() |
| 92 | + if err != nil { |
| 93 | + fmt.Print("Some error occurred") |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + x, y, w, h := robotgo.GetDisplayBounds(display) |
| 98 | + filename := filepath.Join(config.SaveLocation, fmt.Sprintf("Recording_Disp%d_%dx%d_%s.mp4", display+1, w, h, time.Now().Format("20060102_150405"))) |
| 99 | + args := []string{ |
| 100 | + "-f", "gdigrab", |
| 101 | + "-framerate", fmt.Sprintf("%d", config.RecordingOpts.FPS), |
| 102 | + "-offset_x", strconv.Itoa(x), |
| 103 | + "-offset_y", strconv.Itoa(y), |
| 104 | + "-video_size", fmt.Sprintf("%dx%d", w, h), |
| 105 | + "-draw_mouse", ternary(config.RecordingOpts.CaptureMouse, "1", "0").(string), |
| 106 | + "-i", "desktop", |
| 107 | + "-c:v", "libx264", |
| 108 | + "-preset", "ultrafast", |
| 109 | + "-y", filename, |
| 110 | + } |
| 111 | + |
| 112 | + cmd := exec.Command(getFfmpegPath(), args...) |
| 113 | + stdin, _ := cmd.StdinPipe() |
| 114 | + var start time.Time |
| 115 | + if err, start = cmd.Start(), time.Now(); err != nil { |
| 116 | + fmt.Println("Error starting ffmpeg:", err) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + fmt.Println("Recording started. Press ctrl+shift+3 to stop") |
| 121 | + ticker := time.NewTicker(time.Second) |
| 122 | + go func() { |
| 123 | + defer ticker.Stop() |
| 124 | + for range ticker.C { |
| 125 | + select { |
| 126 | + case <-ticker.C: |
| 127 | + fmt.Printf("\rRecording time elapsed: %02d:%02d", int(time.Since(start).Minutes()), int(time.Since(start).Seconds())%60) |
| 128 | + default: |
| 129 | + } |
| 130 | + } |
| 131 | + fmt.Println() |
| 132 | + }() |
| 133 | + hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.Key3) |
| 134 | + err = hk.Register() |
| 135 | + if err != nil { |
| 136 | + fmt.Println("Error registering hotkey:", err) |
| 137 | + return |
| 138 | + } |
| 139 | + defer hk.Unregister() |
| 140 | + keyChan := hk.Keydown() |
| 141 | + for range keyChan { |
| 142 | + fmt.Println("\nStopping recording...") |
| 143 | + stdin.Write([]byte("q")) |
| 144 | + stdin.Close() |
| 145 | + ticker.Stop() |
| 146 | + break |
| 147 | + } |
| 148 | + |
| 149 | + err = cmd.Wait() |
| 150 | + if err != nil { |
| 151 | + fmt.Println("Error waiting for ffmpeg to exit:", err) |
| 152 | + } |
| 153 | + fmt.Printf("\nRecording stopped. Recording saved at %s", filename) |
| 154 | +} |
0 commit comments