Skip to content

Effort free video editing!

8.8 KB
Raw Blame
import std/[math, strformat, strutils]
import ../[action, av, ffmpeg, log, timeline]
import ../util/[rational, speedmap]

# Remux subtitle packets from each clip's source onto the cut timeline,
# clipping each cue's visible window to its clip boundary. A cue that overlaps
# a cut-in point is kept (its start clamped to the cut) and one that runs past
# a cut-out is shortened, so subtitles stay in sync with the edited video
# instead of dropping or bleeding over. Works for text subs (SRT/ASS/WebVTT);
# bitmap subs (DVD/PGS) pass through with adjusted timing only.
proc remuxSubtitles*(layer: seq[Clip], effects: seq[Actions],
    outputStream: ptr AVStream, output: var OutputContainer,
    timelineTb: AVRational) =
  ## Packets can't be converted here, so a clip whose stream isn't the
  ## output's codec is left out, with a warning once per file.
  if layer.len == 0:
    return

  let outTb = outputStream.time_base
  # timelineTb is the frame rate, so one frame lasts 1/timelineTb seconds.
  let frameTb = av_inv_q(timelineTb)

  var srcContainer: InputContainer
  var openPath = ""
  defer:
    if openPath != "": srcContainer.close()
  var leftOut: seq[string]
  # An ASS packet's first field is its read order, by which libass drops
  # repeats; a cue kept on both sides of a cut, or two sources' cues, would
  # share one.
  let renumber = outputStream.codecpar.codec_id == ID_ASS
  var readOrder = 0

  for clipIdx, clip in layer:
    if clip.src == nil:
      continue
    let sourcePath = clip.src[]
    if sourcePath != openPath:
      if openPath != "": srcContainer.close()
      openPath = ""
      srcContainer = (try: av.open(sourcePath) except IOError as e: error e.msg)
      openPath = sourcePath
    if clip.stream >= srcContainer.subtitle.len:
      continue

    var streamIndex = srcContainer.subtitle[clip.stream].index
    var stream = srcContainer.formatContext.streams[streamIndex]
    if stream.codecpar.codec_id != outputStream.codecpar.codec_id:
      if sourcePath notin leftOut:
        leftOut.add sourcePath
        warning &"Subtitles of {sourcePath} are " &
          $avcodec_get_name(stream.codecpar.codec_id) & ", not " &
          $avcodec_get_name(outputStream.codecpar.codec_id) & "; left out"
      continue
    var srcTb = stream.time_base
    let srcStart = srcContainer.startPts(stream)
    # `offset` and `dur` count speed-scaled frames; the media they cover is
    # `mean` times longer.
    let map = initSpeedMap(effects[clip.effects])
    let ticksPerFrame = frameTb.float64 / srcTb.float64

    # The clip selects source interval [clipStartSrc, clipEndSrc) in source ticks.
    let clipStartSrc = srcStart +
      int64(round(clip.offset.float64 * map.mean * ticksPerFrame))
    let clipEndSrc = srcStart +
      int64(round((clip.offset + clip.dur).float64 * map.mean * ticksPerFrame))

    # Seek backward so a cue already on screen at the cut-in is read too.
    # A later clip needs the seek even at source position 0, since the
    # previous clip's loop advanced the demuxer.
    if clipIdx > 0 or clipStartSrc > 0:
      let ret = av_seek_frame(srcContainer.formatContext, stream.index,
        clipStartSrc, AVSEEK_FLAG_BACKWARD)
      if ret < 0:
        # Text sidecars such as SRT are commonly non-seekable. Reopening puts
        # the demuxer at the beginning; scanning forward below is slower but
        # preserves cues that overlap the clip boundary.
        srcContainer.close()
        openPath = ""
        srcContainer = (try: av.open(sourcePath) except IOError as e: error e.msg)
        openPath = sourcePath
        if clip.stream >= srcContainer.subtitle.len:
          continue
        streamIndex = srcContainer.subtitle[clip.stream].index
        stream = srcContainer.formatContext.streams[streamIndex]
        srcTb = stream.time_base

    # Cues are a sliver of the file: let the demuxer skip the video and audio.
    srcContainer.setActiveStream(streamIndex)

    var packet = av_packet_alloc()
    if packet == nil:
      error "Could not allocate subtitle packet"
    defer: av_packet_free(addr packet)

    while av_read_frame(srcContainer.formatContext, packet) >= 0:
      defer: av_packet_unref(packet)

      if packet.stream_index != streamIndex or packet.pts == AV_NOPTS_VALUE:
        continue

      let cueStart = packet.pts
      let hasDur = packet.duration > 0
      let cueEnd = (if hasDur: cueStart + packet.duration else: cueStart)

      # Packets arrive in pts order, so once a cue starts at/after the window, done.
      if cueStart >= clipEndSrc:
        break

      # Keep any cue overlapping the window. With unknown duration we can't tell if
      # an earlier-starting cue still overlaps, so fall back to start-inside.
      let overlaps = (if hasDur: cueEnd > clipStartSrc else: cueStart >= clipStartSrc)
      if not overlaps:
        continue

      # Intersect cue with the window, then map source -> timeline frames -> output.
      let visStart = max(cueStart, clipStartSrc)
      let startFrame = clip.clipFrameAt(map,
        float64(visStart - srcStart) / ticksPerFrame)
      let newPts = av_rescale_q(startFrame, frameTb, outTb)

      var outPacket: AVPacket
      var line = ""
      if renumber and packet.size > 0:
        line = newString(packet.size)
        copyMem(addr line[0], packet.data, packet.size)
      let comma = line.find(',')
      if comma >= 0:
        line = $readOrder & line[comma .. ^1]
        inc readOrder
        if av_new_packet(addr outPacket, line.len.cint) < 0:
          error "Could not allocate subtitle packet"
        copyMem(outPacket.data, addr line[0], line.len)
        outPacket.flags = packet.flags
      elif av_packet_ref(addr outPacket, packet) < 0:
        error "Failed to reference subtitle packet"
      outPacket.stream_index = outputStream.index
      outPacket.time_base = outTb
      outPacket.pts = newPts
      outPacket.dts = newPts # subtitle streams have no reordering; keep dts == pts
      if hasDur:
        let visEnd = min(cueEnd, clipEndSrc)
        let endFrame = clip.clipFrameAt(map, float64(visEnd - srcStart) / ticksPerFrame)
        outPacket.duration = max(1'i64, av_rescale_q(endFrame, frameTb, outTb) - newPts)

      output.mux(outPacket)
      av_packet_unref(addr outPacket)

proc writeCaptions*(track: CaptionTrack, ctx: ptr AVCodecContext,
    outputStream: ptr AVStream, output: var OutputContainer, timelineTb: AVRational) =
  ## Encode `track`'s cues as `outputStream`'s packets through `ctx`, an
  ## encoder from `addCaptionStream`.
  let frameTb = av_inv_q(timelineTb)
  var buf = newSeq[uint8](1 shl 16)
  for i, cue in track.cues:
    let pts = av_rescale_q(cue.start, frameTb, ctx.time_base)
    let dur = max(1'i64,
      av_rescale_q(cue.start + cue.dur, frameTb, ctx.time_base) - pts)

    # A dialogue line without its timing, which the packet carries. Its first
    # field is the read order, by which libass tells repeats apart.
    let line = $i & ",0,Default,,0,0,0,," &
      cue.text.replace("\r\n", "\n").replace("\n", "\\N")
    var rect: AVSubtitleRect
    rect.`type` = SUBTITLE_ASS
    rect.ass = line.cstring
    let rectPtr = addr rect
    var sub: AVSubtitle
    sub.format = 1 # text
    sub.end_display_time = uint32(dur) # ctx.time_base is milliseconds
    sub.num_rects = 1
    sub.rects = cast[ptr UncheckedArray[ptr AVSubtitleRect]](addr rectPtr)
    sub.pts = av_rescale_q(cue.start, frameTb, AVRational(num: 1, den: AV_TIME_BASE))

    let size = avcodec_encode_subtitle(ctx, addr buf[0], buf.len.cint, addr sub)
    if size < 0:
      error "Could not encode caption: " & av_err2str(size)
    if size != 0:
      var packet: AVPacket
      if av_new_packet(addr packet, size) < 0:
        error "Could not allocate caption packet"
      copyMem(packet.data, addr buf[0], size)
      packet.stream_index = outputStream.index
      packet.time_base = ctx.time_base
      packet.pts = pts
      packet.dts = pts
      packet.duration = dur
      output.mux(packet)
      av_packet_unref(addr packet)

proc makeAlteredSubtitle*(sourcePath, outputPath: string, layer: seq[Clip],
    effects: seq[Actions], timelineTb: AVRational) =
  ## Write every subtitle stream in a sidecar file, retimed to `layer`.
  let source = (try: av.open(sourcePath) except IOError as e: error e.msg)
  defer: source.close()

  var output = openWrite(outputPath)
  var outputStreams: seq[ptr AVStream]
  for sourceStream in source.subtitle:
    outputStreams.add output.addStreamFromTemplate(sourceStream)
  # The muxer may finalize stream time bases while writing its header.
  output.startEncoding()

  # The clips follow the media's cuts, but read the sidecar's streams.
  var sidecar = sourcePath
  for i, outputStream in outputStreams:
    var subtitleLayer = layer
    for clip in subtitleLayer.mitems:
      clip.src = addr sidecar
      clip.stream = i.int16
    remuxSubtitles(subtitleLayer, effects, outputStream, output, timelineTb)

  output.close()