Documentation
¶
Overview ¶
Package render makes videos of native UI: a composition is a view of package ui that draws each frame of a video from the frame's number, and Video draws its frames on the GPU, or on the CPU where there is none, and encodes them with the system's own encoders, in hardware where it has them, into an MP4 or a QuickTime movie, with sound:
comp := render.Composition{
Width: 1920, Height: 1080, FPS: 30, Frames: 90,
View: func(c *ui.Context, f render.Frame) {
ui.Text(c, "Hello").FontSize(96).
Opacity(float32(f.Interpolate([]float64{0, 20}, []float64{0, 1})))
},
}
err := render.Video(ctx, comp, "hello.mp4", render.Options{})
Frames are built, laid out and painted by package ui, as a window's are, then drawn by its renderers: Metal on macOS, Direct3D 11 on Windows, OpenGL on Linux (through EGL, without a display), or the CPU renderer, which draws the same pixels, where the GPU's cannot run. No browser is involved: a frame costs what a frame of a window costs.
Images writes the frames as PNG or JPEG files, Still one frame, and Frames hands each to a function. Nothing opens a window, and none of it needs mygo.App.
Index ¶
- func Frames(ctx context.Context, comp Composition, o Options, ...) error
- func Images(ctx context.Context, comp Composition, pattern string, o Options) error
- func Interpolate(x float64, in, out []float64, o ...Interpolation) float64
- func InterpolateColor(x float64, in []float64, out []ui.Color, o ...Interpolation) ui.Color
- func Layer(c *ui.Context) ui.Element
- func NewPlayer(comp Composition) (*ui.VideoPlayer, error)
- func Still(comp Composition, frame int, o Options) (*image.RGBA, error)
- func Video(ctx context.Context, comp Composition, path string, o Options) error
- type AudioOptions
- type Codec
- type Composition
- type Encoder
- type Extrapolate
- type Frame
- func (f Frame) Audio(s *Sound, o ...AudioOptions)
- func (f Frame) Exact() float64
- func (f Frame) Frames(d time.Duration) int
- func (f Frame) Interpolate(in, out []float64, o ...Interpolation) float64
- func (f Frame) Loop(count int) Frame
- func (f Frame) Progress() float64
- func (f Frame) Seconds() float64
- func (f Frame) Sequence(c *ui.Context, from, count int, view func(f Frame))
- func (f Frame) Shift(from, count int) (Frame, bool)
- func (f Frame) Spring(o ui.SpringOptions) float64
- func (f Frame) SpringIn(o ui.SpringOptions, frames int) float64
- func (f Frame) Time() time.Duration
- type Interpolation
- type MotionBlur
- type Options
- type Progress
- type Renderer
- type Sound
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Frames ¶
func Frames(ctx context.Context, comp Composition, o Options, fn func(frame int, img *image.RGBA) error) error
Frames draws the frames of the composition Options choose and calls fn with each, in order, on the goroutine rendering: its pixels, with premultiplied alpha, are fn's to keep. An error from fn stops the render, and Frames returns it.
func Images ¶
Images renders the composition's frames into image files named by pattern, a path with a verb of fmt for the frame's number, as "frames/%04d.png": PNGs, with the alpha of a Transparent composition, or JPEGs (.jpg, .jpeg) of Options.Quality. The directory is made if need be. Files are encoded on as many cores as there are, while the next frames draw.
func Interpolate ¶
func Interpolate(x float64, in, out []float64, o ...Interpolation) float64
Interpolate maps x from the increasing values in to those of out, of the same length, two at least: between in[i] and in[i+1] it goes from out[i] to out[i+1] along o's Easing. A view fades in over its first half second with
opacity := f.Interpolate([]float64{0, 15}, []float64{0, 1})
func InterpolateColor ¶
InterpolateColor maps x from in to the colors out as Interpolate maps it to numbers, mixing them in sRGB, outside in's range clamped.
func Layer ¶
Layer returns a box filling its parent, laid out apart from its siblings, over those before it: for what overlaps, as the layers of a video.
func NewPlayer ¶
func NewPlayer(comp Composition) (*ui.VideoPlayer, error)
NewPlayer returns a player of the composition, for ui.Video to show in a window, as a preview of the video while it is made: its frames are the video's, built at their times and drawn on the CPU on a goroutine of the player's, a few ahead as they come due, and a seek builds the frames before the one it goes to without drawing them. Play, Pause, Seek and SetLoop control it as any video's; it plays no sound.
player, err := render.NewPlayer(comp) ... ui.Video(c, player).FillWidth()
func Still ¶
Still draws frame of the composition, after building the frames before it, and returns its pixels.
func Video ¶
Video renders the composition into a video file at path: an MP4 (.mp4, .m4v), a QuickTime movie (.mov), or a WebM (.webm), with the sound its frames play (Frame.Audio) in AAC, or Opus in WebM. The file appears when the video is whole: until then the frames go into a file beside it, which an error or ctx's end removes.
Types ¶
type AudioOptions ¶
type AudioOptions struct {
// Volume scales the sound, 1 when zero; it may change from frame to
// frame, as a fade, and moves smoothly across each.
Volume float64
// Start is where in the sound it starts playing, cutting what comes
// before.
Start time.Duration
// Loop plays the sound again from Start each time it ends, rather
// than fall silent.
Loop bool
}
AudioOptions are how Frame.Audio plays a sound.
type Codec ¶
type Codec uint8
Codec is how Video compresses the frames.
const ( // DefaultCodec is H264, or VP9 for WebM. DefaultCodec Codec = iota // H264 is H.264 (AVC), which every player plays: in MP4 or QuickTime. H264 // HEVC is H.265, about as good at two thirds of the bitrate, which // newer systems play: in MP4 or QuickTime. Windows encodes it with the // HEVC Video Extensions. HEVC // ProRes is Apple ProRes 422 HQ, for editing: in QuickTime, on macOS, // or with ffmpeg. ProRes // ProRes4444 is Apple ProRes 4444, with the alpha of a Transparent // composition: in QuickTime, on macOS, or with ffmpeg. ProRes4444 // VP9 is VP9 in WebM, with the alpha of a Transparent composition // with ffmpeg: on Linux with GStreamer's vpx plugin, or with ffmpeg. VP9 )
type Composition ¶
type Composition struct {
// Width and Height are the size of the video in pixels, even for
// H.264, HEVC and VP9, whose chroma has half the size.
Width, Height int
// FPS is how many frames a second the video shows, 30 when zero.
// Rates of NTSC, as 29.97, are 30000/1001 to encoders.
FPS float64
// Frames is the length of the video in frames.
Frames int
// Scale is how many pixels a DIP is, 1 when zero: the view lays out
// in Width/Scale × Height/Scale DIPs, as a window at that scale, so
// that a 1920×1080 video at scale 2 is laid out as a 960×540 window
// of a Retina display.
Scale float32
// View builds the frame f. It runs on several goroutines at once, one
// a worker (Options.Workers), each building every frame: it must
// build the same frame the same way each time, and read what it
// shares, never change it.
View func(c *ui.Context, f Frame)
// Background fills the frames behind the view; zero is the theme's
// background. Transparent leaves what the view does not paint
// transparent instead, for the codecs with alpha (ProRes4444) and
// PNGs; other codecs show it black.
Background ui.Color
Transparent bool
// Dark builds the view in the dark appearance, Preferences with the
// desktop's settings that controls follow, and Locale in a language
// (Context.Locale), "" for the system's default.
Dark bool
Preferences ui.Preferences
Locale string
// MotionBlur blurs what moves, as a camera's shutter does, when its
// Samples are more than 1.
MotionBlur MotionBlur
}
A Composition is a video: its size, its frames, and the view that builds each of them.
type Encoder ¶
type Encoder uint8
Encoder chooses what encodes a Video.
const ( // System encodes with the system's media framework, in hardware // where it can: AVFoundation (VideoToolbox) on macOS, Media // Foundation on Windows, GStreamer on Linux. System Encoder = iota // FFmpeg pipes the frames to ffmpeg (Options.FFmpeg), for what the // system has no encoder of, as WebM on macOS and Windows, or a Linux // without GStreamer's encoders. FFmpeg )
type Extrapolate ¶
type Extrapolate uint8
Extrapolate says what Interpolate returns outside the range of its input.
const ( // Clamp keeps the value at the end of the output range. Clamp Extrapolate = iota // Extend goes on as the nearest segment goes. Extend // Identity returns the input. Identity )
type Frame ¶
type Frame struct {
// Index is the frame's, from 0, and Count the frames of the
// composition or sequence.
Index, Count int
// FPS is the frames a second.
FPS float64
// contains filtered or unexported fields
}
A Frame is the frame of a composition a view builds: its Index, counted from 0 at the start of the composition, or of the sequence showing the view (Sequence, Shift), of Count frames at FPS frames a second.
What a view shows follows from the frame alone, as a function of Index: Interpolate and Spring turn it into positions, opacities and colors, and Sequence shows a part of the video during some of its frames. ui's own animations (Animate, Spring, Transition) move with the video's clock too, but from what the frames before showed.
func (Frame) Audio ¶
func (f Frame) Audio(s *Sound, o ...AudioOptions)
Audio plays s during the frame, from where it is at this frame of the composition or the sequence playing it: the sound starts at its frame 0, so that a sound played inside a Sequence starts with the sequence. A view plays a sound in each frame it is heard, as it shows an element in each frame it is seen:
f.Audio(music, render.AudioOptions{Volume: f.Interpolate([]float64{0, 30}, []float64{0, 1})})
Sounds played in the frames of Images, Frames and Still are not written anywhere.
func (Frame) Exact ¶
Exact returns where the frame is, in frames: Index, or a fraction before it while motion blur samples the time the shutter is open before the frame (Composition.MotionBlur). What moves with Exact, Seconds, Time, Interpolate and Spring blurs; what moves with Index, a whole number, does not.
func (Frame) Interpolate ¶
func (f Frame) Interpolate(in, out []float64, o ...Interpolation) float64
Interpolate maps f.Index from the frames in to the values out, as the package's Interpolate does.
func (Frame) Loop ¶
Loop returns the frame as a sequence of count frames playing again and again sees it: Index is f.Index modulo count.
func (Frame) Progress ¶
Progress returns how far into its Count frames the frame is: 0 for the first, 1 for the last.
func (Frame) Sequence ¶
Sequence shows view during count frames from from, given the frame as the sequence sees it (Shift), in a layer that fills the parent, above the layers before it, as video editors stack clips. A count of 0 or less lasts to the end of f's. The layer stays in the layout while the sequence does not show, empty, so that the elements after it keep their identities.
func (Frame) Shift ¶
Shift returns the frame as a sequence of count frames starting at from sees it, and whether the sequence shows then: Index is f.Index-from, within 0 and count. A count of 0 or less lasts to the end of f's.
func (Frame) Spring ¶
func (f Frame) Spring(o ui.SpringOptions) float64
Spring returns where a spring of o going from 0 to 1, starting at the first frame, is at this one: 0 at the start, past 1 where it overshoots, 1 once it rests (ui.SpringOptions.Progress). ui.Smooth, ui.Snappy and ui.Bouncy have the springs of ui's animations.
type Interpolation ¶
type Interpolation struct {
Easing ui.Easing
Left, Right Extrapolate
}
Interpolation describes how Interpolate goes from value to value: along Easing, linear when nil, between each two, and Left and Right before and after the input's range, Clamp by default.
type MotionBlur ¶
MotionBlur is a camera's motion blur, as Remotion's CameraMotionBlur makes it: each frame is the mean of Samples frames built at times spread over the time the shutter is open before it, which Shutter gives in degrees of the frame's time, 180 (half of it) when zero, up to 360. Each sample is built and drawn, so a frame costs Samples times as much; 5 to 10 samples blur smoothly.
The samples' times are fractions of a frame before it: what moves with time blurs, the view's Interpolate, Spring, Exact and Seconds, and ui's own animations, which follow the clock; what moves with the whole Frame.Index does not.
type Options ¶
type Options struct {
// From and To are the frames rendered: those from From to before To,
// all when To is zero. The frames before From are built, not drawn,
// so that what ui animates from frame to frame is where it would be.
From, To int
// Renderer chooses what draws the frames.
Renderer Renderer
// Workers is how many frames are drawn at once, each by a worker of
// its own on a thread of its own: 1 draws them one after another, and
// zero chooses: one for a video without motion blur, which goes as
// fast as its encoder takes frames, else half the cores, up to 4. See
// Composition.View for what a view does with more than one.
Workers int
// Progress is called after each frame written, on the goroutine
// rendering.
Progress func(Progress)
// Codec encodes the video, by default H264, or VP9 into WebM.
// Bitrate is its bits a second, chosen for the size and rate when
// zero; AudioBitrate the sound's, 192 kb/s when zero. ProRes takes
// neither.
Codec Codec
Bitrate int
AudioBitrate int
// Encoder is what encodes Video's frames: the system's media
// framework, or ffmpeg (FFmpeg), for formats the system has no
// encoder of.
Encoder Encoder
// FFmpeg is the path of ffmpeg for the FFmpeg Encoder, "ffmpeg" from
// PATH when empty.
FFmpeg string
// Quality is that of the JPEGs of Images, from 1 to 100, 90 when
// zero.
Quality int
// ConstantFrameRate has Video encode every frame. Otherwise a frame
// that shows what the frame before showed is neither drawn nor
// encoded: the one before lasts longer, as one sample of the video, so
// a video that holds still is made and played at little cost. Some
// video editors take constant frame rates alone.
ConstantFrameRate bool
}
Options are how Video, Images, Frames and Still make a composition's frames.
type Progress ¶
type Progress struct {
// Frame is the frame written last, Done how many were written, of
// Frames.
Frame, Done, Frames int
// Elapsed is the time since the render started.
Elapsed time.Duration
// Renderer names what draws the frames, as "Metal (Apple M2)" or
// "CPU".
Renderer string
}
Progress tells how a render goes.
type Sound ¶
type Sound struct {
// contains filtered or unexported fields
}
A Sound is audio a composition plays (Frame.Audio), decoded into memory once, so that any part of it plays at any frame. WAV decodes out of the box; import audio/mp3, audio/vorbis, audio/flac or audio/aac for theirs, as package audio's formats register.
func DecodeSound ¶
DecodeSound decodes audio in memory, as a file embedded in the program.