Skip to content

Commit

Permalink
Add video compression using the ffmpeg used by Cypress internally (#205)
Browse files Browse the repository at this point in the history
* Add video compression using the ffmpeg used by cypress internally

* Add video compression option to agent
  • Loading branch information
ashvinjaiswal authored Sep 6, 2024
1 parent edb0750 commit 5b4ac86
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ class Reporter {
}

async sendVideo(suiteInfo) {
const { waitForVideoTimeout, waitForVideoInterval, videosFolder } = this.config;
const { waitForVideoTimeout, waitForVideoInterval, videosFolder, videoCompression } =
this.config;
const { testFileName, tempId, title } = suiteInfo;
const file = await getVideoFile(
testFileName,
videoCompression,
videosFolder,
waitForVideoTimeout,
waitForVideoInterval,
Expand Down
34 changes: 34 additions & 0 deletions lib/utils/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
const ffprobeStatic = require('ffprobe-static');

ffmpeg.setFfmpegPath(ffmpegInstaller.path);
ffmpeg.setFfprobePath(ffprobeStatic.path);

const fsPromises = fs.promises;

const DEFAULT_WAIT_FOR_FILE_TIMEOUT = 10000;
const DEFAULT_WAIT_FOR_FILE_INTERVAL = 500;
const DEFAULT_CRF = 32;
const MAX_CRF = 52;

const getScreenshotAttachment = async (absolutePath) => {
if (!absolutePath) return absolutePath;
Expand Down Expand Up @@ -96,8 +104,28 @@ const waitForVideoFile = (
checkFileExistsAndReady().catch(reject);
});

const compressVideo = (filePath, crfValue) => {
return new Promise((resolve, reject) => {
const outputFilePath = path.join(
path.dirname(filePath),
`compressed_${path.basename(filePath)}`,
);

ffmpeg(filePath)
.outputOptions(`-crf ${crfValue}`)
.save(outputFilePath)
.on('end', () => {
resolve(outputFilePath);
})
.on('error', (err) => {
reject(err);
});
});
};

const getVideoFile = async (
specFileName,
videoCompression,
videosFolder = '**',
timeout = DEFAULT_WAIT_FOR_FILE_TIMEOUT,
interval = DEFAULT_WAIT_FOR_FILE_INTERVAL,
Expand All @@ -113,6 +141,12 @@ const getVideoFile = async (

try {
videoFilePath = await waitForVideoFile(globFilePath, timeout, interval);

if (typeof videoCompression === 'boolean' && videoCompression) {
videoFilePath = await compressVideo(videoFilePath, DEFAULT_CRF);
} else if (typeof videoCompression === 'number' && videoCompression < MAX_CRF) {
videoFilePath = await compressVideo(videoFilePath, videoCompression);
}
} catch (e) {
console.warn(e.message);
return null;
Expand Down
211 changes: 204 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^23.20.0",
"eslint-plugin-prettier": "^4.2.1",
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"ffmpeg": "^0.0.4",
"ffprobe-static": "^3.1.0",
"fluent-ffmpeg": "^2.1.3",
"jest": "^29.7.0",
"mock-fs": "^4.14.0",
"prettier": "^2.8.8"
Expand Down

0 comments on commit 5b4ac86

Please sign in to comment.