Skip to content

Commit

Permalink
Add video compression using the ffmpeg used by cypress internally
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvinjaiswal committed Sep 4, 2024
1 parent edb0750 commit 15306af
Show file tree
Hide file tree
Showing 4 changed files with 4,711 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ class Reporter {
async sendVideo(suiteInfo) {
const { waitForVideoTimeout, waitForVideoInterval, videosFolder } = this.config;
const { testFileName, tempId, title } = suiteInfo;
const { videoCompression } = this.fullCypressConfig;
const file = await getVideoFile(
testFileName,
videoCompression,
videosFolder,
waitForVideoTimeout,
waitForVideoInterval,
Expand Down
29 changes: 29 additions & 0 deletions lib/utils/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
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;

Expand Down Expand Up @@ -96,8 +102,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 +139,9 @@ const getVideoFile = async (

try {
videoFilePath = await waitForVideoFile(globFilePath, timeout, interval);
if (videoCompression && typeof videoCompression === 'number' && videoCompression < 52) {
videoFilePath = await compressVideo(videoFilePath, videoCompression);
}
} catch (e) {
console.warn(e.message);
return null;
Expand Down
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
Loading

0 comments on commit 15306af

Please sign in to comment.