Skip to content

Unrestricted Open-source alternative to AI video platforms — Free AI image & video generation studio with 600+ models (Flux, Midjourney, Kling, Sora, Veo). No content filters. Self-hosted, MIT licensed.

Merge pull request #331 from antilya/fix-video-parameter-constraints

jaiprasad0452c6d27988e9
diff --cc packages/studio/src/components/VideoStudio.jsx
index c2748cbd,855c754b..ddeb935e
--- a/packages/studio/src/components/VideoStudio.jsx
+++ b/packages/studio/src/components/VideoStudio.jsx
@@@ -70,94 -51,22 +70,110 @@@ function getQualitiesForModel(modelList
return model?.inputs?.quality?.enum || [];
}
+function getVideoModelsForMode(imageMode, v2vMode) {
+ if (v2vMode) return v2vModels;
+ return imageMode ? i2vModels : t2vModels;
+}
+
+function formatCost({ cost, currency }) {
+ return new Intl.NumberFormat("en-US", {
+ style: "currency",
+ currency,
+ maximumFractionDigits: 4,
+ }).format(cost);
+}
+
+const DEFAULT_VIDEO_MAX_BYTES = 50 * 1024 * 1024;
+
+function getVideoFileError(file, constraints) {
+ const maxBytes = constraints?.maxBytes || DEFAULT_VIDEO_MAX_BYTES;
+ if (file.size > maxBytes) {
+ return `Video exceeds the ${Math.round(maxBytes / 1024 / 1024)}MB limit.`;
+ }
+ if (!constraints) return null;
+
+ const mimeType = file.type.toLowerCase();
+ const extension = file.name.slice(file.name.lastIndexOf(".")).toLowerCase();
+ const hasAllowedType = constraints.allowedMimeTypes.includes(mimeType);
+ const hasAllowedExtension = constraints.allowedExtensions.includes(extension);
+ return hasAllowedType || hasAllowedExtension
+ ? null
+ : `Unsupported video format. ${constraints.requirements}`;
+}
+
+function readVideoMetadata(file) {
+ return new Promise((resolve, reject) => {
+ const objectUrl = URL.createObjectURL(file);
+ const video = document.createElement("video");
+ let timeoutId;
+ const cleanup = () => {
+ clearTimeout(timeoutId);
+ video.onloadedmetadata = null;
+ video.onerror = null;
+ video.removeAttribute("src");
+ video.load();
+ URL.revokeObjectURL(objectUrl);
+ };
+
+ video.preload = "metadata";
+ video.onloadedmetadata = () => {
+ const metadata = {
+ duration: video.duration,
+ width: video.videoWidth,
+ height: video.videoHeight,
+ };
+ cleanup();
+ resolve(metadata);
+ };
+ video.onerror = () => {
+ cleanup();
+ reject(new Error("The selected video's metadata could not be read."));
+ };
+ timeoutId = setTimeout(() => {
+ cleanup();
+ reject(new Error("The selected video's metadata could not be read."));
+ }, 10000);
+ video.src = objectUrl;
+ });
+}
+
+function getVideoMetadataError(metadata, constraints) {
+ if (!constraints) return null;
+ if (
+ !Number.isFinite(metadata.duration) ||
+ metadata.duration < constraints.minDurationSeconds ||
+ metadata.duration > constraints.maxDurationSeconds
+ ) {
+ return `Video duration must be between ${constraints.minDurationSeconds} and ${constraints.maxDurationSeconds} seconds.`;
+ }
+
+ const shortSide = Math.min(metadata.width, metadata.height);
+ const longSide = Math.max(metadata.width, metadata.height);
+ if (
+ shortSide < constraints.minShortSide ||
+ longSide > constraints.maxLongSide
+ ) {
+ return `Video dimensions must have a shorter side of at least ${constraints.minShortSide}px and a longer side of at most ${constraints.maxLongSide}px.`;
+ }
+ return null;
+}
+
+ function resolveSupportedValue(selectedValue, availableValues, fallbackValue) {
+ if (availableValues.includes(selectedValue)) return selectedValue;
+ if (availableValues.includes(fallbackValue)) return fallbackValue;
+ return availableValues[availableValues.length - 1];
+ }
+
+ function getVideoHistoryParameters(requestParams) {
+ const historyParams = {};
+ for (const field of ["aspect_ratio", "duration", "resolution"]) {
+ if (requestParams[field] !== undefined) {
+ historyParams[field] = requestParams[field];
+ }
+ }
+ return historyParams;
+ }
+
async function downloadFile(url, filename) {
try {
const response = await fetch(url);
@@@ -858,137 -608,6 +874,156 @@@ export default function VideoStudio(
[getCurrentModels, selectedModel],
);
+ const currentModelObj = getCurrentModel();
+ const currentToolPresentation = useMemo(
+ () => getVideoToolPresentation(currentModelObj),
+ [currentModelObj],
+ );
+ const currentVideoValidationKey =
+ currentToolPresentation.videoConstraints?.key || null;
+ const uploadedVideoIsValidated =
+ !currentVideoValidationKey ||
+ uploadedVideoValidationKey === currentVideoValidationKey;
+ const continuationConfig = useMemo(
+ () => getContinuationConfig(currentModelObj),
+ [currentModelObj],
+ );
+ const compatibleContinuationSources = useMemo(
+ () => getCompatibleContinuationSources(currentModelObj, history),
+ [currentModelObj, history],
+ );
+ const selectedContinuationSource = useMemo(() => {
+ if (!continuationConfig) return null;
+ const selectedRequestId = continuationSourceIds[continuationConfig.family];
+ return (
+ compatibleContinuationSources.find(
+ (entry) => entry.requestId === selectedRequestId,
+ ) || null
+ );
+ }, [
+ compatibleContinuationSources,
+ continuationConfig,
+ continuationSourceIds,
+ ]);
+ const videoToolOptionDefinitions = useMemo(
+ () => getVideoToolOptionDefinitions(currentModelObj),
+ [currentModelObj],
+ );
+ const v2vRequestParams = useMemo(() => {
+ if (!v2vMode || !currentModelObj) return null;
+
+ const params = {
+ model: selectedModel,
+ video_url: uploadedVideoUrl,
+ options: videoToolOptions,
+ };
+ if (currentModelObj.imageField && uploadedImageUrl) {
+ params.image_url = uploadedImageUrl;
+ }
+
+ const trimmedPrompt = prompt.trim();
+ if (currentToolPresentation.showPrompt && trimmedPrompt) {
+ params.prompt = trimmedPrompt;
+ }
+ return params;
+ }, [
+ currentModelObj,
+ currentToolPresentation.showPrompt,
+ prompt,
+ selectedModel,
+ uploadedImageUrl,
+ uploadedVideoUrl,
+ v2vMode,
+ videoToolOptions,
+ ]);
+ const costEstimateKey = useMemo(
+ () =>
+ currentToolPresentation.estimateCost && v2vRequestParams
+ ? JSON.stringify(v2vRequestParams)
+ : null,
+ [currentToolPresentation.estimateCost, v2vRequestParams],
+ );
+ const hasCompleteCostInputs = Boolean(
+ costEstimateKey &&
+ uploadedVideoUrl &&
+ uploadedVideoIsValidated &&
+ (!currentModelObj?.imageField || uploadedImageUrl) &&
+ (!currentToolPresentation.promptRequired || v2vRequestParams?.prompt),
+ );
+
+ useEffect(() => {
+ const requestVersion = ++costEstimateRequestRef.current;
+ if (!costEstimateKey) {
+ setCostEstimate({ status: "idle" });
+ return undefined;
+ }
+ if (!hasCompleteCostInputs) {
+ setCostEstimate({ status: "waiting", key: costEstimateKey });
+ return undefined;
+ }
+
+ const controller = new AbortController();
+ setCostEstimate({ status: "loading", key: costEstimateKey });
+ const timer = setTimeout(() => {
+ estimateV2VCost(v2vRequestParams, controller.signal)
+ .then((estimate) => {
+ if (costEstimateRequestRef.current !== requestVersion) return;
+ setCostEstimate({ status: "ready", key: costEstimateKey, ...estimate });
+ })
+ .catch((error) => {
+ if (
+ error.name === "AbortError" ||
+ costEstimateRequestRef.current !== requestVersion
+ ) {
+ return;
+ }
+ setCostEstimate({
+ status: "error",
+ key: costEstimateKey,
+ });
+ });
+ }, 300);
+
+ return () => {
+ clearTimeout(timer);
+ controller.abort();
+ };
+ }, [
+ costEstimateKey,
+ costEstimateRetry,
+ hasCompleteCostInputs,
+ v2vRequestParams,
+ ]);
+
+ useEffect(() => {
+ if (!uploadedVideoUrl || uploadedVideoIsValidated) return;
+ setUploadedVideoUrl(null);
+ setUploadedVideoName(null);
+ setUploadedVideoValidationKey(null);
+ toast.error(
+ "Re-upload the source video so its requirements can be validated for this tool.",
+ );
+ }, [uploadedVideoIsValidated, uploadedVideoUrl]);
+
++ useEffect(() => {
++ if (v2vMode) return;
++ const availableResolutions = imageMode
++ ? getResolutionsForI2VModel(selectedModel, { duration: selectedDuration })
++ : getResolutionsForVideoModel(selectedModel, { duration: selectedDuration });
++ if (
++ availableResolutions.length > 0 &&
++ !availableResolutions.includes(selectedResolution)
++ ) {
++ setSelectedResolution(availableResolutions[availableResolutions.length - 1]);
++ }
++ }, [
++ imageMode,
++ selectedDuration,
++ selectedModel,
++ selectedResolution,
++ v2vMode,
++ ]);
++
const isMotionControlSelection = useCallback(
(modelId, isV2v) => {
if (!isV2v) return false;
@@@ -1762,8 -1262,16 +1816,16 @@@
i2vParams.image_url = uploadedImageUrl;
}
if (trimmedPrompt) i2vParams.prompt = trimmedPrompt;
- i2vParams.aspect_ratio = selectedAr;
+ const i2vModel = i2vModels.find((m) => m.id === selectedModel);
+ const aspectRatios = getAspectRatiosForI2VModel(selectedModel);
+ if (aspectRatios.length > 0) {
+ i2vParams.aspect_ratio = resolveSupportedValue(
+ selectedAr,
+ aspectRatios,
+ i2vModel?.inputs?.aspect_ratio?.default,
+ );
+ }
- if (uploadedEndImageUrl && i2vModel?.lastImageField) {
+ if (uploadedEndImageUrl && imageProfile.supportsEndFrame) {
i2vParams.last_image = uploadedEndImageUrl;
}
const durations = getDurationsForI2VModel(selectedModel);
@@@ -1787,10 -1312,7 +1861,8 @@@
url: res.url,
prompt: trimmedPrompt,
model: selectedModel,
+ modelName: selectedModelName,
- aspect_ratio: selectedAr,
- duration: selectedDuration,
- resolution,
+ ...getVideoHistoryParameters(i2vParams),
timestamp: new Date().toISOString(),
};
addToLocalHistory(entry);
@@@ -1825,35 -1343,47 +1897,42 @@@
params.aspect_ratio = selectedAr;
}
+ params.options = videoToolOptions;
+
const durations = getDurationsForModel(selectedModel);
- if (durations.length > 0) params.duration = selectedDuration;
- const resolutions = getResolutionsForVideoModel(selectedModel);
- const resolution =
- resolutions.length > 0 ? selectedResolution : undefined;
- if (resolution) params.resolution = resolution;
+ if (durations.length > 0) {
+ params.duration = resolveSupportedValue(
+ selectedDuration,
+ durations,
+ currentModel?.inputs?.duration?.default,
+ );
+ }
+ const resolutions = getResolutionsForVideoModel(selectedModel, {
+ duration: params.duration,
+ });
+ if (resolutions.length > 0) {
+ params.resolution = resolveSupportedValue(
+ selectedResolution,
+ resolutions,
+ resolutions[resolutions.length - 1],
+ );
+ }
if (selectedQuality) params.quality = selectedQuality;
if (selectedMode) params.mode = selectedMode;
res = await generateVideo(apiKey, params);
if (!res?.url) throw new Error("No video URL returned by API");
- const genId = res.id || Date.now().toString();
- if (
- selectedModel === "seedance-v2.0-t2v" ||
- selectedModel === "seedance-v2.0-i2v"
- ) {
- setLastGenerationId(genId);
- setLastGenerationModel(selectedModel);
- } else {
- setLastGenerationId(null);
- setLastGenerationModel(null);
- }
+ const requestId = res.request_id || null;
+ const genId = requestId || res.id || Date.now().toString();
const entry = {
id: genId,
+ requestId,
url: res.url,
- prompt: trimmedPrompt,
+ prompt: submittedPrompt,
model: selectedModel,
+ modelName: selectedModelName,
- aspect_ratio: continuationConfig ? undefined : selectedAr,
- duration:
- getDurationsForModel(selectedModel).length > 0
- ? selectedDuration
- : undefined,
- resolution,
+ ...getVideoHistoryParameters(params),
timestamp: new Date().toISOString(),
};
addToLocalHistory(entry);
@@@ -1862,9 -1392,7 +1941,9 @@@
onGenerationComplete({
url: res.url,
model: selectedModel,
- prompt: trimmedPrompt,
+ prompt: submittedPrompt,
+ requestId,
- resolution,
++ resolution: params.resolution,
type: "video",
});
}