Master video generation with Dream Machine API. Learn best practices, optimization techniques, and real-world applications for text-to-video and image-to-video generation.
Video content has become the cornerstone of digital communication, from social media to marketing campaigns. The Dream Machine API revolutionizes video creation by enabling developers to generate professional-quality videos from simple text descriptions or static images. This comprehensive guide covers everything you need to know to master video generation with AI.
Dream Machine API is a cutting-edge video generation service that transforms text prompts and images into dynamic, high-quality videos. Built on advanced AI models, it offers:
const DREAM_MACHINE_API = 'https://api.omnapi.com/v1/video';
const API_KEY = process.env.OMNI_API_KEY;
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
};
// Basic video generation function
async function generateVideo(prompt, options = {}) {
const response = await fetch(`${DREAM_MACHINE_API}/generate`, {
method: 'POST',
headers,
body: JSON.stringify({
prompt,
duration: options.duration || 5,
fps: options.fps || 24,
resolution: options.resolution || '1080p',
...options
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
}
// Generate a simple video
const result = await generateVideo(
"A serene lake with gentle ripples reflecting the sunset sky"
);
console.log('Video generation started:', result.jobId);
console.log('Estimated completion time:', result.estimatedTime);
{
"success": true,
"jobId": "video_job_abc123",
"status": "processing",
"videoUrl": null,
"thumbnailUrl": "https://storage.omnapi.com/thumbnails/abc123.jpg",
"metadata": {
"duration": 5,
"fps": 24,
"resolution": "1920x1080",
"format": "MP4",
"aspectRatio": "16:9"
},
"creditsUsed": 50,
"estimatedTime": 45,
"webhookUrl": null
}
Video prompts require different considerations than image prompts:
class VideoPromptBuilder {
constructor() {
this.motionWords = [
'flowing', 'swaying', 'dancing', 'floating', 'moving',
'rotating', 'pulsing', 'growing', 'shrinking', 'transforming'
];
this.cameraMovements = [
'slow zoom in', 'zoom out', 'pan left', 'pan right',
'tilt up', 'tilt down', 'circular motion', 'steady shot'
];
}
buildVideoPrompt(subject, action, setting, style, cameraMovement) {
const prompt = [
subject,
action,
setting,
style,
cameraMovement,
'smooth motion, cinematic quality, high detail'
].filter(Boolean).join(', ');
return prompt;
}
// Predefined prompt templates
getTemplate(type) {
const templates = {
nature: "Beautiful {element} {motion} in {location}, {weather}, {cameraMovement}, peaceful atmosphere",
abstract: "Abstract {shapes} {motion} with {colors}, {style}, hypnotic movement",
product: "{product} {action} against {background}, {lighting}, {cameraMovement}, commercial quality",
lifestyle: "Person {activity} in {setting}, {mood}, natural movement, documentary style"
};
return templates[type];
}
}
// Usage examples
const promptBuilder = new VideoPromptBuilder();
// Nature video
const naturePrompt = promptBuilder.buildVideoPrompt(
"Tall grass field",
"gently swaying in the breeze",
"golden hour countryside",
"cinematic photography",
"slow pan across the landscape"
);
// Abstract art video
const abstractPrompt = promptBuilder.buildVideoPrompt(
"Colorful geometric shapes",
"morphing and rotating",
"dark void background",
"neon glow effect",
"slow zoom in"
);
console.log('Nature prompt:', naturePrompt);
console.log('Abstract prompt:', abstractPrompt);
class MotionController {
static getMotionIntensity(level) {
const intensities = {
subtle: "gentle, slow, barely noticeable movement",
moderate: "smooth, steady, natural movement",
dynamic: "energetic, fast-paced, dramatic movement",
extreme: "intense, rapid, explosive movement"
};
return intensities[level] || intensities.moderate;
}
static getTimingKeywords(duration) {
if (duration <= 3) {
return "quick transition, fast-paced, rapid change";
} else if (duration <= 6) {
return "smooth transition, natural pace, steady movement";
} else {
return "slow transition, contemplative pace, gradual change";
}
}
static buildMotionPrompt(basePrompt, motionLevel, duration) {
const motion = this.getMotionIntensity(motionLevel);
const timing = this.getTimingKeywords(duration);
return `${basePrompt}, ${motion}, ${timing}`;
}
}
// Generate video with specific motion characteristics
async function generateWithMotion(basePrompt, motionLevel = 'moderate', duration = 5) {
const enhancedPrompt = MotionController.buildMotionPrompt(
basePrompt,
motionLevel,
duration
);
return await generateVideo(enhancedPrompt, { duration });
}
// Examples
const subtleVideo = await generateWithMotion(
"A calm forest scene with ancient trees",
'subtle',
8
);
const dynamicVideo = await generateWithMotion(
"City traffic and neon lights at night",
'dynamic',
4
);
async function generateVideoFromImage(imageUrl, prompt, options = {}) {
const response = await fetch(`${DREAM_MACHINE_API}/image-to-video`, {
method: 'POST',
headers,
body: JSON.stringify({
image_url: imageUrl,
prompt: prompt,
duration: options.duration || 5,
motion_strength: options.motionStrength || 0.7,
fps: options.fps || 24,
resolution: options.resolution || '1080p'
})
});
return await response.json();
}
// Example: Animate a landscape photo
const animatedLandscape = await generateVideoFromImage(
'https://example.com/landscape.jpg',
'Gentle wind moving through the grass, clouds slowly drifting across the sky',
{
duration: 6,
motionStrength: 0.5, // Subtle animation
fps: 30
}
);
class ImageAnimator {
constructor(apiKey) {
this.apiKey = apiKey;
}
async animatePortrait(imageUrl, expression = 'subtle_smile') {
const prompts = {
subtle_smile: "Person slowly developing a gentle, warm smile",
blink: "Natural blinking and subtle facial expressions",
look_around: "Eyes moving naturally, looking around with curiosity",
hair_movement: "Hair gently moving as if in a light breeze"
};
return await generateVideoFromImage(imageUrl, prompts[expression], {
duration: 3,
motionStrength: 0.3 // Very subtle for portraits
});
}
async animateProduct(imageUrl, animationType = 'rotate') {
const animations = {
rotate: "Product slowly rotating 360 degrees, showing all angles",
zoom: "Smooth zoom in to highlight product details, then zoom out",
float: "Product gently floating and bobbing in space",
glow: "Product with subtle pulsing glow effect highlighting features"
};
return await generateVideoFromImage(imageUrl, animations[animationType], {
duration: 4,
motionStrength: 0.6
});
}
async animateArtwork(imageUrl, style = 'living_painting') {
const styles = {
living_painting: "Painting coming to life with subtle movements in the scene",
morphing: "Abstract shapes slowly morphing and transforming",
particle_effect: "Elements dissolving into particles and reforming",
color_shift: "Colors gradually shifting and flowing throughout the image"
};
return await generateVideoFromImage(imageUrl, styles[style], {
duration: 7,
motionStrength: 0.8
});
}
}
// Usage examples
const animator = new ImageAnimator(API_KEY);
// Animate a portrait
const portraitVideo = await animator.animatePortrait(
'https://example.com/portrait.jpg',
'subtle_smile'
);
// Animate a product
const productVideo = await animator.animateProduct(
'https://example.com/product.jpg',
'rotate'
);
class VideoBatchProcessor {
constructor(apiKey, maxConcurrent = 2) {
this.apiKey = apiKey;
this.maxConcurrent = maxConcurrent;
this.queue = [];
this.processing = 0;
this.results = new Map();
}
async processBatch(requests) {
const promises = requests.map((request, index) =>
this.addToQueue(request, index)
);
const results = await Promise.allSettled(promises);
return results.map((result, index) => ({
index,
success: result.status === 'fulfilled',
data: result.status === 'fulfilled' ? result.value : null,
error: result.status === 'rejected' ? result.reason : null
}));
}
async addToQueue(request, index) {
return new Promise((resolve, reject) => {
this.queue.push({ request, index, resolve, reject });
this.processQueue();
});
}
async processQueue() {
if (this.processing >= this.maxConcurrent || this.queue.length === 0) {
return;
}
this.processing++;
const { request, index, resolve, reject } = this.queue.shift();
try {
const result = await this.generateSingleVideo(request);
resolve(result);
} catch (error) {
reject(error);
} finally {
this.processing--;
this.processQueue();
}
}
async generateSingleVideo(request) {
// Add delay to avoid rate limiting
await this.delay(1000);
const response = await fetch(`${DREAM_MACHINE_API}/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
// Usage example
const batchProcessor = new VideoBatchProcessor(API_KEY);
const videoRequests = [
{
prompt: "Ocean waves crashing on a rocky shore",
duration: 5,
resolution: "1080p"
},
{
prompt: "Forest fire spreading through autumn trees",
duration: 6,
resolution: "1080p"
},
{
prompt: "Northern lights dancing across the night sky",
duration: 8,
resolution: "4k"
}
];
const batchResults = await batchProcessor.processBatch(videoRequests);
console.log('Batch processing completed:', batchResults);
class WebhookManager {
constructor(apiKey, webhookUrl) {
this.apiKey = apiKey;
this.webhookUrl = webhookUrl;
this.jobs = new Map();
}
async generateVideoWithWebhook(prompt, options = {}) {
const jobId = `job_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const response = await fetch(`${DREAM_MACHINE_API}/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt,
webhook_url: `${this.webhookUrl}/webhook/video-complete`,
job_id: jobId,
...options
})
});
const result = await response.json();
// Store job info for tracking
this.jobs.set(jobId, {
prompt,
startTime: Date.now(),
status: 'processing',
...result
});
return { jobId, ...result };
}
handleWebhook(webhookData) {
const { job_id, status, video_url, error } = webhookData;
if (this.jobs.has(job_id)) {
const job = this.jobs.get(job_id);
job.status = status;
job.completedTime = Date.now();
job.processingDuration = job.completedTime - job.startTime;
if (status === 'completed') {
job.videoUrl = video_url;
console.log(`✅ Video generated successfully: ${video_url}`);
console.log(`⏱️ Processing time: ${job.processingDuration}ms`);
} else if (status === 'failed') {
job.error = error;
console.error(`❌ Video generation failed: ${error}`);
}
this.jobs.set(job_id, job);
// Notify completion (implement your notification logic)
this.notifyCompletion(job);
}
}
notifyCompletion(job) {
// Implement your notification logic here
// e.g., send email, update database, notify frontend via WebSocket
console.log('Job completed:', job);
}
getJobStatus(jobId) {
return this.jobs.get(jobId);
}
getAllJobs() {
return Array.from(this.jobs.values());
}
}
// Express.js webhook endpoint example
const express = require('express');
const app = express();
const webhookManager = new WebhookManager(API_KEY, 'https://yourapp.com');
app.use(express.json());
app.post('/webhook/video-complete', (req, res) => {
try {
webhookManager.handleWebhook(req.body);
res.status(200).send('OK');
} catch (error) {
console.error('Webhook error:', error);
res.status(500).send('Error processing webhook');
}
});
// Start video generation with webhook
const videoJob = await webhookManager.generateVideoWithWebhook(
"Time-lapse of a flower blooming in spring garden",
{ duration: 6, resolution: "4k" }
);
console.log('Video generation started:', videoJob.jobId);
class VideoStyler {
constructor() {
this.styles = {
cinematic: {
modifiers: "cinematic lighting, film grain, color grading, professional cinematography",
fps: 24,
motion: "smooth camera movements, dramatic angles"
},
documentary: {
modifiers: "natural lighting, realistic colors, handheld camera feel",
fps: 30,
motion: "organic movement, observational style"
},
music_video: {
modifiers: "dynamic lighting, vibrant colors, artistic composition",
fps: 30,
motion: "rhythmic movement, creative transitions"
},
commercial: {
modifiers: "bright lighting, polished look, product-focused",
fps: 30,
motion: "smooth product reveals, professional presentation"
},
artistic: {
modifiers: "creative lighting, unique perspective, artistic vision",
fps: 24,
motion: "experimental camera work, abstract movements"
},
vintage: {
modifiers: "retro color palette, film grain, vintage aesthetic",
fps: 24,
motion: "classic camera techniques, nostalgic feel"
}
};
}
applyStyle(basePrompt, styleName) {
const style = this.styles[styleName];
if (!style) {
throw new Error(`Unknown style: ${styleName}`);
}
return {
prompt: `${basePrompt}, ${style.modifiers}, ${style.motion}`,
fps: style.fps,
style: styleName
};
}
async generateStyledVideo(basePrompt, styleName, options = {}) {
const styledConfig = this.applyStyle(basePrompt, styleName);
return await generateVideo(styledConfig.prompt, {
fps: styledConfig.fps,
...options
});
}
}
// Usage examples
const styler = new VideoStyler();
// Generate cinematic style video
const cinematicVideo = await styler.generateStyledVideo(
"A lone figure walking through a misty forest",
"cinematic",
{ duration: 8, resolution: "4k" }
);
// Generate commercial style video
const commercialVideo = await styler.generateStyledVideo(
"Luxury watch on elegant display",
"commercial",
{ duration: 5, resolution: "1080p" }
);
class EnvironmentalEffects {
static getWeatherEffect(weather) {
const effects = {
rain: "gentle rain falling, water droplets, wet surfaces, misty atmosphere",
snow: "snowflakes falling softly, winter atmosphere, crystalline beauty",
fog: "thick fog rolling in, mysterious atmosphere, limited visibility",
storm: "dramatic storm clouds, lightning flashes, intense weather",
wind: "strong wind effects, objects swaying, leaves blowing",
sunrise: "golden hour lighting, warm colors, sun rising slowly",
sunset: "dramatic sunset colors, silhouettes, peaceful evening",
night: "moonlit scene, deep shadows, mysterious lighting"
};
return effects[weather] || "";
}
static getSeason(season) {
const seasons = {
spring: "fresh green leaves, blooming flowers, renewal energy",
summer: "lush greenery, bright sunshine, vibrant life",
autumn: "colorful falling leaves, golden light, harvest atmosphere",
winter: "bare trees, frost, serene winter beauty"
};
return seasons[season] || "";
}
static buildEnvironmentalPrompt(baseScene, weather, season, timeOfDay) {
const components = [
baseScene,
this.getWeatherEffect(weather),
this.getSeason(season),
`${timeOfDay} lighting`,
"atmospheric depth, environmental storytelling"
].filter(Boolean);
return components.join(", ");
}
}
// Generate videos with environmental effects
async function generateEnvironmentalVideo(scene, environment) {
const { weather, season, timeOfDay } = environment;
const enhancedPrompt = EnvironmentalEffects.buildEnvironmentalPrompt(
scene, weather, season, timeOfDay
);
return await generateVideo(enhancedPrompt, {
duration: 7,
resolution: "1080p"
});
}
// Examples
const stormyForest = await generateEnvironmentalVideo(
"Dense forest with tall pine trees",
{ weather: "storm", season: "autumn", timeOfDay: "dusk" }
);
const sunnyMeadow = await generateEnvironmentalVideo(
"Peaceful meadow with wildflowers",
{ weather: "clear", season: "spring", timeOfDay: "morning" }
);
class SocialVideoGenerator {
constructor(apiKey) {
this.apiKey = apiKey;
this.platforms = {
instagram_reel: {
duration: 7,
aspectRatio: "9:16",
style: "dynamic, trending, engaging"
},
youtube_short: {
duration: 8,
aspectRatio: "9:16",
style: "attention-grabbing, viral potential"
},
tiktok: {
duration: 6,
aspectRatio: "9:16",
style: "trendy, fast-paced, entertaining"
},
twitter: {
duration: 4,
aspectRatio: "16:9",
style: "concise, impactful, shareable"
},
linkedin: {
duration: 5,
aspectRatio: "16:9",
style: "professional, informative, business-focused"
}
};
}
async generateSocialVideo(content, platform, theme = "modern") {
const config = this.platforms[platform];
if (!config) {
throw new Error(`Unsupported platform: ${platform}`);
}
const prompt = this.buildSocialPrompt(content, config.style, theme, platform);
return await generateVideo(prompt, {
duration: config.duration,
aspect_ratio: config.aspectRatio,
fps: 30,
resolution: "1080p"
});
}
buildSocialPrompt(content, platformStyle, theme, platform) {
const themeModifiers = {
modern: "clean, contemporary, minimalist aesthetic",
vibrant: "bright colors, high energy, dynamic movement",
professional: "polished, corporate, sophisticated",
creative: "artistic, unique, innovative visuals",
trendy: "current trends, popular aesthetics, viral appeal"
};
return `${content}, ${platformStyle}, ${themeModifiers[theme]}, ${platform} optimized, high engagement potential, smooth motion`;
}
async generateCampaign(concept, platforms = ["instagram_reel", "tiktok"]) {
const campaign = {};
for (const platform of platforms) {
try {
campaign[platform] = await this.generateSocialVideo(concept, platform);
// Add delay between requests
await new Promise(resolve => setTimeout(resolve, 2000));
} catch (error) {
console.error(`Failed to generate ${platform} video:`, error);
campaign[platform] = { error: error.message };
}
}
return campaign;
}
}
// Usage example
const socialGenerator = new SocialVideoGenerator(API_KEY);
// Generate single platform video
const instagramReel = await socialGenerator.generateSocialVideo(
"Coffee beans transforming into a perfect cup of coffee",
"instagram_reel",
"modern"
);
// Generate multi-platform campaign
const coffeeCampaign = await socialGenerator.generateCampaign(
"Morning coffee routine with steam and aroma",
["instagram_reel", "tiktok", "youtube_short"]
);
class ProductVideoGenerator {
constructor(apiKey) {
this.apiKey = apiKey;
}
async generateProductDemo(productName, features, style = "commercial") {
const demoPrompt = this.buildProductDemoPrompt(productName, features, style);
return await generateVideo(demoPrompt, {
duration: 6,
resolution: "4k",
fps: 30
});
}
async generateProductReveal(productName, setting = "studio") {
const revealPrompt = this.buildRevealPrompt(productName, setting);
return await generateVideo(revealPrompt, {
duration: 4,
resolution: "4k",
fps: 24
});
}
async generateLifestyleVideo(productName, scenario, mood = "aspirational") {
const lifestylePrompt = this.buildLifestylePrompt(productName, scenario, mood);
return await generateVideo(lifestylePrompt, {
duration: 8,
resolution: "1080p",
fps: 30
});
}
buildProductDemoPrompt(product, features, style) {
const styleModifiers = {
commercial: "professional studio lighting, clean background, commercial quality",
tech: "modern tech aesthetic, sleek design, futuristic elements",
lifestyle: "natural environment, real-world usage, authentic feel",
luxury: "premium presentation, elegant setting, sophisticated atmosphere"
};
return `${product} demonstration showcasing ${features.join(', ')}, ${styleModifiers[style]}, smooth product movements, clear feature highlights`;
}
buildRevealPrompt(product, setting) {
const settings = {
studio: "professional studio with dramatic lighting and clean backdrop",
natural: "natural outdoor setting with organic lighting",
minimal: "minimalist white space with soft lighting",
dramatic: "dark background with dramatic spotlighting"
};
return `${product} dramatic reveal in ${settings[setting]}, slow motion unveiling, anticipation building, cinematic presentation`;
}
buildLifestylePrompt(product, scenario, mood) {
const moods = {
aspirational: "inspiring, uplifting, aspirational lifestyle",
authentic: "genuine, relatable, everyday moments",
energetic: "dynamic, active, high-energy lifestyle",
peaceful: "calm, serene, mindful living"
};
return `${product} being used in ${scenario}, ${moods[mood]}, natural human interaction, real-world context, emotional connection`;
}
}
// Usage examples
const productGenerator = new ProductVideoGenerator(API_KEY);
// Product demonstration
const smartphoneDemo = await productGenerator.generateProductDemo(
"Latest smartphone",
["camera features", "display quality", "design elegance"],
"tech"
);
// Product reveal
const watchReveal = await productGenerator.generateProductReveal(
"Luxury smartwatch",
"dramatic"
);
// Lifestyle video
const lifestyleVideo = await productGenerator.generateLifestyleVideo(
"Wireless earbuds",
"morning jog in the park",
"energetic"
);
class EducationalVideoGenerator {
constructor(apiKey) {
this.apiKey = apiKey;
}
async generateConceptVisualization(concept, subject, level = "intermediate") {
const visualPrompt = this.buildConceptPrompt(concept, subject, level);
return await generateVideo(visualPrompt, {
duration: 6,
resolution: "1080p",
fps: 30
});
}
async generateProcessAnimation(process, steps, style = "scientific") {
const processPrompt = this.buildProcessPrompt(process, steps, style);
return await generateVideo(processPrompt, {
duration: 8,
resolution: "1080p",
fps: 24
});
}
async generateHistoricalRecreation(event, period, mood = "dramatic") {
const historicalPrompt = this.buildHistoricalPrompt(event, period, mood);
return await generateVideo(historicalPrompt, {
duration: 10,
resolution: "4k",
fps: 24
});
}
buildConceptPrompt(concept, subject, level) {
const levelModifiers = {
beginner: "simple, clear visualization, easy to understand",
intermediate: "detailed explanation, moderate complexity",
advanced: "comprehensive visualization, complex relationships"
};
return `${concept} visualization for ${subject}, ${levelModifiers[level]}, educational animation, clear demonstration, scientific accuracy`;
}
buildProcessPrompt(process, steps, style) {
const styleModifiers = {
scientific: "scientific accuracy, laboratory setting, precise movements",
artistic: "creative visualization, artistic interpretation, engaging visuals",
technical: "technical diagrams, engineering precision, mechanical movements",
natural: "natural phenomena, organic processes, environmental context"
};
return `${process} step-by-step animation showing ${steps.join(' → ')}, ${styleModifiers[style]}, clear progression, educational value`;
}
buildHistoricalPrompt(event, period, mood) {
const moodModifiers = {
dramatic: "cinematic recreation, dramatic lighting, emotional impact",
documentary: "realistic portrayal, authentic details, informative",
artistic: "artistic interpretation, creative visualization, symbolic elements"
};
return `Historical recreation of ${event} during ${period}, ${moodModifiers[mood]}, period-accurate details, immersive atmosphere`;
}
}
// Usage examples
const eduGenerator = new EducationalVideoGenerator(API_KEY);
// Science concept visualization
const photosynthesis = await eduGenerator.generateConceptVisualization(
"Photosynthesis process in plant leaves",
"biology",
"intermediate"
);
// Process animation
const waterCycle = await eduGenerator.generateProcessAnimation(
"Water cycle",
["evaporation", "condensation", "precipitation", "collection"],
"natural"
);
// Historical recreation
const romanForum = await eduGenerator.generateHistoricalRecreation(
"Daily life in Roman Forum",
"Ancient Rome",
"documentary"
);
class VideoQualityController {
constructor() {
this.qualityMetrics = {
resolution: ['720p', '1080p', '4k'],
frameRate: [24, 30, 60],
duration: { min: 2, max: 10 },
motionSmoothnessThresholds: { low: 0.3, medium: 0.7, high: 0.9 }
};
}
validateVideoRequest(request) {
const errors = [];
// Validate resolution
if (request.resolution && !this.qualityMetrics.resolution.includes(request.resolution)) {
errors.push(`Invalid resolution: ${request.resolution}`);
}
// Validate frame rate
if (request.fps && !this.qualityMetrics.frameRate.includes(request.fps)) {
errors.push(`Invalid frame rate: ${request.fps}`);
}
// Validate duration
if (request.duration) {
if (request.duration < this.qualityMetrics.duration.min) {
errors.push(`Duration too short: ${request.duration}s (min: ${this.qualityMetrics.duration.min}s)`);
}
if (request.duration > this.qualityMetrics.duration.max) {
errors.push(`Duration too long: ${request.duration}s (max: ${this.qualityMetrics.duration.max}s)`);
}
}
return errors;
}
optimizeRequestForQuality(request, targetQuality = 'high') {
const optimizations = {
low: { resolution: '720p', fps: 24, complexity: 'simple' },
medium: { resolution: '1080p', fps: 30, complexity: 'moderate' },
high: { resolution: '4k', fps: 30, complexity: 'detailed' }
};
const optimization = optimizations[targetQuality];
return {
...request,
resolution: optimization.resolution,
fps: optimization.fps,
prompt: this.enhancePromptForQuality(request.prompt, optimization.complexity)
};
}
enhancePromptForQuality(prompt, complexity) {
const qualityEnhancers = {
simple: "clean, simple, clear motion",
moderate: "detailed, smooth motion, good quality",
detailed: "highly detailed, cinematic quality, professional production, 4k quality, smooth motion"
};
return `${prompt}, ${qualityEnhancers[complexity]}`;
}
}
// Usage example
const qualityController = new VideoQualityController();
// Validate and optimize request
const originalRequest = {
prompt: "A cat playing in a garden",
duration: 5,
resolution: "1080p",
fps: 30
};
const errors = qualityController.validateVideoRequest(originalRequest);
if (errors.length > 0) {
console.error('Validation errors:', errors);
} else {
const optimizedRequest = qualityController.optimizeRequestForQuality(originalRequest, 'high');
console.log('Optimized request:', optimizedRequest);
}
class CostOptimizer {
constructor() {
this.creditCosts = {
resolution: { '720p': 1.0, '1080p': 1.5, '4k': 3.0 },
duration: { base: 1.0, perSecond: 0.2 },
fps: { 24: 1.0, 30: 1.2, 60: 2.0 }
};
}
estimateCost(request) {
const resolutionMultiplier = this.creditCosts.resolution[request.resolution] || 1.0;
const fpsMultiplier = this.creditCosts.fps[request.fps] || 1.0;
const durationCost = this.creditCosts.duration.base +
(request.duration * this.creditCosts.duration.perSecond);
const totalCost = 50 * resolutionMultiplier * fpsMultiplier * durationCost;
return Math.round(totalCost);
}
suggestCostOptimizations(request, budget) {
const currentCost = this.estimateCost(request);
if (currentCost <= budget) {
return { optimized: request, savings: 0, message: "Within budget" };
}
// Try different optimization strategies
const optimizations = [
{ ...request, resolution: '1080p' },
{ ...request, fps: 24 },
{ ...request, duration: Math.max(3, request.duration - 2) },
{ ...request, resolution: '1080p', fps: 24 },
{ ...request, resolution: '720p', fps: 24 }
];
for (const optimization of optimizations) {
const cost = this.estimateCost(optimization);
if (cost <= budget) {
return {
optimized: optimization,
savings: currentCost - cost,
message: `Optimized to fit budget. Savings: ${currentCost - cost} credits`
};
}
}
return {
optimized: request,
savings: 0,
message: "Cannot optimize to fit budget. Consider increasing budget or reducing requirements."
};
}
}
// Usage example
const costOptimizer = new CostOptimizer();
const expensiveRequest = {
prompt: "High-quality nature documentary scene",
duration: 8,
resolution: '4k',
fps: 60
};
const budget = 200; // credits
const costEstimate = costOptimizer.estimateCost(expensiveRequest);
console.log(`Estimated cost: ${costEstimate} credits`);
if (costEstimate > budget) {
const optimization = costOptimizer.suggestCostOptimizations(expensiveRequest, budget);
console.log('Optimization suggestion:', optimization);
}
class VideoTroubleshooter {
constructor() {
this.commonIssues = {
'generation_timeout': {
description: 'Video generation taking too long',
solutions: [
'Reduce video duration',
'Lower resolution settings',
'Simplify the prompt',
'Use webhook for long-running jobs'
]
},
'poor_quality': {
description: 'Generated video quality is poor',
solutions: [
'Increase resolution to 1080p or 4k',
'Add quality keywords to prompt',
'Increase frame rate',
'Improve prompt specificity'
]
},
'unnatural_motion': {
description: 'Motion appears jerky or unnatural',
solutions: [
'Add motion smoothness keywords',
'Reduce motion complexity',
'Use appropriate frame rate (24-30 fps)',
'Adjust motion strength parameter'
]
},
'prompt_misinterpretation': {
description: 'AI misunderstood the prompt',
solutions: [
'Make prompt more specific',
'Use clear, descriptive language',
'Add style and quality modifiers',
'Break complex prompts into simpler ones'
]
}
};
}
diagnoseIssue(problemDescription, videoResult) {
// Simple keyword matching for demonstration
const keywords = problemDescription.toLowerCase();
if (keywords.includes('slow') || keywords.includes('timeout')) {
return this.commonIssues.generation_timeout;
} else if (keywords.includes('quality') || keywords.includes('blurry')) {
return this.commonIssues.poor_quality;
} else if (keywords.includes('motion') || keywords.includes('jerky')) {
return this.commonIssues.unnatural_motion;
} else if (keywords.includes('wrong') || keywords.includes('different')) {
return this.commonIssues.prompt_misinterpretation;
}
return {
description: 'Unknown issue',
solutions: ['Contact support with detailed description', 'Try regenerating with different parameters']
};
}
generateOptimizedPrompt(originalPrompt, issue) {
const optimizations = {
'poor_quality': (prompt) => `${prompt}, high quality, detailed, cinematic, professional production`,
'unnatural_motion': (prompt) => `${prompt}, smooth motion, natural movement, fluid animation`,
'prompt_misinterpretation': (prompt) => this.clarifyPrompt(prompt)
};
return optimizations[issue] ? optimizations[issue](originalPrompt) : originalPrompt;
}
clarifyPrompt(prompt) {
// Add clarifying keywords
return `${prompt}, realistic, detailed scene, clear subject focus, professional quality`;
}
}
// Usage example
const troubleshooter = new VideoTroubleshooter();
// Diagnose an issue
const issue = troubleshooter.diagnoseIssue("The video quality is poor and blurry", null);
console.log('Diagnosed issue:', issue);
// Generate optimized prompt
const originalPrompt = "A dog running in the park";
const optimizedPrompt = troubleshooter.generateOptimizedPrompt(originalPrompt, 'poor_quality');
console.log('Optimized prompt:', optimizedPrompt);
The Dream Machine API opens up limitless possibilities for video content creation. By mastering the techniques covered in this guide, you can:
Remember that video generation is both an art and a science. Experiment with different prompt styles, study the results, and iterate on your approach. The key to success is understanding how to communicate your creative vision through well-crafted prompts and appropriate technical parameters.
Ready to start creating amazing videos? Get your Dream Machine API access and bring your creative visions to life!
Comprehensive comparison of leading AI APIs in 2025. Compare features, pricing, performance, and use cases for text-to-image, video generation, music creation, and more.
Discover innovative applications of Ace-step Music API for text-to-music generation. Explore real-world use cases, creative projects, and practical implementations for developers and creators.
Complete tutorial on using Flux API for text-to-image generation. Learn advanced prompting techniques, parameter optimization, and integration best practices with code examples.