@@ -176,7 +176,6 @@ export async function imageCreateCommand(
176176 {
177177 animationPrompt,
178178 conversationId : createResult . conversationId ,
179- branchPath : createResult . branchPath ,
180179 extendCount,
181180 outputVideo : outputVideo ! ,
182181 } ,
@@ -654,7 +653,6 @@ async function createImageAnimations(
654653 options : {
655654 animationPrompt : string ;
656655 conversationId : string ;
657- branchPath : string ;
658656 extendCount : number ;
659657 outputVideo : string ;
660658 } ,
@@ -709,7 +707,6 @@ async function createImageAnimation(
709707 imageIndex : number ;
710708 animationPrompt : string ;
711709 conversationId : string ;
712- branchPath : string ;
713710 extendCount : number ;
714711 videoPath : string ;
715712 } ,
@@ -719,7 +716,6 @@ async function createImageAnimation(
719716 image ,
720717 options . animationPrompt ,
721718 ) ;
722- let currentBranchPath = options . branchPath ;
723719 let currentVideo = await pickFirstCompletedVideo (
724720 client ,
725721 animatedDraft . videos ,
@@ -729,12 +725,10 @@ async function createImageAnimation(
729725
730726 for ( let i = 0 ; i < options . extendCount ; i += 1 ) {
731727 await pauseBetweenMutationJobs ( ) ;
732- const extendedDraft = await client . extendVideo (
733- options . conversationId ,
734- currentBranchPath ,
728+ const extendedDraft = await extendVideoWithLightbox (
729+ sessionName ,
735730 currentVideo ,
736731 ) ;
737- currentBranchPath = extendedDraft . branchPath ;
738732 currentVideo = await pickFirstCompletedVideo (
739733 client ,
740734 extendedDraft . videos ,
@@ -769,7 +763,7 @@ async function createImageAnimation(
769763 } ;
770764}
771765
772- type LightboxAnimateResult = {
766+ type LightboxMediaResult = {
773767 ok : boolean ;
774768 before ?: string ;
775769 after ?: string ;
@@ -789,9 +783,12 @@ async function animateImageWithLightbox(
789783 `https://www.meta.ai/create/${ image . id } ` ,
790784 ) ;
791785
792- const result = await runPlaywrightCliCodeJson < LightboxAnimateResult > (
786+ const result = await runPlaywrightCliCodeJson < LightboxMediaResult > (
793787 sessionName ,
794- await buildCustomAnimateCode ( animationPrompt , image . id ) ,
788+ await buildLightboxAutomationCode ( "metaAiCustomAnimate" , {
789+ prompt : animationPrompt ,
790+ sourceMediaId : image . id ,
791+ } ) ,
795792 ) ;
796793
797794 if ( ! result . ok || ! result . videoId ) {
@@ -802,35 +799,70 @@ async function animateImageWithLightbox(
802799 }
803800
804801 return {
805- videos : [ {
806- id : result . videoId ,
807- url : null ,
808- thumbnail : null ,
809- prompt : animationPrompt ,
810- sourceMedia : {
811- id : image . id ,
812- url : image . url ,
813- thumbnail : image . thumbnail ?? null ,
814- } ,
815- } ] ,
802+ videos : [ buildGeneratedVideoDraft ( result . videoId , animationPrompt , image ) ] ,
816803 } ;
817804}
818805
819- async function buildCustomAnimateCode (
820- prompt : string ,
821- sourceMediaId : string ,
806+ async function extendVideoWithLightbox (
807+ sessionName : string ,
808+ video : CompletedVideo ,
809+ ) : Promise < { videos : GeneratedVideo [ ] } > {
810+ await gotoPlaywrightCliSession (
811+ sessionName ,
812+ `https://www.meta.ai/create/${ video . id } ` ,
813+ ) ;
814+
815+ const result = await runPlaywrightCliCodeJson < LightboxMediaResult > (
816+ sessionName ,
817+ await buildLightboxAutomationCode ( "metaAiExtendAnimation" , {
818+ sourceMediaId : video . id ,
819+ } ) ,
820+ ) ;
821+
822+ if ( ! result . ok || ! result . videoId ) {
823+ const detail = result . reason ? ` ${ result . reason } ` : "" ;
824+ throw new Error (
825+ `Meta video extend did not return a video media id.${ detail } ` ,
826+ ) ;
827+ }
828+
829+ return {
830+ videos : [ buildGeneratedVideoDraft ( result . videoId , "Extend" , video ) ] ,
831+ } ;
832+ }
833+
834+ async function buildLightboxAutomationCode (
835+ functionName : "metaAiCustomAnimate" | "metaAiExtendAnimation" ,
836+ input : Record < string , unknown > ,
822837) : Promise < string > {
823838 const scriptPath = await getCustomAnimateScriptPath ( ) ;
824- const input = { prompt, sourceMediaId } ;
825839
826840 return `async (page) => {
827841 await page.addScriptTag({ path: ${ JSON . stringify ( scriptPath ) } });
828842 return await page.evaluate(async (input) => {
829- return await window.metaAiCustomAnimate (input);
843+ return await window[ ${ JSON . stringify ( functionName ) } ] (input);
830844 }, ${ JSON . stringify ( input ) } );
831845 }` ;
832846}
833847
848+ function buildGeneratedVideoDraft (
849+ id : string ,
850+ prompt : string ,
851+ sourceMedia : GeneratedImage | CompletedVideo ,
852+ ) : GeneratedVideo {
853+ return {
854+ id,
855+ url : null ,
856+ thumbnail : null ,
857+ prompt,
858+ sourceMedia : {
859+ id : sourceMedia . id ,
860+ url : sourceMedia . url ,
861+ thumbnail : sourceMedia . thumbnail ?? null ,
862+ } ,
863+ } ;
864+ }
865+
834866async function getCustomAnimateScriptPath ( ) : Promise < string > {
835867 const scriptUrl = new URL ( "./lib/meta_lightbox_animate.js" , import . meta. url ) ;
836868 if ( scriptUrl . protocol === "file:" ) {
0 commit comments