chat-about-video
Version:
Chat about a video clip using ChatGPT hosted in OpenAI or Azure, or Gemini provided by Google
71 lines (70 loc) • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findCommonParentPath = exports.effectiveStorageOptions = exports.effectiveExtractVideoFramesOptions = void 0;
const tslib_1 = require("tslib");
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const storage_1 = require("./storage");
const video_1 = require("./video");
/**
* Calculate the effective values for ExtractVideoFramesOptions by combining the default values and the values provided
* @param options the options containing the values provided
* @returns The effective values for ExtractVideoFramesOptions
*/
function effectiveExtractVideoFramesOptions(options) {
return Object.assign({ extractor: video_1.extractVideoFramesWithFfmpeg, framesDirectoryResolver: (_inputFile, tmpDir, conversationId) => node_path_1.default.join(tmpDir, conversationId), format: 'jpg', interval: 5, limit: 10, width: 200, deleteFilesWhenConversationEnds: true }, options);
}
exports.effectiveExtractVideoFramesOptions = effectiveExtractVideoFramesOptions;
/**
* Calculate the effective values for StorageOptions by combining the default values and the values provided
* @param options the options containing the values provided
* @returns The effective values for StorageOptions
*/
function effectiveStorageOptions(options) {
var _a;
let uploader = options.uploader;
const downloadUrlExpirationSeconds = (_a = options.downloadUrlExpirationSeconds) !== null && _a !== void 0 ? _a : 3600;
if (!uploader) {
// eslint-disable-next-line unicorn/prefer-ternary
if (options.azureStorageConnectionString) {
// use Azure
uploader = (0, storage_1.lazyCreatedFileBatchUploader)(Promise.all([Promise.resolve().then(() => tslib_1.__importStar(require('./azure'))), Promise.resolve().then(() => tslib_1.__importStar(require('@azure/storage-blob')))]).then(([azure, storageBlob]) => azure.createAzureBlobStorageFileBatchUploader(storageBlob.BlobServiceClient.fromConnectionString(options.azureStorageConnectionString), downloadUrlExpirationSeconds)));
}
else {
// use AWS
uploader = (0, storage_1.lazyCreatedFileBatchUploader)(Promise.all([Promise.resolve().then(() => tslib_1.__importStar(require('./aws'))), Promise.resolve().then(() => tslib_1.__importStar(require('@aws-sdk/client-s3')))]).then(([aws, clientS3]) => aws.createAwsS3FileBatchUploader(new clientS3.S3Client(), downloadUrlExpirationSeconds)));
}
}
return Object.assign({ uploader, storagePathPrefix: '', deleteFilesWhenConversationEnds: true }, options);
}
exports.effectiveStorageOptions = effectiveStorageOptions;
/**
* Find the common parent path of the given paths.
* If there is no common parent path, then the root path of the current process will be returned.
* @param paths Input paths to find the common parent path for. It can be absolute or relative paths.
* @returns The common parent path and the relative paths from the common parent.
*/
function findCommonParentPath(paths) {
const rootPath = node_path_1.default.parse(process.cwd()).root;
if (paths.length === 0) {
return { commonParent: rootPath, relativePaths: [] };
}
// Handles both absolute and relative input
const absolutePaths = paths.map((p) => node_path_1.default.resolve(p));
const splitPaths = absolutePaths.map((p) => node_path_1.default.parse(p).dir.split(node_path_1.default.sep));
const commonParts = [];
for (let i = 0;; i++) {
const currentPart = splitPaths[0][i];
if (currentPart === undefined)
break;
if (splitPaths.every((parts) => parts[i] === currentPart)) {
commonParts.push(currentPart);
}
else {
break;
}
}
const commonParent = commonParts.length > 0 ? commonParts.join(node_path_1.default.sep) || rootPath : rootPath;
const relativePaths = absolutePaths.map((p) => node_path_1.default.relative(commonParent, p));
return { commonParent, relativePaths };
}
exports.findCommonParentPath = findCommonParentPath;