Skip to content

Effort free video editing!

6.9 KB
Raw Blame
## What an editor export can't carry from a timeline. The writers skip what
## their format has no place for; this names it so the loss isn't silent.

import std/[sequtils, strutils]
import ../[action, log, timeline]

type
  LayerRule = enum
    lrAll # every layer is written
    lrBase # only the base layer
    lrSameSource # the base, plus layers that only use the base's files: the
                 # editor reads those streams from the files it already has
    lrFirst # the first layer, when it only uses the base's files

  Support = object
    actions: set[ActionKind]
    v, a: LayerRule
    ramps: bool # speed ramps, which the others play at their mean speed

const mltActions = {actSpeed, actVolume, actDeesser, actInvert, actHflip,
  actVflip, actErosion, actBlur, actBrightness, actLuv, actLens, actDrawbox,
  actPixelate, actAberration}

func support(kind: ExportKind): Support {.raises: [].} =
  case kind
  of exPremiere:
    Support(actions: {actSpeed, actOpacity, actVolume}, v: lrAll, a: lrAll)
  of exResolveFcp7:
    Support(actions: {actSpeed, actOpacity, actVolume}, v: lrBase, a: lrAll)
  of exFinalCutPro:
    Support(actions: {actSpeed}, v: lrBase, a: lrSameSource, ramps: true)
  of exResolve: Support(actions: {actSpeed}, v: lrBase, a: lrAll, ramps: true)
  of exShotcut: Support(actions: mltActions, v: lrBase, a: lrFirst)
  of exKdenlive: Support(actions: mltActions, v: lrSameSource, a: lrSameSource)
  of exPremiereOtio:
    Support(actions: {actSpeed, actOpacity, actPos, actRotate, actSpin,
      actInvert, actHflip, actVflip}, v: lrAll, a: lrAll)
  of exEdl: Support(actions: {actSpeed}, v: lrBase, a: lrAll)
  else: Support(v: lrAll, a: lrAll)

func isEditorExport*(kind: ExportKind): bool {.raises: [].} =
  kind in {exPremiere, exResolveFcp7, exFinalCutPro, exResolve, exShotcut,
    exKdenlive, exPremiereOtio, exEdl}

func appliesTo(kind: ActionKind, video: bool): bool {.raises: [].} =
  # Every clip of a section shares its action group, so a video clip also
  # holds the section's audio actions. ActionKind is laid out by media:
  # speed for both, then audio, then video.
  if kind == actSpeed: true
  elif video: kind >= actInvert
  else: kind < actInvert

func plural(n: int, noun: string): string {.raises: [].} =
  if n == 1: "1 " & noun else: $n & " " & noun & "s"

type DropScan = object
  baseSrcs: seq[ptr string]
  videoBase: bool
  actions: seq[string]
  droppedV, droppedA, generated: int

func onlyBaseSrcs(s: DropScan, layer: seq[Clip]): bool {.raises: [].} =
  for clip in layer:
    if clip.src != nil and clip.src notin s.baseSrcs:
      return false
  true

func carried(s: DropScan, layer: seq[Clip], i: int, rule: LayerRule,
    isBase: bool): bool {.raises: [].} =
  if isBase: return true
  case rule
  of lrAll: true
  of lrBase: false
  of lrSameSource: s.onlyBaseSrcs(layer)
  of lrFirst: i == 0 and s.onlyBaseSrcs(layer)

proc scan(s: var DropScan, tl: v3, layers: seq[seq[Clip]], video: bool,
    rule: LayerRule, supported: set[ActionKind], ramps: bool) {.raises: [].} =
  for i, layer in layers:
    if layer.len != 0:
      let isBase = (if video: i == 0 else: i == 0 and not s.videoBase)
      # Generator layers (add:text, add:tone) have no file for an editor to
      # reference. A video base without files is different: it's the canvas
      # an audio-only timeline gets for its overlays, not content.
      if not (video and i == 0) and layer.allIt(it.src == nil):
        inc s.generated
      elif not s.carried(layer, i, rule, isBase):
        if video: inc s.droppedV else: inc s.droppedA
      else:
        for clip in layer:
          if clip.src != nil:
            for act in tl.effects[clip.effects]:
              if act.kind notin supported and act.kind.appliesTo(video):
                let name = ($act).split({':', ','})[0]
                if name notin s.actions: s.actions.add name
              elif act.kind == actSpeed and act.sKf.len > 0 and not ramps:
                const name = "speed ramp (as its mean speed)"
                if name notin s.actions: s.actions.add name

