ExamplescriptIntermediateRunnableguided-flow
Stitch Sora shots into one video with ffmpeg
Find the latest sequence folder, collect its shot videos in order and concatenate them into one file with ffmpeg's concat demuxer.
Key Facts
- Level
- Intermediate
- Runtime
- Python • OpenAI API
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Code last changed
- 7 October 2025
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
6-sora-sequence.py
python
import os
import subprocess
import tempfile
import sys
from pathlib import Path
# --------------------------------------------------------------
# Find most recent sequence folder or use provided path
# --------------------------------------------------------------
# Check if a valid path was provided (not a Jupyter kernel argument)
if len(sys.argv) > 1 and not sys.argv[1].startswith("-"):
sequence_path = Path(sys.argv[1])
else:
folders = sorted(
Path("./output").glob("sequence_*"), key=os.path.getmtime, reverse=True
)
if not folders:
sys.exit("No sequence folders found. Run 5-sora-remix.py first!")
sequence_path = folders[0]
# --------------------------------------------------------------
# Find shot videos
# --------------------------------------------------------------
video_paths = [str(f.resolve()) for f in sorted(sequence_path.glob("shot_*.mp4"))]
if not video_paths:
sys.exit(f"No shot videos found in {sequence_path}")
print(f"Stitching {len(video_paths)} shots from {sequence_path.name}")
# --------------------------------------------------------------
# Create concat file and stitch
# --------------------------------------------------------------
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("\n".join(f"file '{path}'" for path in video_paths))
temp_path = f.name
output_file = sequence_path / "sequence.mp4"
try:
subprocess.run(
[
"ffmpeg",
"-f",
"concat",
"-safe",
"0",
"-i",
temp_path,
"-c",
"copy",
str(output_file),
"-y",
],
check=True,
)
print(f"Done: {output_file}")
finally:
os.unlink(temp_path)
Related principles
- P4TrustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.Open principle →
- P5DelegationReplace implied magic with clear mental modelsThe product should help users understand what the system can do, what it is currently doing, what it cannot do, and what conditions govern its behaviour.Open principle →
- P7TrustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P9OrchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →