codeplay-common 4.3.4 → 4.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
//node take-screen-video.js
|
|
2
|
+
|
|
3
|
+
//--audio without //with,without
|
|
4
|
+
//--duration 0 //30,120 (0 is used "Enter" key to stop)
|
|
5
|
+
//--countdown 0//0,5,10
|
|
6
|
+
//--name test-name
|
|
7
|
+
//--profiles youtube,non-spatial-xr,amazon
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const fs = require('node:fs');
|
|
12
|
+
const path = require('node:path');
|
|
13
|
+
const { spawn, spawnSync } = require('node:child_process');
|
|
14
|
+
|
|
15
|
+
const scriptDirectory = __dirname;
|
|
16
|
+
const projectDirectory = scriptDirectory;
|
|
17
|
+
const temporaryDirectory = path.join(projectDirectory, 'agent-temp', 'store-video');
|
|
18
|
+
const outputDirectory = path.join(projectDirectory, 'Auto-Screenshot', 'Video', 'Output');
|
|
19
|
+
const deviceRecordingPath = '/sdcard/codeplay_store_video.mp4';
|
|
20
|
+
const ffmpegFallbackDirectory = 'W:\\Tools\\ffmpeg\\bin';
|
|
21
|
+
const scrcpyFallbackPath = 'W:\\Tools\\scrcpy\\scrcpy.exe';
|
|
22
|
+
const capacitorConfigPath = path.join(projectDirectory, 'capacitor.config.json');
|
|
23
|
+
|
|
24
|
+
const videoProfiles = {
|
|
25
|
+
youtube: {
|
|
26
|
+
label: 'YouTube preview',
|
|
27
|
+
directory: 'YouTube',
|
|
28
|
+
fileSuffix: 'youtube',
|
|
29
|
+
width: 1920,
|
|
30
|
+
height: 1080,
|
|
31
|
+
videoBitrate: '8M',
|
|
32
|
+
h264Level: '4.2',
|
|
33
|
+
},
|
|
34
|
+
'non-spatial-xr': {
|
|
35
|
+
label: 'Google Play non-spatial XR',
|
|
36
|
+
directory: 'Google-Play-XR-Non-Spatial',
|
|
37
|
+
fileSuffix: 'non-spatial-xr',
|
|
38
|
+
width: 3840,
|
|
39
|
+
height: 2160,
|
|
40
|
+
videoBitrate: '12M',
|
|
41
|
+
h264Level: '5.1',
|
|
42
|
+
},
|
|
43
|
+
amazon: {
|
|
44
|
+
label: 'Amazon Appstore',
|
|
45
|
+
directory: 'Amazon',
|
|
46
|
+
fileSuffix: 'amazon',
|
|
47
|
+
width: 1920,
|
|
48
|
+
height: 1080,
|
|
49
|
+
videoBitrate: '4M',
|
|
50
|
+
h264Level: '4.2',
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function run(command, args, options = {}) {
|
|
55
|
+
const result = spawnSync(command, args, {
|
|
56
|
+
encoding: 'utf8',
|
|
57
|
+
windowsHide: true,
|
|
58
|
+
stdio: options.inherit ? 'inherit' : 'pipe',
|
|
59
|
+
...options,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (result.error) {
|
|
63
|
+
if (result.error.code === 'ENOENT') {
|
|
64
|
+
throw new Error(`${command} was not found in PATH.`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw result.error;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (result.status !== 0) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
String(result.stderr || result.stdout || `${command} exited with code ${result.status}.`).trim(),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return String(result.stdout || '').trim();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function commandExists(command) {
|
|
80
|
+
const result = spawnSync(command, ['-version'], {
|
|
81
|
+
encoding: 'utf8',
|
|
82
|
+
windowsHide: true,
|
|
83
|
+
});
|
|
84
|
+
return !result.error && result.status === 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getExecutable(command) {
|
|
88
|
+
if (commandExists(command)) {
|
|
89
|
+
return command;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const fallbackPath = path.join(ffmpegFallbackDirectory, `${command}.exe`);
|
|
93
|
+
if (fs.existsSync(fallbackPath)) {
|
|
94
|
+
return fallbackPath;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function wait(milliseconds) {
|
|
101
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function runAsync(command, args, options = {}) {
|
|
105
|
+
const child = spawn(command, args, {
|
|
106
|
+
windowsHide: true,
|
|
107
|
+
stdio: 'inherit',
|
|
108
|
+
...options,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
child,
|
|
113
|
+
completion: new Promise((resolve, reject) => {
|
|
114
|
+
child.once('error', reject);
|
|
115
|
+
child.once('exit', (code) => {
|
|
116
|
+
if (code === 0) resolve();
|
|
117
|
+
else reject(new Error(`${command} exited with code ${code}.`));
|
|
118
|
+
});
|
|
119
|
+
}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function getConnectedDevice() {
|
|
124
|
+
const devices = run('adb', ['devices'])
|
|
125
|
+
.split(/\r?\n/)
|
|
126
|
+
.map((line) => line.match(/^(\S+)\s+device$/))
|
|
127
|
+
.filter(Boolean)
|
|
128
|
+
.map((match) => match[1]);
|
|
129
|
+
|
|
130
|
+
if (!devices.length) {
|
|
131
|
+
throw new Error('no_connected_device');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return devices[0];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function sanitizeName(value) {
|
|
138
|
+
const name = String(value || '')
|
|
139
|
+
.trim()
|
|
140
|
+
.replace(/\.mp4$/i, '')
|
|
141
|
+
.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '_')
|
|
142
|
+
.replace(/[. ]+$/g, '');
|
|
143
|
+
return name || 'store-video';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function getCapacitorConfig() {
|
|
147
|
+
return JSON.parse(fs.readFileSync(capacitorConfigPath, 'utf8'));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function getDefaultVideoName() {
|
|
151
|
+
return sanitizeName(getCapacitorConfig().appName || 'store-video');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function parseArguments() {
|
|
155
|
+
const args = process.argv.slice(2);
|
|
156
|
+
const options = {
|
|
157
|
+
audio: 'without',
|
|
158
|
+
countdown: 5,
|
|
159
|
+
duration: 0,
|
|
160
|
+
help: false,
|
|
161
|
+
input: '',
|
|
162
|
+
list: false,
|
|
163
|
+
name: '',
|
|
164
|
+
profiles: 'youtube,non-spatial-xr,amazon',
|
|
165
|
+
spatialInput: '',
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
169
|
+
const argument = args[index];
|
|
170
|
+
|
|
171
|
+
if (argument === '--audio') {
|
|
172
|
+
options.audio = String(args[index + 1] || '').toLowerCase();
|
|
173
|
+
index += 1;
|
|
174
|
+
} else if (argument === '--countdown') {
|
|
175
|
+
options.countdown = Number(args[index + 1]);
|
|
176
|
+
index += 1;
|
|
177
|
+
} else if (argument === '--duration') {
|
|
178
|
+
options.duration = Number(args[index + 1]);
|
|
179
|
+
index += 1;
|
|
180
|
+
} else if (argument === '--input') {
|
|
181
|
+
options.input = args[index + 1] || '';
|
|
182
|
+
index += 1;
|
|
183
|
+
} else if (argument === '--name') {
|
|
184
|
+
options.name = args[index + 1] || '';
|
|
185
|
+
index += 1;
|
|
186
|
+
} else if (argument === '--profiles') {
|
|
187
|
+
options.profiles = args[index + 1] || '';
|
|
188
|
+
index += 1;
|
|
189
|
+
} else if (argument === '--spatial-input') {
|
|
190
|
+
options.spatialInput = args[index + 1] || '';
|
|
191
|
+
index += 1;
|
|
192
|
+
} else if (argument === '--list') {
|
|
193
|
+
options.list = true;
|
|
194
|
+
} else if (argument === '--help' || argument === '-h') {
|
|
195
|
+
options.help = true;
|
|
196
|
+
} else {
|
|
197
|
+
throw new Error(`Unknown argument: ${argument}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (!Number.isInteger(options.duration) || options.duration < 0 || options.duration > 180) {
|
|
202
|
+
throw new Error('--duration must be 0 (unlimited) or a whole number from 1 to 180 seconds.');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!Number.isInteger(options.countdown) || options.countdown < 0 || options.countdown > 30) {
|
|
206
|
+
throw new Error('--countdown must be a whole number from 0 to 30 seconds.');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!['with', 'without'].includes(options.audio)) {
|
|
210
|
+
throw new Error('--audio must be with or without.');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
options.name = options.name
|
|
214
|
+
? sanitizeName(options.name)
|
|
215
|
+
: getDefaultVideoName();
|
|
216
|
+
return options;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function selectProfiles(input) {
|
|
220
|
+
const names = String(input)
|
|
221
|
+
.split(',')
|
|
222
|
+
.map((name) => name.trim().toLowerCase())
|
|
223
|
+
.filter(Boolean);
|
|
224
|
+
|
|
225
|
+
if (names.includes('all')) {
|
|
226
|
+
return Object.keys(videoProfiles);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
for (const name of names) {
|
|
230
|
+
if (!videoProfiles[name] && name !== 'spatial-xr') {
|
|
231
|
+
throw new Error(`Unknown profile: ${name}. Run with --list to see valid profiles.`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return [...new Set(names)];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function printProfiles() {
|
|
239
|
+
for (const [name, profile] of Object.entries(videoProfiles)) {
|
|
240
|
+
console.log(
|
|
241
|
+
`${name.padEnd(16)} ${profile.label} (${profile.width}x${profile.height}, H.264 MP4)`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
console.log('spatial-xr Genuine 360/180/3D source passthrough (requires --spatial-input)');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function printHelp() {
|
|
249
|
+
console.log('Record the connected Android device and create store-ready video variants.');
|
|
250
|
+
console.log('');
|
|
251
|
+
console.log('Usage:');
|
|
252
|
+
console.log(' node take-screen-video.js --name gameplay --duration 30');
|
|
253
|
+
console.log(' node take-screen-video.js --audio with');
|
|
254
|
+
console.log(' node take-screen-video.js --input recording.mp4 --profiles youtube,amazon');
|
|
255
|
+
console.log(' node take-screen-video.js --profiles spatial-xr --spatial-input genuine-360.mp4');
|
|
256
|
+
console.log(' node take-screen-video.js --profiles all');
|
|
257
|
+
console.log(' node take-screen-video.js --list');
|
|
258
|
+
console.log('');
|
|
259
|
+
console.log('Defaults: --audio without --duration 0 (unlimited)');
|
|
260
|
+
console.log(`Default name: ${getDefaultVideoName()} (capacitor.config.json appName)`);
|
|
261
|
+
console.log('Manual mode: press Enter to stop; a positive --duration sets a safety maximum.');
|
|
262
|
+
console.log('Audio recording uses scrcpy and requires Android 11 or newer.');
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function waitForManualStop(recordingCompletion, duration) {
|
|
266
|
+
if (!process.stdin.isTTY) {
|
|
267
|
+
throw new Error('Manual interaction requires an interactive terminal so Enter can stop recording.');
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
const finish = (reason) => {
|
|
272
|
+
if (timeout) clearTimeout(timeout);
|
|
273
|
+
process.stdin.removeListener('data', onInput);
|
|
274
|
+
process.stdin.pause();
|
|
275
|
+
resolve(reason);
|
|
276
|
+
};
|
|
277
|
+
const onInput = (input) => {
|
|
278
|
+
if (/\r|\n/.test(String(input))) finish('enter');
|
|
279
|
+
};
|
|
280
|
+
const timeout = duration > 0
|
|
281
|
+
? setTimeout(() => finish('maximum-duration'), duration * 1000)
|
|
282
|
+
: null;
|
|
283
|
+
|
|
284
|
+
process.stdin.resume();
|
|
285
|
+
process.stdin.on('data', onInput);
|
|
286
|
+
recordingCompletion.then(
|
|
287
|
+
() => finish('recorder-completed'),
|
|
288
|
+
(error) => {
|
|
289
|
+
if (timeout) clearTimeout(timeout);
|
|
290
|
+
process.stdin.removeListener('data', onInput);
|
|
291
|
+
process.stdin.pause();
|
|
292
|
+
reject(error);
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function getScrcpyCommand() {
|
|
299
|
+
if (commandExists('scrcpy')) return 'scrcpy';
|
|
300
|
+
return fs.existsSync(scrcpyFallbackPath) ? scrcpyFallbackPath : '';
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async function recordConnectedDevice(options, destinationPath) {
|
|
304
|
+
const { audio, countdown, duration } = options;
|
|
305
|
+
const deviceId = getConnectedDevice();
|
|
306
|
+
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
|
|
307
|
+
if (fs.existsSync(destinationPath)) {
|
|
308
|
+
fs.unlinkSync(destinationPath);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
console.log(`Connected device: ${deviceId}`);
|
|
312
|
+
console.log(`Interaction: manual; audio: ${audio}.`);
|
|
313
|
+
console.log('Open the required first app screen, then operate the physical device while recording.');
|
|
314
|
+
|
|
315
|
+
for (let seconds = countdown; seconds > 0; seconds -= 1) {
|
|
316
|
+
console.log(`Recording starts in ${seconds}...`);
|
|
317
|
+
wait(1000);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
console.log(
|
|
321
|
+
duration > 0
|
|
322
|
+
? `RECORDING NOW — maximum ${duration} seconds.`
|
|
323
|
+
: 'RECORDING NOW — unlimited duration; press Enter to stop.',
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
let recording;
|
|
327
|
+
if (audio === 'with') {
|
|
328
|
+
const scrcpyCommand = getScrcpyCommand();
|
|
329
|
+
if (!scrcpyCommand) {
|
|
330
|
+
throw new Error(
|
|
331
|
+
'Audio recording requires scrcpy. Install it at W:\\Tools\\scrcpy or add it to PATH.',
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const scrcpyArguments = [
|
|
336
|
+
'--serial', deviceId,
|
|
337
|
+
'--no-window',
|
|
338
|
+
'--no-playback',
|
|
339
|
+
'--no-control',
|
|
340
|
+
'--require-audio',
|
|
341
|
+
'--record', destinationPath,
|
|
342
|
+
];
|
|
343
|
+
if (duration > 0) {
|
|
344
|
+
scrcpyArguments.push('--time-limit', String(duration));
|
|
345
|
+
}
|
|
346
|
+
recording = runAsync(scrcpyCommand, scrcpyArguments);
|
|
347
|
+
} else {
|
|
348
|
+
recording = runAsync('adb', [
|
|
349
|
+
'-s', deviceId,
|
|
350
|
+
'shell', 'screenrecord',
|
|
351
|
+
'--bit-rate', '12000000',
|
|
352
|
+
'--time-limit', String(duration),
|
|
353
|
+
deviceRecordingPath,
|
|
354
|
+
]);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
let manualStopRequested = false;
|
|
358
|
+
console.log('MANUAL RECORDING — use the device, then press Enter here to stop and convert.');
|
|
359
|
+
const stopReason = await waitForManualStop(recording.completion, duration);
|
|
360
|
+
|
|
361
|
+
if (stopReason === 'enter') {
|
|
362
|
+
manualStopRequested = true;
|
|
363
|
+
console.log('Enter pressed — stopping and finalizing the recording...');
|
|
364
|
+
if (audio === 'with') {
|
|
365
|
+
run('adb', [
|
|
366
|
+
'-s', deviceId,
|
|
367
|
+
'shell', 'pkill', '-TERM', '-f', 'com.genymobile.scrcpy.Server',
|
|
368
|
+
]);
|
|
369
|
+
} else {
|
|
370
|
+
run('adb', ['-s', deviceId, 'shell', 'pkill', '-INT', 'screenrecord']);
|
|
371
|
+
}
|
|
372
|
+
} else if (stopReason === 'maximum-duration') {
|
|
373
|
+
console.log(`Safety maximum reached after ${duration} seconds.`);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
await recording.completion;
|
|
378
|
+
} catch (error) {
|
|
379
|
+
if (!manualStopRequested) throw error;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (audio === 'without') {
|
|
383
|
+
try {
|
|
384
|
+
run('adb', ['-s', deviceId, 'pull', deviceRecordingPath, destinationPath], { inherit: true });
|
|
385
|
+
} finally {
|
|
386
|
+
try {
|
|
387
|
+
run('adb', ['-s', deviceId, 'shell', 'rm', deviceRecordingPath]);
|
|
388
|
+
} catch {
|
|
389
|
+
// The device may have disconnected after recording.
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
console.log('RECORDING FINISHED — converting the captured video.');
|
|
395
|
+
console.log(`Recorded connected device ${deviceId}: ${destinationPath}`);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function getUniqueOutputPath(profile, name) {
|
|
399
|
+
const profileDirectory = path.join(outputDirectory, profile.directory);
|
|
400
|
+
fs.mkdirSync(profileDirectory, { recursive: true });
|
|
401
|
+
|
|
402
|
+
let outputPath = path.join(profileDirectory, `${name}-${profile.fileSuffix}.mp4`);
|
|
403
|
+
let duplicateNumber = 2;
|
|
404
|
+
|
|
405
|
+
while (fs.existsSync(outputPath)) {
|
|
406
|
+
outputPath = path.join(
|
|
407
|
+
profileDirectory,
|
|
408
|
+
`${name}-${profile.fileSuffix}-${duplicateNumber}.mp4`,
|
|
409
|
+
);
|
|
410
|
+
duplicateNumber += 1;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return outputPath;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function getVideoDetails(ffprobeCommand, videoPath) {
|
|
417
|
+
const output = run(ffprobeCommand, [
|
|
418
|
+
'-v', 'error',
|
|
419
|
+
'-show_entries', 'stream=width,height,codec_name,codec_type,bit_rate',
|
|
420
|
+
'-show_entries', 'format=duration,size,bit_rate',
|
|
421
|
+
'-of', 'json',
|
|
422
|
+
videoPath,
|
|
423
|
+
]);
|
|
424
|
+
return JSON.parse(output);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function validateVideo(ffprobeCommand, videoPath, profile) {
|
|
428
|
+
const details = getVideoDetails(ffprobeCommand, videoPath);
|
|
429
|
+
const stream = details.streams?.find((candidate) => candidate.codec_type === 'video') || {};
|
|
430
|
+
const format = details.format || {};
|
|
431
|
+
const bitRate = Number(stream.bit_rate || format.bit_rate || 0);
|
|
432
|
+
|
|
433
|
+
if (stream.codec_name !== 'h264') {
|
|
434
|
+
throw new Error(`Validation failed for ${videoPath}: expected H.264 video.`);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (Number(stream.width) !== profile.width || Number(stream.height) !== profile.height) {
|
|
438
|
+
throw new Error(
|
|
439
|
+
`Validation failed for ${videoPath}: expected ${profile.width}x${profile.height}.`,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (profile === videoProfiles.amazon && bitRate < 1200000) {
|
|
444
|
+
throw new Error(`Amazon video bitrate is below 1200 kbps: ${Math.round(bitRate / 1000)} kbps.`);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
console.log(
|
|
448
|
+
`Validated ${profile.label}: ${stream.width}x${stream.height}, H.264, `
|
|
449
|
+
+ `${(Number(format.duration) || 0).toFixed(1)}s, ${Math.round(bitRate / 1000)} kbps.`,
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function createVideoVariant(
|
|
454
|
+
ffmpegCommand,
|
|
455
|
+
ffprobeCommand,
|
|
456
|
+
inputPath,
|
|
457
|
+
profile,
|
|
458
|
+
name,
|
|
459
|
+
requireAudio,
|
|
460
|
+
) {
|
|
461
|
+
const destinationPath = getUniqueOutputPath(profile, name);
|
|
462
|
+
const videoFilter = [
|
|
463
|
+
'setparams=colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=tv',
|
|
464
|
+
`scale=${profile.width}:${profile.height}:force_original_aspect_ratio=decrease`,
|
|
465
|
+
`pad=${profile.width}:${profile.height}:(ow-iw)/2:(oh-ih)/2:color=black`,
|
|
466
|
+
'setsar=1',
|
|
467
|
+
].join(',');
|
|
468
|
+
|
|
469
|
+
run(ffmpegCommand, [
|
|
470
|
+
'-hide_banner',
|
|
471
|
+
'-y',
|
|
472
|
+
'-i', inputPath,
|
|
473
|
+
'-map', '0:v:0',
|
|
474
|
+
'-map', '0:a?',
|
|
475
|
+
'-vf', videoFilter,
|
|
476
|
+
'-c:v', 'libx264',
|
|
477
|
+
'-preset', 'medium',
|
|
478
|
+
'-profile:v', 'high',
|
|
479
|
+
'-level', profile.h264Level,
|
|
480
|
+
'-pix_fmt', 'yuv420p',
|
|
481
|
+
'-b:v', profile.videoBitrate,
|
|
482
|
+
'-minrate', profile.videoBitrate,
|
|
483
|
+
'-maxrate', profile.videoBitrate,
|
|
484
|
+
'-bufsize', profile.videoBitrate,
|
|
485
|
+
'-x264-params', 'nal-hrd=cbr:force-cfr=1',
|
|
486
|
+
'-r', '30',
|
|
487
|
+
'-c:a', 'aac',
|
|
488
|
+
'-b:a', '192k',
|
|
489
|
+
'-movflags', '+faststart',
|
|
490
|
+
destinationPath,
|
|
491
|
+
], { inherit: true });
|
|
492
|
+
|
|
493
|
+
console.log(`Created ${profile.label}: ${destinationPath}`);
|
|
494
|
+
validateVideo(ffprobeCommand, destinationPath, profile);
|
|
495
|
+
|
|
496
|
+
if (requireAudio) {
|
|
497
|
+
const details = getVideoDetails(ffprobeCommand, destinationPath);
|
|
498
|
+
const audioStream = details.streams?.find((stream) => stream.codec_type === 'audio');
|
|
499
|
+
if (!audioStream) {
|
|
500
|
+
throw new Error(`Validation failed for ${destinationPath}: audio track is missing.`);
|
|
501
|
+
}
|
|
502
|
+
console.log(`Validated audio: ${audioStream.codec_name} track included.`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function copySpatialSource(spatialInput, name) {
|
|
507
|
+
if (!spatialInput) {
|
|
508
|
+
throw new Error(
|
|
509
|
+
'Spatial XR requires --spatial-input with a genuine 360-degree, 180-degree, or 3D video.',
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const sourcePath = path.resolve(spatialInput);
|
|
514
|
+
if (!fs.existsSync(sourcePath) || !fs.statSync(sourcePath).isFile()) {
|
|
515
|
+
throw new Error(`Spatial XR source was not found: ${sourcePath}`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const spatialDirectory = path.join(outputDirectory, 'Google-Play-XR-Spatial');
|
|
519
|
+
fs.mkdirSync(spatialDirectory, { recursive: true });
|
|
520
|
+
const extension = path.extname(sourcePath) || '.mp4';
|
|
521
|
+
let destinationPath = path.join(spatialDirectory, `${name}-spatial-xr${extension}`);
|
|
522
|
+
let duplicateNumber = 2;
|
|
523
|
+
|
|
524
|
+
while (fs.existsSync(destinationPath)) {
|
|
525
|
+
destinationPath = path.join(
|
|
526
|
+
spatialDirectory,
|
|
527
|
+
`${name}-spatial-xr-${duplicateNumber}${extension}`,
|
|
528
|
+
);
|
|
529
|
+
duplicateNumber += 1;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
fs.copyFileSync(sourcePath, destinationPath, fs.constants.COPYFILE_EXCL);
|
|
533
|
+
console.log(`Copied genuine Spatial XR source without stripping its metadata: ${destinationPath}`);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
async function main() {
|
|
537
|
+
const options = parseArguments();
|
|
538
|
+
|
|
539
|
+
if (options.help) {
|
|
540
|
+
printHelp();
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (options.list) {
|
|
545
|
+
printProfiles();
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const selectedProfiles = selectProfiles(options.profiles);
|
|
550
|
+
const flatProfiles = selectedProfiles.filter((name) => name !== 'spatial-xr');
|
|
551
|
+
|
|
552
|
+
if (selectedProfiles.includes('spatial-xr')) {
|
|
553
|
+
copySpatialSource(options.spatialInput, options.name);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (!flatProfiles.length) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const ffmpegCommand = getExecutable('ffmpeg');
|
|
561
|
+
const ffprobeCommand = getExecutable('ffprobe');
|
|
562
|
+
|
|
563
|
+
if (!ffmpegCommand || !ffprobeCommand) {
|
|
564
|
+
throw new Error(
|
|
565
|
+
'FFmpeg was not found in PATH. Install FFmpeg, reopen the terminal, and run this script again.',
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
let inputPath;
|
|
570
|
+
if (options.input) {
|
|
571
|
+
inputPath = path.resolve(options.input);
|
|
572
|
+
if (!fs.existsSync(inputPath) || !fs.statSync(inputPath).isFile()) {
|
|
573
|
+
throw new Error(`Input video was not found: ${inputPath}`);
|
|
574
|
+
}
|
|
575
|
+
} else {
|
|
576
|
+
inputPath = path.join(temporaryDirectory, `${options.name}-adb-source.mp4`);
|
|
577
|
+
await recordConnectedDevice(options, inputPath);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
for (const profileName of flatProfiles) {
|
|
581
|
+
createVideoVariant(
|
|
582
|
+
ffmpegCommand,
|
|
583
|
+
ffprobeCommand,
|
|
584
|
+
inputPath,
|
|
585
|
+
videoProfiles[profileName],
|
|
586
|
+
options.name,
|
|
587
|
+
options.audio === 'with',
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
main().catch((error) => {
|
|
593
|
+
console.error(`Store video creation failed: ${error.message}`);
|
|
594
|
+
process.exitCode = 1;
|
|
595
|
+
});
|
package/package.json
CHANGED
package/files/take-screenshot.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
const fs = require('node:fs');
|
|
2
|
-
const path = require('node:path');
|
|
3
|
-
const readline = require('node:readline');
|
|
4
|
-
const { spawnSync } = require('node:child_process');
|
|
5
|
-
|
|
6
|
-
const screenshotDirectory = path.join(__dirname, 'Auto-Screenshot');
|
|
7
|
-
|
|
8
|
-
function getNextNumberedPath() {
|
|
9
|
-
const highestNumber = fs.readdirSync(screenshotDirectory, { withFileTypes: true })
|
|
10
|
-
.filter((entry) => entry.isFile())
|
|
11
|
-
.reduce((highest, entry) => {
|
|
12
|
-
const match = entry.name.match(/^(\d+)\.png$/i);
|
|
13
|
-
return match ? Math.max(highest, Number(match[1])) : highest;
|
|
14
|
-
}, 0);
|
|
15
|
-
|
|
16
|
-
return path.join(screenshotDirectory, `${highestNumber + 1}.png`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function getCustomPath(input) {
|
|
20
|
-
let fileName = input.trim().replace(/\.png$/i, '');
|
|
21
|
-
fileName = fileName
|
|
22
|
-
.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '_')
|
|
23
|
-
.replace(/[. ]+$/g, '');
|
|
24
|
-
|
|
25
|
-
if (!fileName) {
|
|
26
|
-
return getNextNumberedPath();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(fileName)) {
|
|
30
|
-
fileName = `${fileName}_`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let screenshotPath = path.join(screenshotDirectory, `${fileName}.png`);
|
|
34
|
-
let duplicateNumber = 2;
|
|
35
|
-
|
|
36
|
-
while (fs.existsSync(screenshotPath)) {
|
|
37
|
-
screenshotPath = path.join(
|
|
38
|
-
screenshotDirectory,
|
|
39
|
-
`${fileName}-${duplicateNumber}.png`,
|
|
40
|
-
);
|
|
41
|
-
duplicateNumber += 1;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return screenshotPath;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function takeScreenshot(fileNameInput) {
|
|
48
|
-
const result = spawnSync('adb', ['exec-out', 'screencap', '-p'], {
|
|
49
|
-
encoding: null,
|
|
50
|
-
maxBuffer: 100 * 1024 * 1024,
|
|
51
|
-
timeout: 30000,
|
|
52
|
-
windowsHide: true,
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
if (result.error) {
|
|
56
|
-
if (result.error.code === 'ENOENT') {
|
|
57
|
-
throw new Error('ADB was not found. Add Android platform-tools to your PATH.');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (result.error.code === 'ETIMEDOUT') {
|
|
61
|
-
throw new Error('ADB did not respond within 30 seconds. Check the device connection.');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
throw result.error;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (result.status !== 0) {
|
|
68
|
-
const adbMessage = result.stderr.toString().trim();
|
|
69
|
-
throw new Error(adbMessage || `ADB exited with code ${result.status}.`);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!result.stdout.length) {
|
|
73
|
-
throw new Error('ADB returned an empty screenshot. Check the connected device.');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const screenshotPath = fileNameInput.trim()
|
|
77
|
-
? getCustomPath(fileNameInput)
|
|
78
|
-
: getNextNumberedPath();
|
|
79
|
-
|
|
80
|
-
fs.writeFileSync(screenshotPath, result.stdout, { flag: 'wx' });
|
|
81
|
-
return screenshotPath;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
fs.mkdirSync(screenshotDirectory, { recursive: true });
|
|
85
|
-
|
|
86
|
-
const terminal = readline.createInterface({
|
|
87
|
-
input: process.stdin,
|
|
88
|
-
output: process.stdout,
|
|
89
|
-
prompt: 'Enter a filename, or press Enter for the next number: ',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
console.log(`Screenshots will be saved in: ${screenshotDirectory}`);
|
|
93
|
-
console.log('Press Ctrl+C to exit.');
|
|
94
|
-
terminal.prompt();
|
|
95
|
-
|
|
96
|
-
terminal.on('line', (fileNameInput) => {
|
|
97
|
-
try {
|
|
98
|
-
const screenshotPath = takeScreenshot(fileNameInput);
|
|
99
|
-
console.log(`Screenshot saved: ${screenshotPath}`);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.error(`Screenshot failed: ${error.message}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
terminal.prompt();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
terminal.on('SIGINT', () => {
|
|
108
|
-
console.log('\nScreenshot tool closed.');
|
|
109
|
-
terminal.close();
|
|
110
|
-
});
|