StoryboardHandler (#37): breaks the project's script into a numbered scene-by-scene breakdown (description + image prompt per scene) via OpenAI in JSON mode (added json_mode support to OpenAiDriver for reliable structured output), stored as a JSON asset. VideoGenerationHandler (#38): generates one still image per storyboard scene via StabilityAI — the scene *visuals*, not motion/video clips (no text-to-video model integrated; see the task comment for why). VideoEditingHandler (#39): the step that actually produces a video file — assembles the scene stills into an MP4 via ffmpeg's concat demuxer, timed to the voiceover audio's duration (probed via ffprobe) or a fixed per-scene duration if there's no audio, then muxes the audio track in. Verified against the real ffmpeg/ffprobe binaries on this box, not just Process::fake(). Asset previews (#45, #46): a reusable partial renders each asset inline by type — image/audio/video get real playback elements, text/ JSON gets a truncated <pre> block, everything else a download link. Wired into both the awaiting_approval step view (so approving/ rejecting is an actual content review, not just a word count) and a general project assets grid. Also fixes a latent bug in AssetStorage::url(), found while wiring up previews: it called Storage::url() on the default 'local' disk, which has no configured URL driver and throws in production — only worked before because Storage::fake() is more permissive than the real local driver. Now serves assets through a new authenticated streaming route instead (VideoProjectAssetController). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
124 lines
3.5 KiB
PHP
124 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Support\VideoWorkflow\StepHandlers;
|
|
|
|
use App\Models\AiProvider;
|
|
use App\Models\WorkflowStep;
|
|
use App\Support\AiProviders\AiGenerationException;
|
|
use App\Support\AiProviders\AiProviderManager;
|
|
use App\Support\AssetStorage\AssetStorage;
|
|
use App\Support\VideoWorkflow\WorkflowEngine;
|
|
|
|
/**
|
|
* Generates the scene visuals for a project's storyboard, one still image
|
|
* per scene, via a StabilityAI-backed AiProvider. This produces the visual
|
|
* assets VideoEditingHandler later assembles into an actual video file —
|
|
* it does not itself produce a video (no motion/text-to-video model is
|
|
* integrated).
|
|
*/
|
|
class VideoGenerationHandler implements StepHandler
|
|
{
|
|
public function __construct(
|
|
private readonly AiProviderManager $aiProviders,
|
|
private readonly AssetStorage $assets,
|
|
) {}
|
|
|
|
public function type(): string
|
|
{
|
|
return 'video_generation';
|
|
}
|
|
|
|
public function handle(WorkflowStep $step, WorkflowEngine $engine): void
|
|
{
|
|
$provider = AiProvider::query()->where('provider', 'stability_ai')->where('is_active', true)->first();
|
|
|
|
if (! $provider) {
|
|
$engine->failStep($step, 'No active StabilityAI provider configured.');
|
|
|
|
return;
|
|
}
|
|
|
|
$scenes = $this->resolveScenes($step);
|
|
|
|
if (! $scenes) {
|
|
$engine->failStep($step, 'No storyboard available — complete a storyboard step first.');
|
|
|
|
return;
|
|
}
|
|
|
|
$style = ($step->input ?? [])['style'] ?? null;
|
|
$assetIds = [];
|
|
|
|
foreach ($scenes as $scene) {
|
|
$prompt = $scene['visual_prompt'] ?? $scene['description'] ?? null;
|
|
|
|
if (! $prompt) {
|
|
continue;
|
|
}
|
|
|
|
if ($style) {
|
|
$prompt .= ", {$style}";
|
|
}
|
|
|
|
try {
|
|
$result = $this->aiProviders->generate($provider, $prompt);
|
|
} catch (AiGenerationException $e) {
|
|
$engine->failStep($step, "scene {$scene['scene_number']}: {$e->getMessage()}");
|
|
|
|
return;
|
|
}
|
|
|
|
$extension = str($result->contentType)->after('image/')->toString() ?: 'png';
|
|
|
|
$asset = $this->assets->store(
|
|
$step->videoProject,
|
|
'image',
|
|
$result->content,
|
|
$extension,
|
|
step: $step,
|
|
mimeType: $result->contentType,
|
|
metadata: ['scene_number' => $scene['scene_number'] ?? null, 'prompt' => $prompt],
|
|
);
|
|
|
|
$assetIds[] = $asset->id;
|
|
}
|
|
|
|
if (empty($assetIds)) {
|
|
$engine->failStep($step, 'No scenes had a usable visual_prompt.');
|
|
|
|
return;
|
|
}
|
|
|
|
$engine->completeStep($step, [
|
|
'asset_ids' => $assetIds,
|
|
'scene_count' => count($assetIds),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>|null
|
|
*/
|
|
private function resolveScenes(WorkflowStep $step): ?array
|
|
{
|
|
$input = $step->input ?? [];
|
|
|
|
if (! empty($input['scenes'])) {
|
|
return $input['scenes'];
|
|
}
|
|
|
|
$storyboardAsset = $step->videoProject->assets()
|
|
->where('type', 'other')
|
|
->whereJsonContains('metadata->kind', 'storyboard')
|
|
->orderByDesc('version')
|
|
->first();
|
|
|
|
if (! $storyboardAsset) {
|
|
return null;
|
|
}
|
|
|
|
$decoded = json_decode($this->assets->contents($storyboardAsset), true);
|
|
|
|
return $decoded['scenes'] ?? null;
|
|
}
|
|
}
|