UNPKG

@tsavo/creatify-api-ts

Version:

TypeScript client for the Creatify AI API

4 lines 8.52 kB
'use strict';var chunkCVUGJ77J_js=require('./chunk-CVUGJ77J.js');// Creatify API TypeScript Client - MIT License var I={};chunkCVUGJ77J_js.a(I,{AudioProcessor:()=>y,BatchProcessor:()=>x,VideoCreator:()=>_});var B={};var h=class{options;avatar;urlToVideo;textToSpeech;aiEditing;customTemplates;dyoa;aiShorts;aiScripts;musics;workspace;lipsyncV2;constructor(t){this.options=t,this.avatar=new chunkCVUGJ77J_js.e(t,chunkCVUGJ77J_js.d),this.urlToVideo=new chunkCVUGJ77J_js.f(t,chunkCVUGJ77J_js.d),this.textToSpeech=new chunkCVUGJ77J_js.g(t,chunkCVUGJ77J_js.d),this.aiEditing=new chunkCVUGJ77J_js.h(t,chunkCVUGJ77J_js.d),this.customTemplates=new chunkCVUGJ77J_js.i(t,chunkCVUGJ77J_js.d),this.dyoa=new chunkCVUGJ77J_js.j(t,chunkCVUGJ77J_js.d),this.aiShorts=new chunkCVUGJ77J_js.k(t,chunkCVUGJ77J_js.d),this.aiScripts=new chunkCVUGJ77J_js.l(t,chunkCVUGJ77J_js.d),this.musics=new chunkCVUGJ77J_js.m(t,chunkCVUGJ77J_js.d),this.workspace=new chunkCVUGJ77J_js.n(t,chunkCVUGJ77J_js.d),this.lipsyncV2=new chunkCVUGJ77J_js.o(t,chunkCVUGJ77J_js.d);}};var _=class{creatify;avatarApi;avatarCache={};voiceCache={};avatarsLoaded=false;voicesLoaded=false;constructor(t,e,i=chunkCVUGJ77J_js.d){let r=typeof t=="string"?{apiId:t,apiKey:e||""}:t;this.creatify=new h(r),this.avatarApi=new chunkCVUGJ77J_js.e(r,i);}async preload(){return await Promise.all([this.loadAvatars(),this.loadVoices()]),{avatars:Object.keys(this.avatarCache).length,voices:Object.keys(this.voiceCache).length}}async loadAvatars(){if(this.avatarsLoaded)return Object.values(this.avatarCache);let t=await this.avatarApi.getAvatars();for(let e of t)this.avatarCache[e.id]=e;return this.avatarsLoaded=true,t}async loadVoices(){if(this.voicesLoaded)return Object.values(this.voiceCache);let t=await this.avatarApi.getVoices();for(let e of t)this.voiceCache[e.voice_id]=e;return this.voicesLoaded=true,t}async findAvatarByName(t){this.avatarsLoaded||await this.loadAvatars();let e=t.toLowerCase();return Object.values(this.avatarCache).find(i=>i.name?.toLowerCase().includes(e))}async findVoiceByName(t){this.voicesLoaded||await this.loadVoices();let e=t.toLowerCase();return Object.values(this.voiceCache).find(i=>i.name?.toLowerCase().includes(e))}async createVideo(t){try{let e=t.pollInterval||5e3,i=t.maxPollingAttempts||30,r=t.aspectRatio||"16:9",n=t.avatarId;if(!n&&t.avatarName){let a=await this.findAvatarByName(t.avatarName);if(!a)throw new Error(`Avatar with name "${t.avatarName}" not found`);n=a.avatar_id;}if(!n){let a=await this.loadAvatars();if(a.length===0)throw new Error("No avatars available. Please check your API credentials.");n=a[0].avatar_id;}let d=t.voiceId;if(!d&&t.voiceName){let a=await this.findVoiceByName(t.voiceName);if(!a)throw new Error(`Voice with name "${t.voiceName}" not found`);d=a.voice_id;}let p=await this.avatarApi.createLipsync({text:t.script,creator:n||"",aspect_ratio:r,voice_id:d});if(!p||!p.id)throw new Error("Failed to create lipsync video: Invalid response from API");let s=await this.avatarApi.getLipsync(p.id),g=0;for(;s.status!=="done"&&s.status!=="error"&&g<i;)await new Promise(a=>setTimeout(a,e)),s=await this.avatarApi.getLipsync(p.id),g++;if(s.status==="done"&&s.output)return {id:p.id,status:"done",url:s.output};throw new Error(s.error_message||`Video generation failed or timed out. Status: ${s.status}`)}catch(e){throw e instanceof Error?e:typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"?new Error(e.message):new Error("An error occurred with the API request")}}async createConversation(t){try{let e=t.conversation,i=t.pollInterval||5e3,r=t.maxPollingAttempts||30,n=t.aspectRatio||"16:9",d=t.backgroundUrl||"https://video.creatify.ai/bg.jpg",p=[];for(let o of e){let u=o.avatarId;if(!u&&o.avatarName){let l=await this.findAvatarByName(o.avatarName);if(!l)throw new Error(`Avatar with name "${o.avatarName}" not found`);u=l.avatar_id;}if(!u){let l=await this.loadAvatars();if(l.length===0)throw new Error("No avatars available. Please check your API credentials.");u=l[0].avatar_id;}let m=o.voiceId;if(!m&&o.voiceName){let l=await this.findVoiceByName(o.voiceName);if(!l)throw new Error(`Voice with name "${o.voiceName}" not found`);m=l.voice_id;}p.push({character:{type:"avatar",avatar_id:u||"",avatar_style:"normal",offset:{x:-.23,y:.35}},voice:{type:"text",input_text:o.text,voice_id:m||""},background:{type:"image",url:d},caption_setting:{style:"normal-black",offset:{x:0,y:.45}}});}let s=await this.avatarApi.createMultiAvatarLipsync({video_inputs:p,aspect_ratio:n});if(!s||!s.id)throw new Error("Failed to create multi-avatar lipsync video: Invalid response from API");let g=await this.avatarApi.getLipsync(s.id),a=0;for(;g.status!=="done"&&g.status!=="error"&&a<r;)await new Promise(o=>setTimeout(o,i)),g=await this.avatarApi.getLipsync(s.id),a++;if(g.status==="done"&&g.output)return {id:s.id,status:"done",url:g.output};throw new Error(g.error_message||`Video generation failed or timed out. Status: ${g.status}`)}catch(e){throw e instanceof Error?e:typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"?new Error(e.message):new Error("An error occurred with the API request")}}};var y=class{api;constructor(t,e){let i=typeof t=="string"?{apiId:t,apiKey:e}:t;this.api=new chunkCVUGJ77J_js.g(i);}async generateAudio(t,e,i=true){let r={script:t,accent:e};return i?this.api.createAndWaitForTextToSpeech(r):this.api.createTextToSpeech(r)}async checkAudioStatus(t){return this.api.getTextToSpeech(t)}async waitForAudioCompletion(t,e=2e3,i=60){let r=0,n=await this.api.getTextToSpeech(t);for(;r<i&&n.status!=="done"&&n.status!=="error";)await new Promise(d=>setTimeout(d,e)),n=await this.api.getTextToSpeech(t),r++;if(r>=i&&n.status!=="done"&&n.status!=="error")throw new Error(`Audio generation task ${t} did not complete within the timeout period`);return n}async listAudioTasks(){return this.api.getTextToSpeechList()}};var N={concurrency:3,continueOnError:false,taskStartDelay:500,maxRetries:0},x=class{api;constructor(t,e,i=chunkCVUGJ77J_js.d){let r=typeof t=="string"?{apiId:t,apiKey:e}:t;this.api=new h(r);}async processBatch(t,e={}){let i={...N,...e},r={successes:[],errors:[],allSuccessful:true},n=0,d=0,p=[];for(;d<t.length;){if(n>=i.concurrency){await Promise.race(p);continue}let s=d++;n++;let g=(async()=>{try{i.taskStartDelay>0&&s>0&&await new Promise(m=>setTimeout(m,i.taskStartDelay));let a,o,u=0;for(;u<=i.maxRetries;)try{a=await t[s]();break}catch(m){if(o=m,u++,u<=i.maxRetries){let l=Math.min(1e3*Math.pow(2,u-1),3e4);await new Promise(E=>setTimeout(E,l)),console.warn(`Retrying task ${s}, attempt ${u} of ${i.maxRetries}`);}}if(a)r.successes.push(a);else if(r.errors.push({index:s,error:o}),r.allSuccessful=!1,!i.continueOnError)throw o}catch(a){if(r.errors.push({index:s,error:a}),r.allSuccessful=false,!i.continueOnError)throw a}finally{n--;let a=p.findIndex(o=>o===g);a!==-1&&p.splice(a,1);}})();if(p.push(g),!i.continueOnError&&!r.allSuccessful)break}return p.length>0&&await Promise.all(p).catch(s=>{i.continueOnError&&console.error("Unexpected error in batch processing:",s);}),r}async processAvatarBatch(t,e={}){let i=t.map(r=>async()=>this.api.avatar.createAndWaitForLipsync({text:r.text,creator:r.avatarId,voice_id:r.voiceId,aspect_ratio:r.aspectRatio}));return this.processBatch(i,e)}async processTextToSpeechBatch(t,e={}){let i=t.map(r=>async()=>this.api.textToSpeech.createAndWaitForTextToSpeech({script:r.script,accent:r.accent}));return this.processBatch(i,e)}async processAiEditingBatch(t,e={}){let i=t.map(r=>async()=>this.api.aiEditing.createAndWaitForAiEditing({video_url:r.videoUrl,editing_style:r.editingStyle}));return this.processBatch(i,e)}async processAiShortsBatch(t,e={}){let i=t.map(r=>async()=>this.api.aiShorts.createAndWaitForAiShorts({prompt:r.prompt,aspect_ratio:r.aspectRatio,target_platform:r.targetPlatform,target_audience:r.targetAudience,language:r.language}));return this.processBatch(i,e)}async processAiScriptsBatch(t,e={}){let i=t.map(r=>async()=>this.api.aiScripts.createAndWaitForAiScript({prompt:r.prompt,target_platform:r.targetPlatform,target_audience:r.targetAudience,language:r.language,script_length:r.scriptLength}));return this.processBatch(i,e)}async processLipsyncV2Batch(t,e={}){let i=t.map(r=>async()=>this.api.lipsyncV2.createAndWaitForLipsyncV2({video_inputs:r.videoInputs,aspect_ratio:r.aspectRatio}));return this.processBatch(i,e)}}; exports.a=B;exports.b=_;exports.c=y;exports.d=x;exports.e=I;exports.f=h;//# sourceMappingURL=chunk-WZFLY6CB.js.map //# sourceMappingURL=chunk-WZFLY6CB.js.map