UNPKG

stability-ai

Version:
98 lines 4.08 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.imageToVideoResult = exports.imageToVideo = void 0; const axios_1 = __importDefault(require("axios")); const fs_extra_1 = __importDefault(require("fs-extra")); const form_data_1 = __importDefault(require("form-data")); const util_1 = require("../../util"); const error_1 = require("../../error"); const Util = __importStar(require("../../util")); const RESOURCE = 'image-to-video'; var Endpoint; (function (Endpoint) { Endpoint["IMAGE_TO_VIDEO"] = ""; Endpoint["IMAGE_TO_VIDEO_RESULT"] = "result"; })(Endpoint || (Endpoint = {})); /** * Stability AI Stable Video Image to Video (v2beta) * * @param image - URL or filepath of the image to convert to video * @param options - Image to Video Options */ async function imageToVideo(image, cfgScale = 1.8, motionBucketId = 127, options) { const imagePath = new Util.ImagePath(image); const formData = { image: fs_extra_1.default.createReadStream(await imagePath.filepath()), motion_bucket_id: motionBucketId, cfg_scale: cfgScale, }; if (options === null || options === void 0 ? void 0 : options.seed) formData.seed = options.seed; const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.IMAGE_TO_VIDEO), axios_1.default.toFormData(formData, new form_data_1.default()), { validateStatus: undefined, headers: { ...this.authHeaders, Accept: 'application/json', }, }); imagePath.cleanup(); if (response.status === 200 && typeof response.data.id === 'string') { return { id: response.data.id }; } throw new error_1.StabilityAIError(response.status, 'Failed to start stable video image to video', response.data); } exports.imageToVideo = imageToVideo; /** * Stability AI Stable Video Image To Video Result (v2beta) * * @param id - ID of the upscale job */ async function imageToVideoResult(...args) { const [id] = args; const response = await axios_1.default.get(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.IMAGE_TO_VIDEO_RESULT) + `/${id}`, { validateStatus: undefined, headers: { ...this.authHeaders, Accept: 'application/json', }, }); if (response.status === 200) { return Util.processContentResponse(response.data, 'mp4', 'v2beta_image_to_video'); } else if (response.status === 202 && typeof response.data.id === 'string' && response.data.status === 'in-progress') { const { id, status } = response.data; return { id, status }; } throw new error_1.StabilityAIError(response.status, 'Failed to fetch stable video image to video result', response.data); } exports.imageToVideoResult = imageToVideoResult; //# sourceMappingURL=image-to-video.js.map