From c249a14d788f82bc3097b6ee5b11ebd94df55258 Mon Sep 17 00:00:00 2001
From: m105
Date: Thu, 6 Aug 2026 21:15:31 +0300
Subject: [PATCH] Add storyboard/video-gen/video-editing handlers and asset
previews (#37-39, #45, #46)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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
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
---
.../VideoProjectAssetController.php | 21 ++
app/Providers/AppServiceProvider.php | 6 +
app/Support/AiProviders/OpenAiDriver.php | 21 +-
app/Support/AssetStorage/AssetStorage.php | 9 +-
.../StepHandlers/StoryboardHandler.php | 116 +++++++++++
.../StepHandlers/VideoEditingHandler.php | 185 +++++++++++++++++
.../StepHandlers/VideoGenerationHandler.php | 123 ++++++++++++
.../partials/asset-preview.blade.php | 26 +++
.../livewire/video-projects/show.blade.php | 43 ++--
routes/web.php | 1 +
.../Feature/AiProviders/OpenAiDriverTest.php | 20 ++
.../Feature/AssetStorage/AssetStorageTest.php | 4 +-
tests/Feature/Jobs/RunWorkflowStepJobTest.php | 2 +-
.../Livewire/VideoProjectsShowTest.php | 33 ++-
.../VideoProjectAssetControllerTest.php | 50 +++++
.../StepHandlers/StoryboardHandlerTest.php | 100 +++++++++
.../StepHandlers/VideoEditingHandlerTest.php | 189 ++++++++++++++++++
.../VideoGenerationHandlerTest.php | 111 ++++++++++
18 files changed, 1026 insertions(+), 34 deletions(-)
create mode 100644 app/Http/Controllers/VideoProjectAssetController.php
create mode 100644 app/Support/VideoWorkflow/StepHandlers/StoryboardHandler.php
create mode 100644 app/Support/VideoWorkflow/StepHandlers/VideoEditingHandler.php
create mode 100644 app/Support/VideoWorkflow/StepHandlers/VideoGenerationHandler.php
create mode 100644 resources/views/livewire/video-projects/partials/asset-preview.blade.php
create mode 100644 tests/Feature/VideoProjectAssetControllerTest.php
create mode 100644 tests/Feature/VideoWorkflow/StepHandlers/StoryboardHandlerTest.php
create mode 100644 tests/Feature/VideoWorkflow/StepHandlers/VideoEditingHandlerTest.php
create mode 100644 tests/Feature/VideoWorkflow/StepHandlers/VideoGenerationHandlerTest.php
diff --git a/app/Http/Controllers/VideoProjectAssetController.php b/app/Http/Controllers/VideoProjectAssetController.php
new file mode 100644
index 0000000..d3a09eb
--- /dev/null
+++ b/app/Http/Controllers/VideoProjectAssetController.php
@@ -0,0 +1,21 @@
+video_project_id !== $videoProject->id, 404);
+ abort_unless(Storage::exists($asset->disk_path), 404);
+
+ return Storage::response($asset->disk_path, null, [
+ 'Content-Type' => $asset->mime_type ?: 'application/octet-stream',
+ ]);
+ }
+}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index 21e663d..722c92a 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -13,7 +13,10 @@ use App\Support\Publishing\TwitterPublisher;
use App\Support\Publishing\WordPressPublisher;
use App\Support\VideoWorkflow\StepHandlers\ScriptGenerationHandler;
use App\Support\VideoWorkflow\StepHandlers\StepHandlerManager;
+use App\Support\VideoWorkflow\StepHandlers\StoryboardHandler;
use App\Support\VideoWorkflow\StepHandlers\TtsVoiceoverHandler;
+use App\Support\VideoWorkflow\StepHandlers\VideoEditingHandler;
+use App\Support\VideoWorkflow\StepHandlers\VideoGenerationHandler;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -39,6 +42,9 @@ class AppServiceProvider extends ServiceProvider
$this->app->singleton(StepHandlerManager::class, fn ($app) => new StepHandlerManager([
$app->make(ScriptGenerationHandler::class),
$app->make(TtsVoiceoverHandler::class),
+ $app->make(StoryboardHandler::class),
+ $app->make(VideoGenerationHandler::class),
+ $app->make(VideoEditingHandler::class),
]));
}
diff --git a/app/Support/AiProviders/OpenAiDriver.php b/app/Support/AiProviders/OpenAiDriver.php
index 170a60f..8bd2d52 100644
--- a/app/Support/AiProviders/OpenAiDriver.php
+++ b/app/Support/AiProviders/OpenAiDriver.php
@@ -16,14 +16,23 @@ class OpenAiDriver implements AiProviderDriver
{
$model = $options['model'] ?? $provider->config['model'] ?? 'gpt-4o-mini';
+ $body = [
+ 'model' => $model,
+ 'messages' => [
+ ['role' => 'user', 'content' => $prompt],
+ ],
+ ];
+
+ // Ask the API to guarantee valid JSON back, for callers that need
+ // structured output (e.g. a storyboard's scene breakdown). The
+ // prompt itself must still ask for JSON — this only enforces it.
+ if ($options['json_mode'] ?? false) {
+ $body['response_format'] = ['type' => 'json_object'];
+ }
+
try {
$response = Http::withToken($provider->api_key)
- ->post('https://api.openai.com/v1/chat/completions', [
- 'model' => $model,
- 'messages' => [
- ['role' => 'user', 'content' => $prompt],
- ],
- ]);
+ ->post('https://api.openai.com/v1/chat/completions', $body);
} catch (ConnectionException $e) {
throw new AiGenerationException("could not connect to OpenAI: {$e->getMessage()}");
}
diff --git a/app/Support/AssetStorage/AssetStorage.php b/app/Support/AssetStorage/AssetStorage.php
index 64c91e1..adca7db 100644
--- a/app/Support/AssetStorage/AssetStorage.php
+++ b/app/Support/AssetStorage/AssetStorage.php
@@ -59,9 +59,16 @@ class AssetStorage
return Storage::get($asset->disk_path);
}
+ /**
+ * A URL to view/download this asset. Not Storage::url() — the default
+ * 'local' disk has no configured URL driver and throws if asked for
+ * one, so this goes through the authenticated streaming route instead.
+ */
public function url(Asset $asset): ?string
{
- return Storage::exists($asset->disk_path) ? Storage::url($asset->disk_path) : null;
+ return Storage::exists($asset->disk_path)
+ ? route('video-projects.assets.show', [$asset->video_project_id, $asset->id])
+ : null;
}
public function delete(Asset $asset): void
diff --git a/app/Support/VideoWorkflow/StepHandlers/StoryboardHandler.php b/app/Support/VideoWorkflow/StepHandlers/StoryboardHandler.php
new file mode 100644
index 0000000..09821d4
--- /dev/null
+++ b/app/Support/VideoWorkflow/StepHandlers/StoryboardHandler.php
@@ -0,0 +1,116 @@
+where('provider', 'openai')->where('is_active', true)->first();
+
+ if (! $provider) {
+ $engine->failStep($step, 'No active OpenAI provider configured.');
+
+ return;
+ }
+
+ $script = $this->resolveScript($step);
+
+ if (! $script) {
+ $engine->failStep($step, 'No script available to storyboard — complete a script_generation step first.');
+
+ return;
+ }
+
+ try {
+ $result = $this->aiProviders->generate($provider, $this->buildPrompt($step, $script), ['json_mode' => true]);
+ } catch (AiGenerationException $e) {
+ $engine->failStep($step, $e->getMessage());
+
+ return;
+ }
+
+ $decoded = json_decode($result->content, true);
+ $scenes = $decoded['scenes'] ?? null;
+
+ if (! is_array($scenes) || empty($scenes)) {
+ $engine->failStep($step, 'AI provider returned an unusable storyboard: no scenes found.');
+
+ return;
+ }
+
+ $asset = $this->assets->store(
+ $step->videoProject,
+ 'other',
+ json_encode(['scenes' => $scenes], JSON_PRETTY_PRINT),
+ 'json',
+ step: $step,
+ mimeType: 'application/json',
+ metadata: ['kind' => 'storyboard', 'scene_count' => count($scenes)],
+ );
+
+ $engine->completeStep($step, [
+ 'asset_id' => $asset->id,
+ 'scene_count' => count($scenes),
+ ]);
+ }
+
+ private function resolveScript(WorkflowStep $step): ?string
+ {
+ $input = $step->input ?? [];
+
+ if (! empty($input['script'])) {
+ return $input['script'];
+ }
+
+ $scriptAsset = $step->videoProject->assets()
+ ->where('type', 'script')
+ ->orderByDesc('version')
+ ->first();
+
+ return $scriptAsset ? $this->assets->contents($scriptAsset) : null;
+ }
+
+ private function buildPrompt(WorkflowStep $step, string $script): string
+ {
+ $input = $step->input ?? [];
+
+ $prompt = "Break the following video script into a numbered scene-by-scene storyboard.\n\n"
+ ."Script:\n{$script}\n\n"
+ .'For each scene provide: scene_number (integer), description (what happens on screen), '
+ ."and visual_prompt (a concise, self-contained image-generation prompt describing this scene's visual).";
+
+ if (! empty($input['style'])) {
+ $prompt .= " Every visual_prompt must consistently reflect this visual style: {$input['style']}.";
+ }
+
+ $prompt .= "\n\nRespond with JSON only, in exactly this shape: "
+ .'{"scenes": [{"scene_number": 1, "description": "...", "visual_prompt": "..."}]}';
+
+ return $prompt;
+ }
+}
diff --git a/app/Support/VideoWorkflow/StepHandlers/VideoEditingHandler.php b/app/Support/VideoWorkflow/StepHandlers/VideoEditingHandler.php
new file mode 100644
index 0000000..e943f78
--- /dev/null
+++ b/app/Support/VideoWorkflow/StepHandlers/VideoEditingHandler.php
@@ -0,0 +1,185 @@
+resolveSceneImages($step);
+
+ if (! $images || $images->isEmpty()) {
+ $engine->failStep($step, 'No scene images available — complete a video_generation step first.');
+
+ return;
+ }
+
+ $audioAsset = $this->resolveAudio($step);
+
+ $tempDir = storage_path('app/tmp/video-editing-'.$step->id.'-'.uniqid());
+ File::ensureDirectoryExists($tempDir);
+
+ try {
+ $imagePaths = $images->map(fn (Asset $a) => Storage::path($a->disk_path))->all();
+ $audioPath = $audioAsset ? Storage::path($audioAsset->disk_path) : null;
+
+ $duration = null;
+
+ if ($audioPath) {
+ $duration = $this->probeDuration($audioPath);
+
+ if ($duration === null) {
+ $engine->failStep($step, 'Could not read the voiceover audio\'s duration.');
+
+ return;
+ }
+ }
+
+ $input = $step->input ?? [];
+ $perScene = $input['seconds_per_scene'] ?? ($duration ? $duration / count($imagePaths) : self::DEFAULT_SECONDS_PER_SCENE);
+
+ $concatListPath = "{$tempDir}/concat.txt";
+ $this->writeConcatList($concatListPath, $imagePaths, $perScene);
+
+ $silentVideoPath = "{$tempDir}/silent.mp4";
+ $result = Process::timeout(300)->run([
+ 'ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', $concatListPath,
+ '-vsync', 'vfr', '-pix_fmt', 'yuv420p', $silentVideoPath,
+ ]);
+
+ if ($result->failed()) {
+ $engine->failStep($step, 'ffmpeg failed to assemble scene images: '.trim($result->errorOutput()));
+
+ return;
+ }
+
+ $finalVideoPath = "{$tempDir}/final.mp4";
+
+ if ($audioPath) {
+ $result = Process::timeout(300)->run([
+ 'ffmpeg', '-y', '-i', $silentVideoPath, '-i', $audioPath,
+ '-c:v', 'copy', '-c:a', 'aac', '-shortest', $finalVideoPath,
+ ]);
+
+ if ($result->failed()) {
+ $engine->failStep($step, 'ffmpeg failed to mux the voiceover audio: '.trim($result->errorOutput()));
+
+ return;
+ }
+ } else {
+ File::copy($silentVideoPath, $finalVideoPath);
+ }
+
+ $asset = $this->assets->store(
+ $step->videoProject,
+ 'video',
+ File::get($finalVideoPath),
+ 'mp4',
+ step: $step,
+ mimeType: 'video/mp4',
+ metadata: ['scene_count' => count($imagePaths), 'duration_seconds' => $duration ?? ($perScene * count($imagePaths))],
+ );
+
+ $engine->completeStep($step, ['asset_id' => $asset->id]);
+ } finally {
+ File::deleteDirectory($tempDir);
+ }
+ }
+
+ private function probeDuration(string $path): ?float
+ {
+ $result = Process::timeout(60)->run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', $path]);
+
+ if ($result->failed()) {
+ return null;
+ }
+
+ $value = trim($result->output());
+
+ return is_numeric($value) ? (float) $value : null;
+ }
+
+ private function writeConcatList(string $path, array $imagePaths, float $secondsPerImage): void
+ {
+ $lines = [];
+
+ foreach ($imagePaths as $imagePath) {
+ $lines[] = "file '".addslashes($imagePath)."'";
+ $lines[] = "duration {$secondsPerImage}";
+ }
+
+ // ffmpeg's concat demuxer ignores the last entry's duration, so the
+ // final image needs to be repeated once more without one.
+ $lines[] = "file '".addslashes(end($imagePaths))."'";
+
+ File::put($path, implode("\n", $lines));
+ }
+
+ /**
+ * @return Collection|null
+ */
+ private function resolveSceneImages(WorkflowStep $step): ?Collection
+ {
+ $input = $step->input ?? [];
+
+ $ids = $input['image_asset_ids'] ?? null;
+
+ if (! $ids) {
+ $videoGenStep = $step->videoProject->steps()
+ ->where('type', 'video_generation')
+ ->where('status', 'completed')
+ ->orderByDesc('completed_at')
+ ->first();
+
+ $ids = $videoGenStep?->output['asset_ids'] ?? null;
+ }
+
+ if (! $ids) {
+ return null;
+ }
+
+ $assetsById = Asset::whereIn('id', $ids)->get()->keyBy('id');
+
+ return collect($ids)->map(fn ($id) => $assetsById->get($id))->filter()->values();
+ }
+
+ private function resolveAudio(WorkflowStep $step): ?Asset
+ {
+ $input = $step->input ?? [];
+
+ if (! empty($input['audio_asset_id'])) {
+ return Asset::find($input['audio_asset_id']);
+ }
+
+ return $step->videoProject->assets()
+ ->where('type', 'audio')
+ ->orderByDesc('version')
+ ->first();
+ }
+}
diff --git a/app/Support/VideoWorkflow/StepHandlers/VideoGenerationHandler.php b/app/Support/VideoWorkflow/StepHandlers/VideoGenerationHandler.php
new file mode 100644
index 0000000..a001490
--- /dev/null
+++ b/app/Support/VideoWorkflow/StepHandlers/VideoGenerationHandler.php
@@ -0,0 +1,123 @@
+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>|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;
+ }
+}
diff --git a/resources/views/livewire/video-projects/partials/asset-preview.blade.php b/resources/views/livewire/video-projects/partials/asset-preview.blade.php
new file mode 100644
index 0000000..d63bc4b
--- /dev/null
+++ b/resources/views/livewire/video-projects/partials/asset-preview.blade.php
@@ -0,0 +1,26 @@
+@php
+ $assetStorage = app(\App\Support\AssetStorage\AssetStorage::class);
+ $url = $assetStorage->url($asset);
+ $isText = $asset->type === 'script' || in_array($asset->mime_type, ['application/json', 'text/plain']);
+@endphp
+
+