@vertesia/workflow 0.78.0 → 0.79.1
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.
- package/lib/cjs/activities/advanced/createDocumentTypeFromInteractionRun.js +8 -7
- package/lib/cjs/activities/advanced/createDocumentTypeFromInteractionRun.js.map +1 -1
- package/lib/cjs/activities/advanced/createOrUpdateDocumentFromInteractionRun.js +10 -9
- package/lib/cjs/activities/advanced/createOrUpdateDocumentFromInteractionRun.js.map +1 -1
- package/lib/cjs/activities/advanced/updateDocumentFromInteractionRun.js +2 -1
- package/lib/cjs/activities/advanced/updateDocumentFromInteractionRun.js.map +1 -1
- package/lib/cjs/activities/chunkDocument.js +2 -1
- package/lib/cjs/activities/chunkDocument.js.map +1 -1
- package/lib/cjs/activities/executeInteraction.js +11 -7
- package/lib/cjs/activities/executeInteraction.js.map +1 -1
- package/lib/cjs/activities/generateDocumentProperties.js +11 -6
- package/lib/cjs/activities/generateDocumentProperties.js.map +1 -1
- package/lib/cjs/activities/generateOrAssignContentType.js +12 -10
- package/lib/cjs/activities/generateOrAssignContentType.js.map +1 -1
- package/lib/cjs/activities/index-dsl.js +5 -3
- package/lib/cjs/activities/index-dsl.js.map +1 -1
- package/lib/cjs/activities/media/prepareVideo.js +429 -0
- package/lib/cjs/activities/media/prepareVideo.js.map +1 -0
- package/lib/cjs/activities/media/transcribeMediaWithGladia.js +48 -15
- package/lib/cjs/activities/media/transcribeMediaWithGladia.js.map +1 -1
- package/lib/cjs/activities/notifyWebhook.js +137 -12
- package/lib/cjs/activities/notifyWebhook.js.map +1 -1
- package/lib/cjs/activities/rateLimiter.js +30 -0
- package/lib/cjs/activities/rateLimiter.js.map +1 -0
- package/lib/cjs/conversion/image.js +4 -2
- package/lib/cjs/conversion/image.js.map +1 -1
- package/lib/cjs/dsl/dsl-workflow.js +66 -0
- package/lib/cjs/dsl/dsl-workflow.js.map +1 -1
- package/lib/cjs/dsl/setup/ActivityContext.js +7 -4
- package/lib/cjs/dsl/setup/ActivityContext.js.map +1 -1
- package/lib/cjs/errors.js +22 -1
- package/lib/cjs/errors.js.map +1 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/iterative-generation/activities/extractToc.js +2 -2
- package/lib/cjs/iterative-generation/activities/extractToc.js.map +1 -1
- package/lib/cjs/iterative-generation/activities/finalizeOutput.js +7 -4
- package/lib/cjs/iterative-generation/activities/finalizeOutput.js.map +1 -1
- package/lib/cjs/iterative-generation/activities/generatePart.js +18 -13
- package/lib/cjs/iterative-generation/activities/generatePart.js.map +1 -1
- package/lib/cjs/iterative-generation/activities/generateToc.js +50 -55
- package/lib/cjs/iterative-generation/activities/generateToc.js.map +1 -1
- package/lib/cjs/iterative-generation/utils.js.map +1 -1
- package/lib/cjs/system/notifyWebhookWorkflow.js +10 -3
- package/lib/cjs/system/notifyWebhookWorkflow.js.map +1 -1
- package/lib/cjs/utils/blobs.js +4 -1
- package/lib/cjs/utils/blobs.js.map +1 -1
- package/lib/cjs/utils/client.js +3 -1
- package/lib/cjs/utils/client.js.map +1 -1
- package/lib/esm/activities/advanced/createDocumentTypeFromInteractionRun.js +8 -7
- package/lib/esm/activities/advanced/createDocumentTypeFromInteractionRun.js.map +1 -1
- package/lib/esm/activities/advanced/createOrUpdateDocumentFromInteractionRun.js +10 -9
- package/lib/esm/activities/advanced/createOrUpdateDocumentFromInteractionRun.js.map +1 -1
- package/lib/esm/activities/advanced/updateDocumentFromInteractionRun.js +2 -1
- package/lib/esm/activities/advanced/updateDocumentFromInteractionRun.js.map +1 -1
- package/lib/esm/activities/chunkDocument.js +2 -1
- package/lib/esm/activities/chunkDocument.js.map +1 -1
- package/lib/esm/activities/executeInteraction.js +12 -8
- package/lib/esm/activities/executeInteraction.js.map +1 -1
- package/lib/esm/activities/generateDocumentProperties.js +11 -6
- package/lib/esm/activities/generateDocumentProperties.js.map +1 -1
- package/lib/esm/activities/generateOrAssignContentType.js +12 -10
- package/lib/esm/activities/generateOrAssignContentType.js.map +1 -1
- package/lib/esm/activities/index-dsl.js +2 -1
- package/lib/esm/activities/index-dsl.js.map +1 -1
- package/lib/esm/activities/media/prepareVideo.js +390 -0
- package/lib/esm/activities/media/prepareVideo.js.map +1 -0
- package/lib/esm/activities/media/transcribeMediaWithGladia.js +50 -17
- package/lib/esm/activities/media/transcribeMediaWithGladia.js.map +1 -1
- package/lib/esm/activities/notifyWebhook.js +137 -12
- package/lib/esm/activities/notifyWebhook.js.map +1 -1
- package/lib/esm/activities/rateLimiter.js +27 -0
- package/lib/esm/activities/rateLimiter.js.map +1 -0
- package/lib/esm/conversion/image.js +4 -2
- package/lib/esm/conversion/image.js.map +1 -1
- package/lib/esm/dsl/dsl-workflow.js +68 -2
- package/lib/esm/dsl/dsl-workflow.js.map +1 -1
- package/lib/esm/dsl/setup/ActivityContext.js +10 -7
- package/lib/esm/dsl/setup/ActivityContext.js.map +1 -1
- package/lib/esm/errors.js +19 -0
- package/lib/esm/errors.js.map +1 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/iterative-generation/activities/extractToc.js +3 -3
- package/lib/esm/iterative-generation/activities/extractToc.js.map +1 -1
- package/lib/esm/iterative-generation/activities/finalizeOutput.js +9 -6
- package/lib/esm/iterative-generation/activities/finalizeOutput.js.map +1 -1
- package/lib/esm/iterative-generation/activities/generatePart.js +19 -14
- package/lib/esm/iterative-generation/activities/generatePart.js.map +1 -1
- package/lib/esm/iterative-generation/activities/generateToc.js +50 -55
- package/lib/esm/iterative-generation/activities/generateToc.js.map +1 -1
- package/lib/esm/iterative-generation/utils.js.map +1 -1
- package/lib/esm/system/notifyWebhookWorkflow.js +11 -4
- package/lib/esm/system/notifyWebhookWorkflow.js.map +1 -1
- package/lib/esm/utils/blobs.js +4 -1
- package/lib/esm/utils/blobs.js.map +1 -1
- package/lib/esm/utils/client.js +4 -2
- package/lib/esm/utils/client.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/activities/advanced/createDocumentTypeFromInteractionRun.d.ts.map +1 -1
- package/lib/types/activities/advanced/createOrUpdateDocumentFromInteractionRun.d.ts +1 -1
- package/lib/types/activities/advanced/createOrUpdateDocumentFromInteractionRun.d.ts.map +1 -1
- package/lib/types/activities/advanced/updateDocumentFromInteractionRun.d.ts.map +1 -1
- package/lib/types/activities/chunkDocument.d.ts.map +1 -1
- package/lib/types/activities/executeInteraction.d.ts +5 -1
- package/lib/types/activities/executeInteraction.d.ts.map +1 -1
- package/lib/types/activities/generateDocumentProperties.d.ts.map +1 -1
- package/lib/types/activities/generateOrAssignContentType.d.ts.map +1 -1
- package/lib/types/activities/index-dsl.d.ts +2 -1
- package/lib/types/activities/index-dsl.d.ts.map +1 -1
- package/lib/types/activities/media/prepareVideo.d.ts +30 -0
- package/lib/types/activities/media/prepareVideo.d.ts.map +1 -0
- package/lib/types/activities/media/transcribeMediaWithGladia.d.ts.map +1 -1
- package/lib/types/activities/notifyWebhook.d.ts +14 -3
- package/lib/types/activities/notifyWebhook.d.ts.map +1 -1
- package/lib/types/activities/rateLimiter.d.ts +11 -0
- package/lib/types/activities/rateLimiter.d.ts.map +1 -0
- package/lib/types/conversion/image.d.ts.map +1 -1
- package/lib/types/dsl/dsl-workflow.d.ts.map +1 -1
- package/lib/types/dsl/setup/ActivityContext.d.ts.map +1 -1
- package/lib/types/errors.d.ts +10 -0
- package/lib/types/errors.d.ts.map +1 -1
- package/lib/types/index.d.ts +2 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/iterative-generation/activities/extractToc.d.ts.map +1 -1
- package/lib/types/iterative-generation/activities/finalizeOutput.d.ts.map +1 -1
- package/lib/types/iterative-generation/activities/generatePart.d.ts.map +1 -1
- package/lib/types/iterative-generation/activities/generateToc.d.ts.map +1 -1
- package/lib/types/iterative-generation/utils.d.ts +3 -4
- package/lib/types/iterative-generation/utils.d.ts.map +1 -1
- package/lib/types/system/notifyWebhookWorkflow.d.ts +3 -2
- package/lib/types/system/notifyWebhookWorkflow.d.ts.map +1 -1
- package/lib/types/utils/blobs.d.ts.map +1 -1
- package/lib/types/utils/client.d.ts +2 -6
- package/lib/types/utils/client.d.ts.map +1 -1
- package/lib/workflows-bundle.js +8413 -5201
- package/package.json +128 -120
- package/src/activities/advanced/createDocumentTypeFromInteractionRun.ts +9 -8
- package/src/activities/advanced/createOrUpdateDocumentFromInteractionRun.ts +11 -9
- package/src/activities/advanced/updateDocumentFromInteractionRun.ts +2 -1
- package/src/activities/chunkDocument.ts +3 -1
- package/src/activities/executeInteraction.ts +23 -14
- package/src/activities/generateDocumentProperties.ts +12 -7
- package/src/activities/generateOrAssignContentType.ts +16 -11
- package/src/activities/index-dsl.ts +2 -1
- package/src/activities/media/prepareVideo.ts +622 -0
- package/src/activities/media/transcribeMediaWithGladia.ts +52 -21
- package/src/activities/notifyWebhook.test.ts +121 -19
- package/src/activities/notifyWebhook.ts +165 -16
- package/src/activities/rateLimiter.ts +41 -0
- package/src/conversion/image.ts +6 -3
- package/src/dsl/dsl-workflow.ts +86 -0
- package/src/dsl/workflow-exec-child.test.ts +1 -0
- package/src/dsl/workflow.test.ts +1 -0
- package/src/errors.ts +28 -0
- package/src/index.ts +2 -1
- package/src/iterative-generation/activities/generatePart.ts +1 -1
- package/src/iterative-generation/activities/generateToc.ts +7 -2
- package/src/iterative-generation/utils.ts +4 -5
- package/src/system/notifyWebhookWorkflow.ts +15 -6
- package/lib/cjs/activities/identifyTextSections.js +0 -48
- package/lib/cjs/activities/identifyTextSections.js.map +0 -1
- package/lib/esm/activities/identifyTextSections.js +0 -45
- package/lib/esm/activities/identifyTextSections.js.map +0 -1
- package/lib/types/activities/identifyTextSections.d.ts +0 -12
- package/lib/types/activities/identifyTextSections.d.ts.map +0 -1
- package/src/activities/identifyTextSections.ts +0 -71
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
import { log } from '@temporalio/activity';
|
|
2
|
+
import { DSLActivityExecutionPayload, DSLActivitySpec, VideoMetadata, VideoRendition, POSTER_RENDITION_NAME, AUDIO_RENDITION_NAME, WEB_VIDEO_RENDITION_NAME, ContentNature } from '@vertesia/common';
|
|
3
|
+
import { exec } from 'child_process';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import { promisify } from 'util';
|
|
8
|
+
import { setupActivity } from '../../dsl/setup/ActivityContext.js';
|
|
9
|
+
import { DocumentNotFoundError, InvalidContentTypeError } from '../../errors.js';
|
|
10
|
+
import { saveBlobToTempFile } from '../../utils/blobs.js';
|
|
11
|
+
import { VertesiaClient } from '@vertesia/client';
|
|
12
|
+
import { RequestError } from '@vertesia/api-fetch-client';
|
|
13
|
+
|
|
14
|
+
const execAsync = promisify(exec);
|
|
15
|
+
|
|
16
|
+
// Default configuration constants
|
|
17
|
+
const DEFAULT_MAX_RESOLUTION = 1920; // Max resolution for video rendition (produces 1080p)
|
|
18
|
+
const DEFAULT_THUMBNAIL_SIZE = 256; // Thumbnail longest side in pixels
|
|
19
|
+
const DEFAULT_POSTER_SIZE = 1920; // Poster longest side in pixels
|
|
20
|
+
const DEFAULT_GENERATE_AUDIO = true; // Generate audio-only rendition by default
|
|
21
|
+
|
|
22
|
+
// Timestamp calculation constants for screenshots
|
|
23
|
+
const THUMBNAIL_TIMESTAMP_RATIO = 0.25; // Extract thumbnail at 25% of video duration
|
|
24
|
+
const POSTER_TIMESTAMP_RATIO = 0.05; // Extract poster at 5% of video duration
|
|
25
|
+
const POSTER_TIMESTAMP_MAX = 2; // Maximum poster timestamp in seconds
|
|
26
|
+
const MIN_SCREENSHOT_TIMESTAMP = 1; // Minimum timestamp for screenshots in seconds
|
|
27
|
+
|
|
28
|
+
// FFmpeg configuration constants
|
|
29
|
+
const FFMPEG_MAX_BUFFER = 1024 * 1024 * 10; // 10MB buffer for ffmpeg output
|
|
30
|
+
const VIDEO_CRF = '23'; // Constant Rate Factor for video quality (18-28, lower = better)
|
|
31
|
+
const AUDIO_BITRATE = '128k'; // Audio bitrate for AAC encoding
|
|
32
|
+
const JPEG_QUALITY = '2'; // JPEG quality for screenshots (1-31, lower = better)
|
|
33
|
+
|
|
34
|
+
export interface PrepareVideoParams {
|
|
35
|
+
maxResolution?: number; // Max resolution (longest side) for video rendition, default 1920 (produces 1080p: 1920x1080 landscape or 1080x1920 portrait)
|
|
36
|
+
thumbnailSize?: number; // Max size (longest side) for thumbnail image, default 256
|
|
37
|
+
posterSize?: number; // Max size (longest side) for poster image, default 1920
|
|
38
|
+
generateAudio?: boolean; // Generate audio-only rendition (AAC), default true
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PrepareVideo extends DSLActivitySpec<PrepareVideoParams> {
|
|
42
|
+
name: 'prepareVideo';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface VideoMetadataExtended {
|
|
46
|
+
duration: number;
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
codec: string;
|
|
50
|
+
bitrate: number;
|
|
51
|
+
fps: number;
|
|
52
|
+
hasAudio: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface FFProbeStream {
|
|
56
|
+
codec_type: string;
|
|
57
|
+
codec_name?: string;
|
|
58
|
+
width?: number;
|
|
59
|
+
height?: number;
|
|
60
|
+
r_frame_rate?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface FFProbeFormat {
|
|
64
|
+
duration?: string;
|
|
65
|
+
bit_rate?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface FFProbeOutput {
|
|
69
|
+
streams: FFProbeStream[];
|
|
70
|
+
format: FFProbeFormat;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Extract comprehensive video metadata using ffprobe
|
|
75
|
+
*/
|
|
76
|
+
async function getVideoMetadata(videoPath: string): Promise<VideoMetadataExtended> {
|
|
77
|
+
try {
|
|
78
|
+
const command = `ffprobe -v quiet -print_format json -show_format -show_streams "${videoPath}"`;
|
|
79
|
+
const { stdout } = await execAsync(command);
|
|
80
|
+
const metadata = JSON.parse(stdout) as FFProbeOutput;
|
|
81
|
+
|
|
82
|
+
const videoStream = metadata.streams.find(
|
|
83
|
+
(stream) => stream.codec_type === 'video',
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (!videoStream) {
|
|
87
|
+
throw new Error('No video stream found in file');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const duration = parseFloat(metadata.format.duration ?? '0') || 0;
|
|
91
|
+
const width = videoStream.width || 0;
|
|
92
|
+
const height = videoStream.height || 0;
|
|
93
|
+
const codec = videoStream.codec_name || 'unknown';
|
|
94
|
+
const bitrate = parseInt(metadata.format.bit_rate ?? '0', 10) || 0;
|
|
95
|
+
|
|
96
|
+
// Calculate FPS from r_frame_rate (e.g., "30/1" or "24000/1001")
|
|
97
|
+
let fps = 0;
|
|
98
|
+
if (videoStream.r_frame_rate) {
|
|
99
|
+
const [num, den] = videoStream.r_frame_rate.split('/').map(Number);
|
|
100
|
+
fps = den > 0 ? num / den : 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Check if video has an audio stream
|
|
104
|
+
const audioStream = metadata.streams.find(
|
|
105
|
+
(stream) => stream.codec_type === 'audio',
|
|
106
|
+
);
|
|
107
|
+
const hasAudio = !!audioStream;
|
|
108
|
+
|
|
109
|
+
return { duration, width, height, codec, bitrate, fps, hasAudio };
|
|
110
|
+
} catch (error) {
|
|
111
|
+
log.error(
|
|
112
|
+
`Failed to get video metadata: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
113
|
+
);
|
|
114
|
+
throw new Error(
|
|
115
|
+
`Failed to probe video metadata: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Calculate scaled dimensions maintaining aspect ratio
|
|
122
|
+
* Scales based on longest side, ensuring dimensions are even (required for H.264)
|
|
123
|
+
*/
|
|
124
|
+
function calculateScaledDimensions(
|
|
125
|
+
width: number,
|
|
126
|
+
height: number,
|
|
127
|
+
maxResolution: number,
|
|
128
|
+
): { width: number; height: number } {
|
|
129
|
+
const longestSide = Math.max(width, height);
|
|
130
|
+
let newWidth: number;
|
|
131
|
+
let newHeight: number;
|
|
132
|
+
|
|
133
|
+
if (longestSide > maxResolution) {
|
|
134
|
+
// Scale down if video is larger than max resolution
|
|
135
|
+
const scale = maxResolution / longestSide;
|
|
136
|
+
newWidth = Math.floor(width * scale);
|
|
137
|
+
newHeight = Math.floor(height * scale);
|
|
138
|
+
} else {
|
|
139
|
+
// Keep original dimensions if video is smaller
|
|
140
|
+
newWidth = width;
|
|
141
|
+
newHeight = height;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Ensure dimensions are divisible by 2 (required for H.264/image processing)
|
|
145
|
+
const adjustedWidth = newWidth % 2 === 0 ? newWidth : newWidth - 1;
|
|
146
|
+
const adjustedHeight = newHeight % 2 === 0 ? newHeight : newHeight - 1;
|
|
147
|
+
|
|
148
|
+
return { width: adjustedWidth, height: adjustedHeight };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface MediaResult {
|
|
152
|
+
file: string;
|
|
153
|
+
width: number;
|
|
154
|
+
height: number;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface PrepareVideoMetadata {
|
|
158
|
+
duration: number;
|
|
159
|
+
width: number;
|
|
160
|
+
height: number;
|
|
161
|
+
codec: string;
|
|
162
|
+
bitrate: number;
|
|
163
|
+
fps: number;
|
|
164
|
+
hasAudio: boolean;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface PrepareVideoResult {
|
|
168
|
+
objectId: string;
|
|
169
|
+
metadata: PrepareVideoMetadata;
|
|
170
|
+
renditions: VideoRendition[];
|
|
171
|
+
status: 'success';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Generate a video rendition with resolution limited to maxResolution (e.g., 1080p)
|
|
176
|
+
* Uses H.264 codec for broad compatibility
|
|
177
|
+
*/
|
|
178
|
+
async function generateVideoRendition(
|
|
179
|
+
videoPath: string,
|
|
180
|
+
outputDir: string,
|
|
181
|
+
metadata: VideoMetadataExtended,
|
|
182
|
+
maxResolution: number,
|
|
183
|
+
): Promise<MediaResult | null> {
|
|
184
|
+
const outputFile = path.join(outputDir, `rendition_${maxResolution}p.mp4`);
|
|
185
|
+
|
|
186
|
+
// Calculate scaled dimensions
|
|
187
|
+
const dimensions = calculateScaledDimensions(metadata.width, metadata.height, maxResolution);
|
|
188
|
+
|
|
189
|
+
log.info(`Video rendition dimensions: ${metadata.width}x${metadata.height} -> ${dimensions.width}x${dimensions.height}`);
|
|
190
|
+
|
|
191
|
+
// Combine scale with color space conversion for maximum web compatibility
|
|
192
|
+
// format=yuv420p converts to 4:2:0 chroma subsampling (from ProRes 4:2:2 or higher)
|
|
193
|
+
const videoFilters = `scale=${dimensions.width}:${dimensions.height},format=yuv420p`;
|
|
194
|
+
|
|
195
|
+
const command = [
|
|
196
|
+
'ffmpeg',
|
|
197
|
+
'-y', // Overwrite output
|
|
198
|
+
'-i', `"${videoPath}"`,
|
|
199
|
+
'-vf', `"${videoFilters}"`,
|
|
200
|
+
'-c:v', 'libx264', // H.264 codec
|
|
201
|
+
'-preset', 'medium', // Balance between speed and compression
|
|
202
|
+
'-crf', VIDEO_CRF, // Constant Rate Factor (18-28, lower = better quality)
|
|
203
|
+
'-pix_fmt', 'yuv420p', // Explicitly set pixel format for compatibility
|
|
204
|
+
'-c:a', 'aac', // Audio codec
|
|
205
|
+
'-b:a', AUDIO_BITRATE, // Audio bitrate
|
|
206
|
+
'-movflags', '+faststart', // Enable streaming
|
|
207
|
+
'-max_muxing_queue_size', '1024', // Prevent muxing issues
|
|
208
|
+
`"${outputFile}"`,
|
|
209
|
+
].join(' ');
|
|
210
|
+
|
|
211
|
+
log.info(`Generating ${maxResolution}p video rendition`, { command });
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const { stderr } = await execAsync(command, { maxBuffer: FFMPEG_MAX_BUFFER });
|
|
215
|
+
|
|
216
|
+
if (stderr && !stderr.includes('frame=')) {
|
|
217
|
+
log.debug(`FFmpeg stderr for video rendition: ${stderr}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Verify output file was created
|
|
221
|
+
try {
|
|
222
|
+
await fs.promises.access(outputFile, fs.constants.F_OK);
|
|
223
|
+
log.info(`Generated ${maxResolution}p video rendition: ${outputFile}`);
|
|
224
|
+
return {
|
|
225
|
+
file: outputFile,
|
|
226
|
+
width: dimensions.width,
|
|
227
|
+
height: dimensions.height,
|
|
228
|
+
};
|
|
229
|
+
} catch {
|
|
230
|
+
log.warn(`Video rendition not generated`);
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
} catch (error) {
|
|
234
|
+
log.error(
|
|
235
|
+
`Failed to generate video rendition: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
236
|
+
);
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Extract audio track from video as AAC audio file
|
|
243
|
+
*/
|
|
244
|
+
async function generateAudioRendition(
|
|
245
|
+
videoPath: string,
|
|
246
|
+
outputDir: string,
|
|
247
|
+
): Promise<string | null> {
|
|
248
|
+
const outputFile = path.join(outputDir, 'audio.m4a');
|
|
249
|
+
|
|
250
|
+
const command = [
|
|
251
|
+
'ffmpeg',
|
|
252
|
+
'-y', // Overwrite output
|
|
253
|
+
'-i', `"${videoPath}"`,
|
|
254
|
+
'-vn', // No video
|
|
255
|
+
'-c:a', 'aac', // Audio codec
|
|
256
|
+
'-b:a', AUDIO_BITRATE, // Audio bitrate
|
|
257
|
+
`"${outputFile}"`,
|
|
258
|
+
].join(' ');
|
|
259
|
+
|
|
260
|
+
log.info('Generating audio-only rendition', { command });
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
const { stderr } = await execAsync(command, { maxBuffer: FFMPEG_MAX_BUFFER });
|
|
264
|
+
|
|
265
|
+
if (stderr && !stderr.includes('frame=')) {
|
|
266
|
+
log.debug(`FFmpeg stderr for audio rendition: ${stderr}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Verify output file was created
|
|
270
|
+
try {
|
|
271
|
+
await fs.promises.access(outputFile, fs.constants.F_OK);
|
|
272
|
+
log.info(`Generated audio rendition: ${outputFile}`);
|
|
273
|
+
return outputFile;
|
|
274
|
+
} catch {
|
|
275
|
+
log.warn('Audio rendition not generated');
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
} catch (error) {
|
|
279
|
+
log.error(
|
|
280
|
+
`Failed to generate audio rendition: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
281
|
+
);
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Extract a screenshot frame from the video at a specific timestamp
|
|
288
|
+
*/
|
|
289
|
+
async function generateScreenshot(
|
|
290
|
+
videoPath: string,
|
|
291
|
+
outputDir: string,
|
|
292
|
+
timestamp: number,
|
|
293
|
+
maxSize: number,
|
|
294
|
+
name: string,
|
|
295
|
+
metadata: VideoMetadataExtended,
|
|
296
|
+
): Promise<MediaResult | null> {
|
|
297
|
+
const outputFile = path.join(outputDir, `${name}.jpg`);
|
|
298
|
+
|
|
299
|
+
// Calculate scaled dimensions
|
|
300
|
+
const dimensions = calculateScaledDimensions(metadata.width, metadata.height, maxSize);
|
|
301
|
+
|
|
302
|
+
const scaleFilter = `scale=${dimensions.width}:${dimensions.height}`;
|
|
303
|
+
|
|
304
|
+
const command = [
|
|
305
|
+
'ffmpeg',
|
|
306
|
+
'-y',
|
|
307
|
+
'-ss', timestamp.toString(),
|
|
308
|
+
'-i', `"${videoPath}"`,
|
|
309
|
+
'-vframes', '1',
|
|
310
|
+
'-vf', `"${scaleFilter}"`,
|
|
311
|
+
'-q:v', JPEG_QUALITY, // High quality JPEG
|
|
312
|
+
`"${outputFile}"`,
|
|
313
|
+
].join(' ');
|
|
314
|
+
|
|
315
|
+
log.info(`Generating ${name} at ${timestamp}s`, { command });
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const { stderr } = await execAsync(command);
|
|
319
|
+
|
|
320
|
+
if (stderr && !stderr.includes('frame=')) {
|
|
321
|
+
log.debug(`FFmpeg stderr for ${name}: ${stderr}`);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Verify output file was created
|
|
325
|
+
try {
|
|
326
|
+
await fs.promises.access(outputFile, fs.constants.F_OK);
|
|
327
|
+
log.info(`Generated ${name}: ${outputFile}`);
|
|
328
|
+
return {
|
|
329
|
+
file: outputFile,
|
|
330
|
+
width: dimensions.width,
|
|
331
|
+
height: dimensions.height,
|
|
332
|
+
};
|
|
333
|
+
} catch {
|
|
334
|
+
log.warn(`${name} not generated`);
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
} catch (error) {
|
|
338
|
+
log.error(
|
|
339
|
+
`Failed to generate ${name}: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
340
|
+
);
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Upload a file to the storage and return its URI
|
|
347
|
+
*/
|
|
348
|
+
async function uploadFile(
|
|
349
|
+
client: VertesiaClient,
|
|
350
|
+
filePath: string,
|
|
351
|
+
mimeType: string,
|
|
352
|
+
fileName: string,
|
|
353
|
+
storagePath: string,
|
|
354
|
+
): Promise<string> {
|
|
355
|
+
const { NodeStreamSource } = await import('@vertesia/client/node');
|
|
356
|
+
const fileStream = fs.createReadStream(filePath);
|
|
357
|
+
const source = new NodeStreamSource(fileStream, fileName, mimeType, storagePath);
|
|
358
|
+
|
|
359
|
+
const result = await client.files.uploadFile(source);
|
|
360
|
+
log.info(`Uploaded file to ${storagePath}`, { result });
|
|
361
|
+
|
|
362
|
+
return result;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Upload a media result and create a rendition entry
|
|
367
|
+
*/
|
|
368
|
+
async function uploadMediaAsRendition(
|
|
369
|
+
client: VertesiaClient,
|
|
370
|
+
result: MediaResult,
|
|
371
|
+
renditionName: string,
|
|
372
|
+
fileName: string,
|
|
373
|
+
mimeType: string,
|
|
374
|
+
etag: string,
|
|
375
|
+
pathSegment: string,
|
|
376
|
+
): Promise<VideoRendition> {
|
|
377
|
+
const storagePath = `renditions/${etag}/${pathSegment}/${fileName}`;
|
|
378
|
+
const uri = await uploadFile(client, result.file, mimeType, fileName, storagePath);
|
|
379
|
+
|
|
380
|
+
return {
|
|
381
|
+
name: renditionName,
|
|
382
|
+
dimensions: {
|
|
383
|
+
width: result.width,
|
|
384
|
+
height: result.height,
|
|
385
|
+
},
|
|
386
|
+
content: {
|
|
387
|
+
source: uri,
|
|
388
|
+
type: mimeType,
|
|
389
|
+
name: fileName,
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Main activity: Prepare video by extracting metadata and generating renditions
|
|
396
|
+
*/
|
|
397
|
+
export async function prepareVideo(
|
|
398
|
+
payload: DSLActivityExecutionPayload<PrepareVideoParams>,
|
|
399
|
+
): Promise<PrepareVideoResult> {
|
|
400
|
+
const {
|
|
401
|
+
client,
|
|
402
|
+
objectId,
|
|
403
|
+
params,
|
|
404
|
+
} = await setupActivity<PrepareVideoParams>(payload);
|
|
405
|
+
|
|
406
|
+
const maxResolution = params.maxResolution ?? DEFAULT_MAX_RESOLUTION;
|
|
407
|
+
const thumbnailSize = params.thumbnailSize ?? DEFAULT_THUMBNAIL_SIZE;
|
|
408
|
+
const posterSize = params.posterSize ?? DEFAULT_POSTER_SIZE;
|
|
409
|
+
const generateAudio = params.generateAudio ?? DEFAULT_GENERATE_AUDIO;
|
|
410
|
+
|
|
411
|
+
log.info(`Preparing video for ${objectId}`, {
|
|
412
|
+
maxResolution,
|
|
413
|
+
thumbnailSize,
|
|
414
|
+
posterSize,
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
// Retrieve the content object
|
|
418
|
+
const inputObject = await client.objects.retrieve(objectId).catch((err: unknown) => {
|
|
419
|
+
log.error(`Failed to retrieve document ${objectId}`, { err });
|
|
420
|
+
if (err instanceof RequestError && err.status === 404) {
|
|
421
|
+
throw new DocumentNotFoundError(`Document ${objectId} not found`, [objectId]);
|
|
422
|
+
}
|
|
423
|
+
throw err;
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
if (!inputObject.content?.source) {
|
|
427
|
+
log.error(`Document ${objectId} has no source`);
|
|
428
|
+
throw new DocumentNotFoundError(`Document ${objectId} has no source`, [objectId]);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (!inputObject.content.type || !inputObject.content.type.startsWith('video/')) {
|
|
432
|
+
log.error(`Document ${objectId} is not a video: ${inputObject.content.type}`);
|
|
433
|
+
throw new InvalidContentTypeError(
|
|
434
|
+
objectId,
|
|
435
|
+
'video/*',
|
|
436
|
+
inputObject.content.type || 'unknown',
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Download video to temp file
|
|
441
|
+
const videoFile = await saveBlobToTempFile(client, inputObject.content.source);
|
|
442
|
+
const tempOutputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prepare-video-'));
|
|
443
|
+
|
|
444
|
+
try {
|
|
445
|
+
// Step 1: Extract video metadata
|
|
446
|
+
log.info('Extracting video metadata');
|
|
447
|
+
const metadata = await getVideoMetadata(videoFile);
|
|
448
|
+
|
|
449
|
+
// Step 2: Generate video rendition
|
|
450
|
+
log.info('Generating video rendition');
|
|
451
|
+
const renditionResult = await generateVideoRendition(
|
|
452
|
+
videoFile,
|
|
453
|
+
tempOutputDir,
|
|
454
|
+
metadata,
|
|
455
|
+
maxResolution,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
// Step 3 & 4: Generate thumbnail and poster in parallel
|
|
459
|
+
log.info('Generating thumbnail and poster');
|
|
460
|
+
const thumbnailTimestamp = Math.max(metadata.duration * THUMBNAIL_TIMESTAMP_RATIO, MIN_SCREENSHOT_TIMESTAMP);
|
|
461
|
+
const posterTimestamp = Math.max(Math.min(metadata.duration * POSTER_TIMESTAMP_RATIO, POSTER_TIMESTAMP_MAX), MIN_SCREENSHOT_TIMESTAMP);
|
|
462
|
+
|
|
463
|
+
const [thumbnailResult, posterResult] = await Promise.all([
|
|
464
|
+
generateScreenshot(
|
|
465
|
+
videoFile,
|
|
466
|
+
tempOutputDir,
|
|
467
|
+
thumbnailTimestamp,
|
|
468
|
+
thumbnailSize,
|
|
469
|
+
'thumbnail',
|
|
470
|
+
metadata,
|
|
471
|
+
),
|
|
472
|
+
generateScreenshot(
|
|
473
|
+
videoFile,
|
|
474
|
+
tempOutputDir,
|
|
475
|
+
posterTimestamp,
|
|
476
|
+
posterSize,
|
|
477
|
+
'poster',
|
|
478
|
+
metadata,
|
|
479
|
+
),
|
|
480
|
+
]);
|
|
481
|
+
|
|
482
|
+
// Step 5: Generate audio rendition (if video has audio and requested)
|
|
483
|
+
let audioFile: string | null = null;
|
|
484
|
+
if (generateAudio && metadata.hasAudio) {
|
|
485
|
+
log.info('Generating audio rendition');
|
|
486
|
+
audioFile = await generateAudioRendition(videoFile, tempOutputDir);
|
|
487
|
+
} else if (generateAudio && !metadata.hasAudio) {
|
|
488
|
+
log.info('Skipping audio rendition - video has no audio track');
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Step 6: Upload generated files
|
|
492
|
+
const renditions: VideoRendition[] = [];
|
|
493
|
+
const etag = inputObject.content.etag ?? inputObject.id;
|
|
494
|
+
|
|
495
|
+
if (renditionResult) {
|
|
496
|
+
const videoRendition = await uploadMediaAsRendition(
|
|
497
|
+
client,
|
|
498
|
+
renditionResult,
|
|
499
|
+
WEB_VIDEO_RENDITION_NAME,
|
|
500
|
+
`${maxResolution}px.mp4`,
|
|
501
|
+
'video/mp4',
|
|
502
|
+
etag,
|
|
503
|
+
'video',
|
|
504
|
+
);
|
|
505
|
+
renditions.push(videoRendition);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if (thumbnailResult) {
|
|
509
|
+
const fileName = 'thumbnail.jpg';
|
|
510
|
+
const storagePath = `renditions/${etag}/${thumbnailSize}/${fileName}`;
|
|
511
|
+
await uploadFile(
|
|
512
|
+
client,
|
|
513
|
+
thumbnailResult.file,
|
|
514
|
+
'image/jpeg',
|
|
515
|
+
fileName,
|
|
516
|
+
storagePath,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (posterResult) {
|
|
521
|
+
const posterRendition = await uploadMediaAsRendition(
|
|
522
|
+
client,
|
|
523
|
+
posterResult,
|
|
524
|
+
POSTER_RENDITION_NAME,
|
|
525
|
+
'poster.jpg',
|
|
526
|
+
'image/jpeg',
|
|
527
|
+
etag,
|
|
528
|
+
`${posterSize}`,
|
|
529
|
+
);
|
|
530
|
+
renditions.push(posterRendition);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (audioFile) {
|
|
534
|
+
const audioRendition = await uploadMediaAsRendition(
|
|
535
|
+
client,
|
|
536
|
+
{ file: audioFile, width: 0, height: 0 },
|
|
537
|
+
AUDIO_RENDITION_NAME,
|
|
538
|
+
'audio.m4a',
|
|
539
|
+
'audio/mp4',
|
|
540
|
+
etag,
|
|
541
|
+
'audio',
|
|
542
|
+
);
|
|
543
|
+
renditions.push(audioRendition);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Step 7: Update content object with metadata and renditions
|
|
547
|
+
const videoMetadata: VideoMetadata = {
|
|
548
|
+
type: ContentNature.Video,
|
|
549
|
+
duration: metadata.duration,
|
|
550
|
+
dimensions: {
|
|
551
|
+
width: metadata.width,
|
|
552
|
+
height: metadata.height,
|
|
553
|
+
},
|
|
554
|
+
renditions,
|
|
555
|
+
hasAudio: metadata.hasAudio,
|
|
556
|
+
generation_runs: inputObject.metadata?.generation_runs || [],
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
await client.objects.update(objectId, {
|
|
560
|
+
metadata: videoMetadata,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
log.info(`Successfully prepared video ${objectId}`, {
|
|
564
|
+
duration: metadata.duration,
|
|
565
|
+
dimensions: `${metadata.width}x${metadata.height}`,
|
|
566
|
+
codec: metadata.codec,
|
|
567
|
+
bitrate: metadata.bitrate,
|
|
568
|
+
fps: metadata.fps,
|
|
569
|
+
hasAudio: metadata.hasAudio,
|
|
570
|
+
renditionsGenerated: renditions.length,
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
return {
|
|
574
|
+
objectId,
|
|
575
|
+
metadata: {
|
|
576
|
+
duration: metadata.duration,
|
|
577
|
+
width: metadata.width,
|
|
578
|
+
height: metadata.height,
|
|
579
|
+
codec: metadata.codec,
|
|
580
|
+
bitrate: metadata.bitrate,
|
|
581
|
+
fps: metadata.fps,
|
|
582
|
+
hasAudio: metadata.hasAudio,
|
|
583
|
+
},
|
|
584
|
+
renditions: renditions,
|
|
585
|
+
status: 'success',
|
|
586
|
+
};
|
|
587
|
+
} catch (error) {
|
|
588
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
589
|
+
log.error(`Error preparing video: ${errorMessage}`, { error });
|
|
590
|
+
|
|
591
|
+
// Re-throw known errors as-is
|
|
592
|
+
if (error instanceof DocumentNotFoundError || error instanceof InvalidContentTypeError) {
|
|
593
|
+
throw error;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Wrap unknown errors in Error
|
|
597
|
+
throw new Error(
|
|
598
|
+
`Failed to prepare video ${objectId}: ${errorMessage}`,
|
|
599
|
+
);
|
|
600
|
+
} finally {
|
|
601
|
+
// Clean up temporary files
|
|
602
|
+
const cleanupPromises: Promise<void>[] = [];
|
|
603
|
+
|
|
604
|
+
if (videoFile) {
|
|
605
|
+
cleanupPromises.push(
|
|
606
|
+
fs.promises.unlink(videoFile).catch((err) => {
|
|
607
|
+
log.warn(`Failed to cleanup video file: ${videoFile}`, { err });
|
|
608
|
+
}),
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (tempOutputDir) {
|
|
613
|
+
cleanupPromises.push(
|
|
614
|
+
fs.promises.rm(tempOutputDir, { recursive: true, force: true }).catch((err) => {
|
|
615
|
+
log.warn(`Failed to cleanup temp directory: ${tempOutputDir}`, { err });
|
|
616
|
+
}),
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
await Promise.allSettled(cleanupPromises);
|
|
621
|
+
}
|
|
622
|
+
}
|