func shortensSpans(base: seq[Clip], markers: seq[Marker]): bool {.raises: [].} =
  ## Whether a marker span runs past the clip it's written on. FCPXML keeps
  ## a marker on the base layer's clip it starts during, as fcp11 does, so
  ## the span ends with that clip.
  for m in markers:
    if m.dur > 0:
      for i, clip in base:
        if clip.src != nil:
          var hi = high(int64)
          for j in i + 1 ..< base.len:
            if base[j].src != nil:
              hi = base[j].start
              break
          if m.start >= (if i == 0: 0'i64 else: clip.start) and m.start < hi:
            if m.dur > clip.start + clip.dur - m.start: return true
            break

func fcpxmlShortensSpans(tl: v3): bool {.raises: [].} =
  if tl.hasBaseVideo: tl.v[0].shortensSpans(tl.markers)
  elif tl.a.len > 0: tl.a[0].shortensSpans(tl.markers)
  else: false

proc exportDrops*(tl: v3, kind: ExportKind): seq[string] {.raises: [].} =
  ## What exporting `tl` as `kind` leaves out: action names, then counts of
  ## video, audio, generated, subtitle and caption tracks. Empty when nothing
  ## is lost.
  if not kind.isEditorExport:
    return
  let sup = support(kind)
  let videoBase = tl.hasBaseVideo

  var s = DropScan(videoBase: videoBase)
  for clip in (if videoBase: tl.v[0] elif tl.a.len > 0: tl.a[0] else: @[]):
    if clip.src != nil and clip.src notin s.baseSrcs:
      s.baseSrcs.add clip.src

  s.scan(tl, tl.v, true, sup.v, sup.actions, sup.ramps)
  s.scan(tl, tl.a, false, sup.a, sup.actions, sup.ramps)

  result = s.actions
  if s.droppedV > 0: result.add plural(s.droppedV, "video track")
  if s.droppedA > 0: result.add plural(s.droppedA, "audio track")
  if s.generated > 0: result.add plural(s.generated, "generated track")
  var subs = 0
  for layer in tl.s:
    if layer.len > 0: inc subs
  if subs > 0: result.add plural(subs, "subtitle track")
  # FCPXML is the one editor format with a place for captions.
  if kind notin {exFinalCutPro, exResolve}:
    var captions = 0
    for track in tl.c:
      if track.cues.len > 0: inc captions
    if captions > 0: result.add plural(captions, "caption track")
  # Every editor export carries markers, but not every part of one: OTIO
  # alone has a place for all of it.
  if tl.markers.len > 0:
    # Resolve reads no markers from FCPXML, though the file has them.
    if kind == exResolve:
      result.add plural(tl.markers.len, "marker")
    if kind in {exFinalCutPro, exResolveFcp7} and
        tl.markers.anyIt(it.color != mcBlue):
      result.add "marker colors"
    if kind in {exEdl, exShotcut, exKdenlive} and tl.markers.anyIt(it.note != ""):
      result.add "marker notes"
    if kind == exEdl and tl.markers.anyIt(it.dur > 0):
      result.add "marker spans"
    if kind == exFinalCutPro and tl.fcpxmlShortensSpans:
      result.add "marker spans (to their clip's end)"

proc warnDrops*(tl: v3, kind: ExportKind) {.raises: [].} =
  let drops = exportDrops(tl, kind)
  if drops.len > 0:
    warning "Left out of the export: " & drops.join(", ")