Add storyboard/video-gen/video-editing handlers and asset previews (#37-39, #45, #46)

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>
This commit is contained in:
m105
2026-08-06 21:15:31 +03:00
co-authored by Claude Sonnet 5
parent 55fa2ebfda
commit c249a14d78
18 changed files with 1026 additions and 34 deletions
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers;
use App\Models\Asset;
use App\Models\VideoProject;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class VideoProjectAssetController extends Controller
{
public function show(VideoProject $videoProject, Asset $asset): StreamedResponse
{
abort_if($asset->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',
]);
}
}
+6
View File
@@ -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),
]));
}
+15 -6
View File
@@ -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()}");
}
+8 -1
View File
@@ -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
@@ -0,0 +1,116 @@
<?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;
/**
* Breaks the project's script into a numbered scene-by-scene storyboard —
* each scene gets a description and an image-generation prompt — via an
* OpenAI-backed AiProvider in JSON mode. Produces the text/description
* layer only; VideoGenerationHandler turns each scene's visual_prompt into
* an actual image.
*/
class StoryboardHandler implements StepHandler
{
public function __construct(
private readonly AiProviderManager $aiProviders,
private readonly AssetStorage $assets,
) {}
public function type(): string
{
return 'storyboard';
}
public function handle(WorkflowStep $step, WorkflowEngine $engine): void
{
$provider = AiProvider::query()->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;
}
}
@@ -0,0 +1,185 @@
<?php
namespace App\Support\VideoWorkflow\StepHandlers;
use App\Models\Asset;
use App\Models\WorkflowStep;
use App\Support\AssetStorage\AssetStorage;
use App\Support\VideoWorkflow\WorkflowEngine;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
/**
* Assembles the scene images from a completed video_generation step and
* (optionally) the project's voiceover audio into an actual video file,
* via the ffmpeg/ffprobe CLIs. Each scene image is shown for an equal
* share of the audio's duration (or a fixed duration if there's no
* audio); no transitions/effects — that's future scope, not what this
* pass covers.
*/
class VideoEditingHandler implements StepHandler
{
private const DEFAULT_SECONDS_PER_SCENE = 4.0;
public function __construct(private readonly AssetStorage $assets) {}
public function type(): string
{
return 'video_editing';
}
public function handle(WorkflowStep $step, WorkflowEngine $engine): void
{
$images = $this->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<int, Asset>|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();
}
}
@@ -0,0 +1,123 @@
<?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;
}
}