Effort free video editing!
import std/strformat
import ./[ffmpeg, log]
import ./util/rational
type Graph* = ref object
# nodes[0] is the buffer/abuffer source; nodes[^1] is the buffer/abuffer sink.
nodes: seq[ptr AVFilterContext]
graph: ptr AVFilterGraph
recvFrame: ptr AVFrame
configured: bool = false
proc newGraph*(): Graph =
result = Graph()
result.graph = avfilter_graph_alloc()
if result.graph == nil:
error "Could not allocate filter graph"
proc cleanup*(graph: Graph) =
if graph == nil:
return
if graph.recvFrame != nil:
av_frame_free(addr graph.recvFrame)
if graph.graph != nil:
avfilter_graph_free(addr graph.graph)
graph.nodes.setLen(0)
proc add*(graph: Graph, name: string, filterArgs: string = ""): ptr AVFilterContext =
if graph.configured:
error "Cannot add filters after graph is configured"
var filterCtx: ptr AVFilterContext = nil
let args = if filterArgs.len > 0: filterArgs.cstring else: nil
let filterName = &"filter_{graph.nodes.len}"
let filter = avfilter_get_by_name(name.cstring)
if filter == nil:
error &"Filter '{name}' not found"
let ret = avfilter_graph_create_filter(
addr filterCtx, filter, filterName.cstring, args, nil, graph.graph
)
if ret < 0:
error &"Cannot create filter '{name}' with args '{filterArgs}': {ret}"
if filterCtx == nil:
error &"Filter context is nil for '{name}'"
graph.nodes.add(filterCtx)
return filterCtx
proc linkNodes*(graph: Graph, nodes: varargs[ptr AVFilterContext]): Graph =
if graph.configured:
error "Cannot link nodes after graph is configured"
if nodes.len < 2:
error "Need at least 2 nodes to link"
# Link nodes sequentially: nodes[0] -> nodes[1] -> nodes[2] -> ...
for i in 0 ..< (nodes.len - 1):
let ret = avfilter_link(nodes[i], 0, nodes[i + 1], 0)
if ret < 0:
error &"Could not link node {i} to node {i + 1}: {ret}"
return graph
proc link*(graph: Graph; src, dst: ptr AVFilterContext; srcPad: cuint = 0;
dstPad: cuint = 0): Graph {.discardable.} =
## Link a single output pad of `src` to an input pad of `dst`. Unlike
## linkNodes, this allows multi-input filters (e.g. overlay's pad 0/1).
if graph.configured:
error "Cannot link nodes after graph is configured"
let ret = avfilter_link(src, srcPad, dst, dstPad)
if ret < 0:
error &"Could not link filter pads ({srcPad} -> {dstPad}): {ret}"
return graph
proc pushIdx*(graph: Graph; idx: int; frame: ptr AVFrame) =
## Push a frame into the buffer source at node index `idx` (for graphs with
## more than one source, like overlay).
if not graph.configured:
error "Graph must be configured before pushing frames"
if frame == nil:
error "Frame shouldn't be nil here"
let ret = av_buffersrc_write_frame(graph.nodes[idx], frame)
if ret < 0:
error &"Error pushing frame to graph source {idx}: {ret}"
proc command*(graph: Graph, target, cmd, arg: string) =
## Set a runtime option on the filters named or typed `target`.
let ret = avfilter_graph_send_command(graph.graph, target.cstring, cmd.cstring,
arg.cstring, nil, 0, 0)
if ret < 0:
error &"Could not send '{cmd}' to filter '{target}': {ret}"
proc move*(graph: Graph; frame: ptr AVFrame; idx = 0) =
## Hand `frame`'s picture to the buffer source at node index `idx`, leaving
## `frame` empty for the caller to free. `push` gives the graph a second
## reference, which makes a filter that writes in place copy the picture
## first; this gives it the only one.
if not graph.configured:
error "Graph must be configured before pushing frames"
if frame == nil:
error "Frame shouldn't be nil here"
let ret = av_buffersrc_add_frame(graph.nodes[idx], frame)
if ret < 0:
error &"Error moving frame to graph source {idx}: {ret}"
proc configure*(graph: Graph) =
if graph.configured:
return
if graph.nodes.len < 2:
error "Graph needs at least a source and a sink before configure"
let ret = avfilter_graph_config(graph.graph, nil)
if ret < 0:
error &"Could not configure filter graph: {ret}"
graph.configured = true
proc push*(graph: Graph, frame: ptr AVFrame) =
if not graph.configured:
error "Graph must be configured before pushing frames"
if frame == nil:
error "Frame shouldn't be nil here"
let ret = av_buffersrc_write_frame(graph.nodes[0], frame)
if ret < 0:
error &"Error pushing frame to graph: {ret}"
proc pull*(graph: Graph): ptr AVFrame =
# Caller responsible for freeing frames
if not graph.configured:
error "Graph must be configured before pulling frames"
var frame = av_frame_alloc()
if frame == nil:
error "Could not allocate frame for pulling"
let ret = av_buffersink_get_frame(graph.nodes[^1], frame)
if ret < 0:
av_frame_free(addr frame)
error &"Error pulling frame from graph: {ret}"
return frame
proc pullTransient*(graph: Graph): ptr AVFrame =
# Returns a Graph-owned frame valid until the next pullTransient/cleanup.
# Caller must NOT free; data lifetime is tied to the Graph.
# Returns nil on EAGAIN/EOF.
if not graph.configured:
error "Graph must be configured before pulling frames"
if graph.recvFrame == nil:
graph.recvFrame = av_frame_alloc()
if graph.recvFrame == nil:
error "Could not allocate frame for pulling"
else:
av_frame_unref(graph.recvFrame)
let ret = av_buffersink_get_frame(graph.nodes[^1], graph.recvFrame)
if ret == AVERROR_EAGAIN or ret == AVERROR_EOF:
return nil
if ret < 0:
# A mid-stream filter error is not "done"; treating it as EOF would
# silently truncate the stream being drained.
error &"Error pulling frame from filter graph: {av_err2str(ret)}"
return graph.recvFrame
proc tryPull*(graph: Graph, dst: ptr AVFrame): bool =
## Move the sink's next frame into `dst`; false while a filter that needs
## more input holds it back, or once the stream has ended.
av_frame_unref(dst)
let ret = av_buffersink_get_frame(graph.nodes[^1], dst)
if ret == AVERROR_EAGAIN or ret == AVERROR_EOF:
return false
if ret < 0:
error &"Error pulling frame from filter graph: {av_err2str(ret)}"
true
proc sinkTimeBase*(graph: Graph): AVRational =
av_buffersink_get_time_base(graph.nodes[^1])
proc flush*(graph: Graph) =
# Flush the filter graph by sending a nil frame
if not graph.configured:
error "Graph must be configured before flushing"
let ret = av_buffersrc_write_frame(graph.nodes[0], nil)
if ret < 0:
error &"Error flushing graph: {ret}"
proc flushIdx*(graph: Graph; idx: int) =
## Signal end-of-stream on the buffer source at node index `idx`. Needed for
## framesync filters (e.g. overlay) so they emit once every input has a frame.
if not graph.configured:
error "Graph must be configured before flushing"
let ret = av_buffersrc_write_frame(graph.nodes[idx], nil)
if ret < 0:
error &"Error flushing graph source {idx}: {ret}"