forked from wulkano/Aperture
/
main.swift
78 lines (67 loc) · 1.66 KB
/
main.swift
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
import Foundation
import AVFoundation
struct Options: Decodable {
let destination: URL
let framesPerSecond: Int
let cropRect: CGRect?
let showCursor: Bool
let highlightClicks: Bool
let screenId: CGDirectDisplayID
let audioDeviceId: String?
let videoCodec: String?
}
func record() throws {
let options: Options = try CLI.arguments.first!.jsonDecoded()
let recorder = try Recorder(
destination: options.destination,
framesPerSecond: options.framesPerSecond,
cropRect: options.cropRect,
showCursor: options.showCursor,
highlightClicks: options.highlightClicks,
screenId: options.screenId == 0 ? .main : options.screenId,
audioDevice: options.audioDeviceId != nil ? AVCaptureDevice(uniqueID: options.audioDeviceId!) : nil,
videoCodec: options.videoCodec
)
recorder.onStart = {
print("R")
}
recorder.onFinish = {
exit(0)
}
recorder.onError = {
print($0, to: .standardError)
exit(1)
}
CLI.onExit = {
recorder.stop()
// Do not call `exit()` here as the video is not always done
// saving at this point and will be corrupted randomly
}
recorder.start()
setbuf(__stdoutp, nil)
RunLoop.main.run()
}
func showUsage() {
print(
"""
Usage:
aperture <options>
aperture list-screens
aperture list-audio-devices
"""
)
}
switch CLI.arguments.first {
case "list-screens":
print(try toJson(DeviceList.screen()), to: .standardError)
exit(0)
case "list-audio-devices":
// Uses stderr because of unrelated stuff being outputted on stdout
print(try toJson(DeviceList.audio()), to: .standardError)
exit(0)
case .none:
showUsage()
exit(1)
default:
try record()
}