forked from danielpaulus/quicktime_video_hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
280 lines (238 loc) · 8.21 KB
/
main.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/signal"
"github.com/danielpaulus/quicktime_video_hack/screencapture"
"github.com/danielpaulus/quicktime_video_hack/screencapture/coremedia"
"github.com/danielpaulus/quicktime_video_hack/screencapture/gstadapter"
"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
)
const version = "v0.3-beta"
func main() {
usage := fmt.Sprintf(`Q.uickTime V.ideo H.ack (qvh) %s
Usage:
qvh devices [-v]
qvh activate [--udid=<udid>] [-v]
qvh record <h264file> <wavfile> [-v] [--udid=<udid>]
qvh gstreamer [--pipeline=<pipeline>] [--examples] [-v]
qvh --version | version
Options:
-h --help Show this screen.
-v Enable verbose mode (debug logging).
--version Show version.
--udid=<udid> UDID of the device. If not specified, the first found device will be used automatically.
The commands work as following:
devices lists iOS devices attached to this host and tells you if video streaming was activated for them
activate enables the video streaming config for the device specified by --udid
record will start video&audio recording. Video will be saved in a raw h264 file playable by VLC.
Audio will be saved in a uncompressed wav file. Run like: "qvh record /home/yourname/out.h264 /home/yourname/out.wav"
gstreamer If no additional param is provided, qvh will open a new window and push AV data to gstreamer.
If "qvh gstreamer --examples" is provided, qvh will print some common gstreamer pipeline examples.
If --pipeline is provided, qvh will use the provided gstreamer pipeline instead of
displaying audio and video in a window.
`, version)
arguments, _ := docopt.ParseDoc(usage)
log.SetFormatter(&log.JSONFormatter{})
verboseLoggingEnabled, _ := arguments.Bool("-v")
if verboseLoggingEnabled {
log.Info("Set Debug mode")
log.SetLevel(log.DebugLevel)
}
shouldPrintVersionNoDashes, _ := arguments.Bool("version")
shouldPrintVersion, _ := arguments.Bool("--version")
if shouldPrintVersionNoDashes || shouldPrintVersion {
printVersion()
return
}
devicesCommand, _ := arguments.Bool("devices")
if devicesCommand {
devices()
return
}
udid, _ := arguments.String("--udid")
log.Debugf("requested udid:'%s'", udid)
activateCommand, _ := arguments.Bool("activate")
if activateCommand {
activate(udid)
return
}
recordCommand, _ := arguments.Bool("record")
if recordCommand {
h264FilePath, err := arguments.String("<h264file>")
if err != nil {
printErrJSON(err, "Missing <h264file> parameter. Please specify a valid path like '/home/me/out.h264'")
return
}
waveFilePath, err := arguments.String("<wavfile>")
if err != nil {
printErrJSON(err, "Missing <wavfile> parameter. Please specify a valid path like '/home/me/out.raw'")
return
}
record(h264FilePath, waveFilePath, udid)
}
gstreamerCommand, _ := arguments.Bool("gstreamer")
if gstreamerCommand {
shouldPrintExamples, _ := arguments.Bool("--examples")
if shouldPrintExamples {
printExamples()
return
}
gstPipeline, _ := arguments.String("--pipeline")
if gstPipeline == "" {
startGStreamer(udid)
return
}
startGStreamerWithCustomPipeline(udid, gstPipeline)
}
}
func printVersion() {
versionMap := map[string]interface{}{
"version": version,
}
printJSON(versionMap)
}
func printExamples() {
examples := `Examples:
Writing an MP4 file
This pipeline will save the recording in video.mp4 with h264 and aac format. The default settings
of this pipeline will create a compressed video that takes up way less space than raw h264.
Note that you need to set "ignore-length" on the wavparse because we are streaming and do not know the length in advance.
Write MP4 file Mac OSX:
vtdec is the hardware accelerated decoder on the mac.
qvh gstreamer --pipeline "mp4mux name=mux ! filesink location=video.mp4 \
queue name=audio_target ! wavparse ignore-length=true ! audioconvert ! faac ! aacparse ! mux. \
queue name=video_target ! h264parse ! vtdec ! videoconvert ! x264enc tune=zerolatency ! mux."
Write MP4 file Linux:
note that I am using software en and decoding, if you have intel VAAPI available, maybe use those.
gstreamer --pipeline "mp4mux name=mux ! filesink location=video.mp4 \
queue name=audio_target ! wavparse ignore-length=true ! audioconvert ! avenc_aac ! aacparse ! mux. \
queue name=video_target ! h264parse ! avdec_h264 ! videoconvert ! x264enc tune=zerolatency ! mux."
`
fmt.Print(examples)
}
func startGStreamerWithCustomPipeline(udid string, pipelineString string) {
log.Debug("Starting Gstreamer with custom pipeline")
gStreamer, err := gstadapter.NewWithCustomPipeline(pipelineString)
if err != nil {
printErrJSON(err, "Failed creating custom pipeline")
return
}
startWithConsumer(gStreamer, udid)
}
func startGStreamer(udid string) {
log.Debug("Starting Gstreamer")
gStreamer := gstadapter.New()
startWithConsumer(gStreamer, udid)
}
// Just dump a list of what was discovered to the console
func devices() {
deviceList, err := screencapture.FindIosDevices()
if err != nil {
printErrJSON(err, "Error finding iOS Devices")
}
log.Debugf("Found (%d) iOS Devices with UsbMux Endpoint", len(deviceList))
if err != nil {
printErrJSON(err, "Error finding iOS Devices")
}
output := screencapture.PrintDeviceDetails(deviceList)
printJSON(map[string]interface{}{"devices": output})
}
// This command is for testing if we can enable the hidden Quicktime device config
func activate(udid string) {
device, err := screencapture.FindIosDevice(udid)
if err != nil {
printErrJSON(err, "no device found to activate")
return
}
log.Debugf("Enabling device: %v", device)
device, err = screencapture.EnableQTConfig(device)
if err != nil {
printErrJSON(err, "Error enabling QT config")
return
}
printJSON(map[string]interface{}{
"device_activated": device.DetailsMap(),
})
}
func record(h264FilePath string, wavFilePath string, udid string) {
log.Debugf("Writing video output to:'%s' and audio to: %s", h264FilePath, wavFilePath)
h264File, err := os.Create(h264FilePath)
if err != nil {
log.Debugf("Error creating h264File:%s", err)
log.Errorf("Could not open h264File '%s'", h264FilePath)
}
wavFile, err := os.Create(wavFilePath)
if err != nil {
log.Debugf("Error creating wav file:%s", err)
log.Errorf("Could not open wav file '%s'", wavFilePath)
}
writer := coremedia.NewAVFileWriter(bufio.NewWriter(h264File), bufio.NewWriter(wavFile))
defer func() {
stat, err := wavFile.Stat()
if err != nil {
log.Fatal("Could not get wav file stats", err)
}
err = coremedia.WriteWavHeader(int(stat.Size()), wavFile)
if err != nil {
log.Fatalf("Error writing wave header %s might be invalid. %s", wavFilePath, err.Error())
}
err = wavFile.Close()
if err != nil {
log.Fatalf("Error closing wave file. '%s' might be invalid. %s", wavFilePath, err.Error())
}
err = h264File.Close()
if err != nil {
log.Fatalf("Error closing h264File '%s'. %s", h264FilePath, err.Error())
}
}()
startWithConsumer(writer, udid)
}
func startWithConsumer(consumer screencapture.CmSampleBufConsumer, udid string) {
device, err := screencapture.FindIosDevice(udid)
if err != nil {
printErrJSON(err, "no device found to activate")
return
}
device, err = screencapture.EnableQTConfig(device)
if err != nil {
printErrJSON(err, "Error enabling QT config")
return
}
adapter := screencapture.UsbAdapter{}
stopSignal := make(chan interface{})
waitForSigInt(stopSignal)
mp := screencapture.NewMessageProcessor(&adapter, stopSignal, consumer)
err = adapter.StartReading(device, &mp, stopSignal)
consumer.Stop()
if err != nil {
printErrJSON(err, "failed connecting to usb")
}
}
func waitForSigInt(stopSignalChannel chan interface{}) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Debugf("Signal received: %s", sig)
var stopSignal interface{}
stopSignalChannel <- stopSignal
}
}()
}
func printErrJSON(err error, msg string) {
printJSON(map[string]interface{}{
"original_error": err.Error(),
"error_message": msg,
})
}
func printJSON(output map[string]interface{}) {
text, err := json.Marshal(output)
if err != nil {
log.Fatalf("Broken json serialization, error: %s", err)
}
println(string(text))
}