Documentation
¶
Overview ¶
Package export renders a project to a file.
Index ¶
- Constants
- Variables
- func EncodeImage(ctx context.Context, img *image.RGBA, o ImageEncodeOptions) ([]byte, error)
- func ImageOutputSize(srcW, srcH, w, h int) (int, int)
- func RenderAudioPreview(ctx context.Context, job Job, src plan.AudioSource, out string) error
- func RenderImage(ctx context.Context, doc *coreimage.Document, cfg ImageConfig) (*image.RGBA, error)
- type AudioMeasure
- type Check
- type CodecPlan
- type FFmpegWebP
- type Format
- type GIFEstimate
- type ImageCache
- type ImageCacheStats
- type ImageConfig
- type ImageEncodeOptions
- type Job
- type Loudness
- type PixelRenderer
- type Progress
- type RenderConfig
- type Renderer
- func (r *Renderer) Close() error
- func (r *Renderer) MixFrom(ctx context.Context, from, n int, sink func([]float32) error) error
- func (r *Renderer) Render(ctx context.Context, t int, outScale float64) (*image.RGBA, error)
- func (r *Renderer) RenderSize(ctx context.Context, t, w, h int) (*image.RGBA, error)
- type Result
- type Verified
- type WebPEncoder
Constants ¶
const ( CodeLintBlocking = "E_LINT_BLOCKING" CodeExportFailed = "E_EXPORT_FAILED" CodeVerifyFailed = "E_VERIFY_FAILED" )
Error codes of SDD-000 section 8 that an export raises. E_NO_ENCODER and E_INTERNAL come from engine/ffmpeg; a cancelled export returns the error of its context.
const ( ReasonEncoder = "encoder_failed" ReasonDecoder = "decoder_failed" ReasonRender = "render_failed" ReasonAudio = "audio_failed" ReasonTimeout = "timeout" ReasonStalled = "stalled" ReasonPublish = "publish_failed" ReasonBadOutput = "bad_output_dir" ReasonBadFormat = "bad_format" )
Reasons of E_EXPORT_FAILED (details.reason).
const ( ImagePNG = "png" ImageJPEG = "jpeg" ImageWebP = "webp" )
The formats of an image export (SDD-021 section 6).
const DefaultImageCacheBytes = 384 << 20
DefaultImageCacheBytes is the memory the pictures of an ImageCache may hold: six pictures of a 4,000 by 4,000 pixel document (SDD-021 section 6.1).
const DefaultJPEGQuality = 90
DefaultJPEGQuality is the quality of a JPEG when none is asked.
const MaxMeasureSeconds = 600
MaxMeasureSeconds is the longest stretch AudioMeasure renders (SDD-004 section 7.1).
const MaxPreviewSeconds = 4 * 3600
MaxPreviewSeconds is the longest source RenderAudioPreview renders: the length of an item the document allows.
const ReasonSizeLimit = "size_limit"
ReasonSizeLimit is details.reason of an export whose file passed STUARTY_MAX_EXPORT_BYTES.
const ReasonUnsupportedSize = "unsupported_size"
ReasonUnsupportedSize is details.reason of an export the chosen encoder cannot do at the size or the frame rate of the project.
ReasonWebPUnavailable is details.reason of an export in WebP when no encoder is wired.
Variables ¶
var ErrMeasureTooLong = errors.New("export: the range to measure is longer than 600 seconds")
ErrMeasureTooLong is returned for a range longer than MaxMeasureSeconds.
var ImageFormats = []string{ImagePNG, ImageJPEG, ImageWebP}
ImageFormats lists them.
Functions ¶
func EncodeImage ¶ added in v0.31.0
EncodeImage encodes a picture in the format of the options, after resampling it when a size is asked.
func ImageOutputSize ¶ added in v0.37.0
ImageOutputSize is the pixel size of an export of a srcW x srcH picture asked at w x h: 0 on both sides keeps the size, and 0 on one side keeps the aspect.
func RenderAudioPreview ¶ added in v0.39.0
RenderAudioPreview renders one source made by plan.PreviewSource, with its asset range, speed, noise reduction and item chain, through the mixer of the export, and writes it as a 48 kHz stereo 16-bit WAV at out (SDD-004 section 7.1, "Processed audio on the timeline"). The sound is taken before the envelope and before the loudness gain. out must not exist: the caller writes a name of its own and renames it when this returns.
func RenderImage ¶ added in v0.31.0
func RenderImage(ctx context.Context, doc *coreimage.Document, cfg ImageConfig) (*image.RGBA, error)
RenderImage draws an image document at its canvas size with the CPU path of the Rust renderer (the bit-exact one, through wazero), as the export of a video does: it builds the one-frame plan, rasterizes each text and shape, decodes each image layer, uploads them and takes the pixels. The same document and the same files always give the same pixels. The result is premultiplied RGBA, as image.RGBA is.
Types ¶
type AudioMeasure ¶ added in v0.35.0
type AudioMeasure struct {
Before audio.Measurement `json:"before"`
After audio.Measurement `json:"after"`
// Start and End are the frames measured, the end exclusive, and Seconds their length.
Start int `json:"start"`
End int `json:"end"`
Seconds float64 `json:"seconds"`
}
AudioMeasure is what AudioMeasure says about a range: the same mix measured with the processors of the items and of their tracks (After) and without them (Before), both before the loudness gain of the export.
func MeasureAudio ¶ added in v0.35.0
func MeasureAudio(ctx context.Context, job Job, itemID string, start, end int) (*AudioMeasure, error)
MeasureAudio renders the audio of the frames [start, end) of the job's document twice through the mixer of the export, with and without the processors, and measures each in Go (SDD-004 section 7.1). With itemID only that item's source is measured. start and end of 0 and 0 are the whole timeline.
type Check ¶
type Check struct {
Name string `json:"name"`
Expected string `json:"expected"`
Got string `json:"got"`
}
Check is one failed check of the verification of section 8 step 7.
type CodecPlan ¶ added in v0.33.0
type CodecPlan struct {
// Codec is what is written.
Codec exportcore.Codec
// Wanted is the codec that the preset preferred when it could not be had, empty otherwise.
Wanted exportcore.Codec
// Notes tell a person why the codec is not the preferred one.
Notes []string
}
CodecPlan is the codec an export writes, and what the plan has to say about it (SDD-004 section 8.3).
func PlanCodec ¶ added in v0.33.0
func PlanCodec( ctx context.Context, runner *ffmpeg.Runner, caps *ffmpeg.Capabilities, opt ffmpeg.DetectOptions, asked exportcore.Codec, preset *exportcore.Preset, ) (CodecPlan, error)
PlanCodec decides the codec of an export before any work: the codec the caller named, which must be offered (else E_NO_ENCODER with the reason codec_unavailable, never a silent change), or the first codec of the preset's list that the machine offers, or H.264 with a note. A codec is offered when an allowed encoder passes its test encode (ffmpeg.Runner.DetectCodec). runner is the one that runs the detection; the same answers are cached for the export that follows.
type FFmpegWebP ¶ added in v0.37.0
type FFmpegWebP struct {
Runner *ffmpeg.Runner
// MaxBytes arms the size watchdog of the run; 0 leaves it off.
MaxBytes int64
}
FFmpegWebP is the WebPEncoder of the product: FFmpeg with libwebp, a separate process started through the argument grammar (ADR-0003, SDD-007 section 6). The picture goes in as a PNG file of a run directory and the WebP comes out as a file of the same directory, which is removed afterwards.
func (FFmpegWebP) EncodeWebP ¶ added in v0.37.0
EncodeWebP encodes the PNG as a WebP. Quality 0 is lossless; 1 to 100 is lossy. A build of FFmpeg with no libwebp encoder answers E_EXPORT_FAILED with reason webp_unavailable.
type Format ¶
type Format string
Format is what an export writes (SDD-004 section 8, RF-064). The zero value is MP4.
const ( FormatMP4 Format = "mp4" // H.264 and AAC, the default FormatGIF Format = "gif" // composed frames, a fixed palette, written by the engine FormatWAV Format = "wav" // the 48 kHz stereo 16-bit mix FormatAAC Format = "aac" // the mix as AAC in an MP4 container, extension .m4a )
The formats of an export. Audio is a project's mix only: no picture is composed.
type GIFEstimate ¶
type GIFEstimate struct {
Width, Height int // the pixel size of the GIF (gifMaxWidth wide at most)
Frames int // the number of GIF frames, after the frame rate is lowered
TypicalBytes int64 // the size of ordinary footage
WorstBytes int64 // the size of the noisiest picture, which no export passes
}
GIFEstimate is what a GIF export of a project will be, known before it starts.
func EstimateGIF ¶
func EstimateGIF(st timeline.Settings, frames int) GIFEstimate
EstimateGIF is the size of the GIF of a project of frames timeline frames, from the size and the frame rate the export will use (gifSize and gifStep). It is an estimate to warn a person before a huge file is written (VV-056), not a promise: the real size depends on the picture.
type ImageCache ¶ added in v0.43.0
type ImageCache struct {
// contains filtered or unexported fields
}
ImageCache keeps pictures that RenderImage composed, so that the render of an edited document starts from the picture of the layers that did not change instead of drawing them again (SDD-021 section 6.1). A picture is keyed by the owner (CacheScope, a user in network mode) and by a digest of everything that made it: the canvas and the content of each plan layer from the bottom up. The cache is bounded by the bytes of its pictures and drops the least recently used first. It is safe for concurrent use; the pictures it holds are never handed out for writing.
func NewImageCache ¶ added in v0.43.0
func NewImageCache(limit int64) *ImageCache
NewImageCache makes a cache that holds at most limit bytes of pixels; a limit of zero or less is the default.
func (*ImageCache) Stats ¶ added in v0.43.0
func (c *ImageCache) Stats() ImageCacheStats
Stats reports the state of the cache.
type ImageCacheStats ¶ added in v0.43.0
ImageCacheStats is what a cache holds and how often a render found its start in it.
type ImageConfig ¶ added in v0.31.0
type ImageConfig struct {
// AssetPath gives the path of the normalized PNG of an image asset, by content hash.
AssetPath func(hash string) (string, error)
// LUTPath gives the path of the normalized .cube file of a lookup-table asset (SDD-021 section 3.1), by
// content hash. It is needed only by a document with a layer that has a table.
LUTPath func(hash string) (string, error)
// Fonts is the font set of the text layers; the bundled fonts when nil.
Fonts *rasterize.FontSet
// Cache, when set, lets the render start from the picture of the layers that an earlier render of the same
// document already drew (SDD-021 section 6.1); CacheScope is the owner of those pictures, a user in network
// mode, and a render never reads a picture of another scope. A custom Fonts set turns the cache off.
Cache *ImageCache
CacheScope string
}
ImageConfig is what the render of an image document needs from the machine (SDD-021 section 6).
type ImageEncodeOptions ¶ added in v0.31.0
type ImageEncodeOptions struct {
Format string
// Quality is 1 to 100 for JPEG (default 90) and for lossy WebP; 0 is the default (WebP: lossless).
Quality int
// Width and Height resample the picture to another size; 0 keeps it. The resampling is the integer
// sampler of the compositor, so it is the same on every machine.
Width, Height int
WebP WebPEncoder
}
ImageEncodeOptions say how a picture becomes a file.
type Job ¶
type Job struct {
Runner *ffmpeg.Runner
// Caps is ffmpeg's capability list, for the encoder detection.
Caps *ffmpeg.Capabilities
Doc *timeline.Document
// Nested are the other timelines of the project that the composition items of Doc use, directly or through
// each other, by id (SDD-004 section 3.16); nil for a document with no composition. The pictures and the
// sound of those items are made from them, and their assets are read like the assets of Doc.
Nested map[string]*timeline.Document
// JobID names the run directory under <workspace>/tmp.
JobID string
// ArtifactsDir is <workspace>/artifacts, where the normalized media of the assets is.
ArtifactsDir string
// ExportsDir and ProjectID make the destination: <ExportsDir>/<ProjectID>/<JobID>.mp4, from
// identifiers that are checked, written through os.Root (SDD-004 section 8 step 8). There is no
// way to name another destination. Nothing is written there unless the file passed its verification.
ExportsDir string
ProjectID string
// OutPath is the old way to name the destination, used only when ExportsDir is empty; the service
// moves to ExportsDir and ProjectID and OutPath is then removed.
OutPath string
// Format is what the export writes; the zero value is FormatMP4.
Format Format
// Codec is the video codec of an mp4 export; the zero value is the first choice of the preset that the
// machine offers, else H.264. A codec that is named and cannot be had is E_NO_ENCODER (SDD-004 section 8.3).
Codec exportcore.Codec
// Preset is the platform preset of the export (SDD-004 section 8.2): its bit rates and codecs are used and
// its limits are checked on the file that was written. Nil is no preset.
Preset *exportcore.Preset
// CodecPlan is the decision of PlanCodec when the caller has already made it (the service does, so that it
// can pick the runner of the encoder); nil means Run decides.
CodecPlan *CodecPlan
// MaxBytes is STUARTY_MAX_EXPORT_BYTES: a larger file ends the job with reason "size_limit".
// Zero is no limit.
MaxBytes int64
// Encoder is STUARTY_ENCODER and STUARTY_ALLOW_USER_X264 (ADR-0011 item 5).
Encoder ffmpeg.DetectOptions
// Start and End are the frames [Start, End) of the timeline to export. End 0 is the end of the
// last item; Start 0 and End 0 export the whole timeline (VV-018).
Start, End int
// Lint is the environment of the lint that gates the export (unready media, text sizes).
Lint lint.Env
// Fonts is the font set of the rasterizer; the bundled fonts when nil.
Fonts *rasterize.FontSet
// Bands is the number of bands of the compositor; zero is one per CPU, at most 8.
Bands int
// PixelRenderer is STUARTY_EXPORT_RENDERER: the Go compositor (the zero value) or the Rust renderer
// running as WebAssembly under wazero (SDD-013 section 4.6). Both draw the same bytes.
PixelRenderer PixelRenderer
// GPUHost is the absolute path of the native GPU host stuarty-render. With PixelRendererGPU and a path the
// frames are drawn by the child when the machine has a graphics adapter, and by the WebAssembly module
// otherwise (SDD-013 section 4.9); empty means no GPU host.
GPUHost string
// OnProgress is called at most 4 times per second and once at the last frame.
OnProgress func(Progress)
// contains filtered or unexported fields
}
Job is one export (SDD-004 section 8).
type Loudness ¶
type Loudness struct {
IntegratedLUFS *float64 `json:"integrated_lufs"` // before the gain; nil for a silent mix
TruePeakDBTP *float64 `json:"true_peak_dbtp"` // before the gain
GainDB float64 `json:"gain_db"`
TargetLUFS float64 `json:"target_lufs"`
// Limited is true when the gain took the mix over the true peak ceiling and the limiter held
// the peaks. OutputLUFS is then the loudness of the file after the limiter, and ShortfallDB how
// far it stays under the target (0 when it reached it).
Limited bool `json:"limited"`
OutputLUFS *float64 `json:"output_lufs,omitempty"`
ShortfallDB float64 `json:"shortfall_db"`
}
Loudness is what the mix measured and the gain that was applied to it (SDD-004 section 7).
type PixelRenderer ¶
type PixelRenderer string
PixelRenderer names what places the pixels of an exported frame (SDD-013 section 4.6, ADR-0029 item 8).
const ( PixelRendererGo PixelRenderer = "go" PixelRendererRust PixelRenderer = "rust" // PixelRendererGPU draws on the GPU in the child process stuarty-render when there is an adapter and a // GPU host, and as PixelRendererRust otherwise. The GPU frames are held to the CPU path by SSIM, not byte // for byte (SDD-013 section 3.8). PixelRendererGPU PixelRenderer = "gpu" )
The two renderers. The Go compositor stays the default until the byte-for-byte gate of stage C passes on Linux, macOS and Windows; the Rust renderer is the same frames drawn by the WebAssembly module.
func ParsePixelRenderer ¶
func ParsePixelRenderer(s string) (PixelRenderer, error)
ParsePixelRenderer reads STUARTY_EXPORT_RENDERER: empty is the default, the Go compositor.
type Progress ¶
type Progress struct {
Frame, Total int
}
Progress is the share of the frames written to the encoder.
type RenderConfig ¶
type RenderConfig struct {
Runner *ffmpeg.Runner
Doc *timeline.Document
// Nested are the timelines the compositions of Doc use, by id (Job.Nested).
Nested map[string]*timeline.Document
JobID string
ArtifactsDir string
Fonts *rasterize.FontSet // the bundled fonts when nil
Bands int
}
RenderConfig is what a Renderer needs: the same inputs as an export, without the output.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer composes single frames of a project through the same compositor and decoders as the export (SDD-004 section 9), for the paused frame of the editor and for the views of an agent. It owns a work directory and, while a frame is being made, FFmpeg processes: Close ends both. It is not safe for concurrent use.
func NewRenderer ¶
func NewRenderer(ctx context.Context, cfg RenderConfig) (*Renderer, error)
NewRenderer makes the renderer and its work directory. Each frame is independent: a decoder is not kept open for the next frame. ViewFrame and the paused frame use this.
func NewSequenceRenderer ¶
func NewSequenceRenderer(ctx context.Context, cfg RenderConfig) (*Renderer, error)
NewSequenceRenderer is NewRenderer for frames asked in order, which is how an export reads a clip. Decoders stay open and read ahead. A jump backwards, or of more than the restart gap, starts the process at the new frame. Close ends them. It is not safe for concurrent use, same as Renderer.
func (*Renderer) MixFrom ¶
MixFrom mixes the timeline audio from frame from, for n frames, and hands the result to sink in blocks of stereo float32 at 48 kHz (engine/audio.Mix). The sources are the same ones an export mixes. A range with no sound still calls sink with silence, so a player can keep its clock. n must be at least 1.
func (*Renderer) Render ¶
Render composes frame t at outScale times the project's size. The result is RGBA of round(width*outScale) by round(height*outScale) pixels. Uncovered pixels are opaque unless the plan is transparent, in which case their alpha is 0.
func (*Renderer) RenderSize ¶
RenderSize composes frame t into exactly w by h pixels, scaled by w over the project width, for a caller that owns the rounding of the height (the size table of SDD-003 section 3.11). The layers are placed at the width scale, so the picture differs from Render only by the rounding of the last row.
type Result ¶
type Result struct {
Path string
Format Format
Size int64
DurationMS int64
Frames int
Encoder string
// Codec is the video codec written ("h264", "hevc" or "av1"); empty for a format with no video codec.
Codec exportcore.Codec
// CodecWanted is the codec the preset preferred when the export fell back to another; empty otherwise.
CodecWanted exportcore.Codec
// Notes say in words what a person should know about the result, such as that fall back.
Notes []string
Loudness Loudness
// Width and Height are the pixels of the file when they differ from the project's (a GIF is
// capped at 480 pixels wide); zero means the project's size.
Width, Height int
}
Result is what a successful export produced.
type Verified ¶
type Verified struct {
DurationMS int64
Size int64
// Width and Height are the pixels of the file when they are not the project's (a GIF is
// capped, VV-056); zero means the project's size.
Width, Height int
}
Verified is what verification learned about a good file.
type WebPEncoder ¶ added in v0.31.0
type WebPEncoder interface {
EncodeWebP(ctx context.Context, png []byte, quality int) ([]byte, error)
}
WebPEncoder encodes a picture as WebP. It is a seam: the encoder is FFmpegWebP, FFmpeg with libwebp started through the argument grammar of engine/ffmpeg; with none given the WebP export answers E_EXPORT_FAILED with reason webp_unavailable.
Source Files
¶
- audio.go
- audio_preview.go
- audio_shaped.go
- capability.go
- chapters.go
- codec.go
- doc.go
- encode.go
- errors.go
- format.go
- format_audio.go
- format_gif.go
- framesource_gif.go
- image.go
- image_cache.go
- image_encode.go
- image_lut.go
- image_webp.go
- job.go
- lut.go
- measure.go
- media.go
- mp4.go
- paths.go
- render.go
- rust_renderer.go
- sequence.go
- verify.go