gitbutler/apps/desktop/e2e/record.ts

70 lines
1.6 KiB
TypeScript
Raw Normal View History

import { spawn, type ChildProcessWithoutNullStreams } from 'child_process';
2024-08-16 15:51:33 +03:00
import path from 'node:path';
import type { Frameworks } from '@wdio/types';
function fileName(title: string) {
return encodeURIComponent(title.trim().replace(/\s+/g, '-'));
}
export class TestRecorder {
ffmpeg!: ChildProcessWithoutNullStreams;
constructor() {}
stop() {
this.ffmpeg?.kill('SIGINT');
}
start(test: Frameworks.Test, videoPath: string) {
2024-08-16 15:51:33 +03:00
if (!videoPath || !test) {
throw new Error('Cannot start recording without a test and path for the video file.');
}
if (process.env.DISPLAY && process.env.DISPLAY.startsWith(':')) {
2024-08-16 15:51:33 +03:00
const parsedPath = path.join(
videoPath,
2024-08-16 15:51:33 +03:00
`${fileName(test.parent)}-${fileName(test.title)}.mp4`
);
this.ffmpeg = spawn('ffmpeg', [
'-f',
2024-08-16 15:51:33 +03:00
'x11grab',
'-video_size',
2024-08-16 15:51:33 +03:00
'1280x1024',
'-i',
2024-08-16 15:51:33 +03:00
process.env.DISPLAY,
'-loglevel',
2024-08-16 15:51:33 +03:00
'error',
'-y',
'-pix_fmt',
2024-08-16 15:51:33 +03:00
'yuv420p',
parsedPath
]);
2024-08-16 15:51:33 +03:00
function logBuffer(buffer: Buffer, prefix: string) {
const lines = buffer.toString().trim().split('\n');
lines.forEach(function (line) {
console.log(prefix + line);
});
2024-08-16 15:51:33 +03:00
}
this.ffmpeg.stdout.on('data', (data: Buffer) => {
logBuffer(data, '[ffmpeg:stdout] ');
});
this.ffmpeg.stderr.on('data', (data: Buffer) => {
logBuffer(data, '[ffmpeg:error] ');
});
2024-08-16 15:51:33 +03:00
this.ffmpeg.on('close', (code?: number, signal?: string) => {
if (code) {
console.log(`[ffmpeg:stdout] exited with code ${code}: ${videoPath}`);
}
2024-08-16 15:51:33 +03:00
if (signal) {
console.log(`[ffmpeg:stdout] received signal ${signal}: ${videoPath}`);
}
});
}
}
}