Effort free video editing!
Skip other streams when reading subtitles
WyattBluec7dd7015ce5f
diff --git a/src/analyze/subtitle.nim b/src/analyze/subtitle.nimindex f884b13a..1fdaba94 100644--- a/src/analyze/subtitle.nim+++ b/src/analyze/subtitle.nim@@ -26,6 +26,9 @@ proc subtitle*(container: InputContainer, tb: AVRational, pattern: Re,# failure: sidecar subtitle demuxers (e.g. srt) can't seek, but those# containers are freshly opened and already at the start.discard av_seek_frame(formatCtx, -1, 0, AVSEEK_FLAG_BACKWARD)+ # Cues are a sliver of the file: let the demuxer skip the video and audio.+ let levels = container.setActiveStream(s)+ defer: container.restoreDiscards(levels)var length = 0var spans: seq[(int, int)] = @[]diff --git a/src/av.nim b/src/av.nimindex 1275faa6..e7a5fff1 100644--- a/src/av.nim+++ b/src/av.nim@@ -381,18 +381,25 @@ proc scanVideoLength*(formatCtx: ptr AVFormatContext, video: ptr AVStream): AVRaden: video.avg_frame_rate.num)AVRational(num: 0, den: 1)-proc setActiveStream*(container: InputContainer, index: cint) =+proc setActiveStream*(container: InputContainer,+ index: cint): seq[AVDiscard] {.discardable.} =## Tell the demuxer to discard packets for every stream except `index`, so## av_read_frame skips demuxing streams we don't decode (e.g. video while- ## analyzing audio). Call resetDiscards() when finished to restore the- ## container for callers that read every stream.- for i in 0 ..< container.formatContext.nb_streams.int:+ ## analyzing audio). Returns the levels the streams had, for+ ## restoreDiscards() to put back when the container is read again.+ let n = container.formatContext.nb_streams.int+ result = newSeq[AVDiscard](n)+ for i in 0 ..< n:let s = container.formatContext.streams[i]+ result[i] = s.discardLevels.discardLevel = (if s.index == index: AVDISCARD_DEFAULT else: AVDISCARD_ALL)-proc resetDiscards*(container: InputContainer) =- for i in 0 ..< container.formatContext.nb_streams.int:- container.formatContext.streams[i].discardLevel = AVDISCARD_DEFAULT+proc restoreDiscards*(container: InputContainer, levels: seq[AVDiscard]) =+ ## A demuxer discards some streams by its own choice (mov, a data stream+ ## of zero-length samples), so the levels are put back, not cleared.+ # A demuxer can add streams as it reads (MPEG-TS); those keep their level.+ for i in 0 ..< min(levels.len, container.formatContext.nb_streams.int):+ container.formatContext.streams[i].discardLevel = levels[i]proc mediaLength*(container: InputContainer): AVRational =# Result is in seconds.@@ -403,8 +410,8 @@ proc mediaLength*(container: InputContainer): AVRational =let audio = container.audio[0]# Only the audio's packets count: let the demuxer skip the rest instead# of reading every video frame off the disk.- container.setActiveStream(audio.index)- defer: container.resetDiscards()+ let levels = container.setActiveStream(audio.index)+ defer: container.restoreDiscards(levels)let endPts = scanPackets(formatCtx, audio, 0).endPtsreturn (if endPts > 0: endPts - formatCtx.startPts(audio) else: 0) *audio.time_base@@ -433,6 +440,7 @@ type FrameReader* = objectcodecCtx: ptr AVCodecContextframe*: ptr AVFrameindex: cint+ levels: seq[AVDiscard]receiving, flushing: boolproc initFrameReader*(container: InputContainer, index: cint,@@ -440,12 +448,11 @@ proc initFrameReader*(container: InputContainer, index: cint,# We decode a single stream to EOF here, so let the demuxer skip every other# stream instead of reading packets we'd only throw away (big win when# analyzing audio in a high-bitrate video file).- container.setActiveStream(index)FrameReader(container: container, codecCtx: codecCtx, frame: frame,- index: index)+ index: index, levels: container.setActiveStream(index))proc close*(r: FrameReader) =- r.container.resetDiscards()+ r.container.restoreDiscards(r.levels)proc next*(r: var FrameReader): bool =## Decode the next frame into `r.frame`; false once the stream is drained.diff --git a/src/cmds/subdump.nim b/src/cmds/subdump.nimindex 4a300122..d2351b6b 100644--- a/src/cmds/subdump.nim+++ b/src/cmds/subdump.nim@@ -18,6 +18,9 @@ iterator subtitleEvents(container: InputContainer, stream: cint):var codecCtx = initDecoder(formatCtx.streams[stream].codecpar)defer: avcodec_free_context(addr codecCtx)var subtitle: AVSubtitle+ # Cues are a sliver of the file: let the demuxer skip the video and audio.+ let levels = container.setActiveStream(stream)+ defer: container.restoreDiscards(levels)while av_read_frame(formatCtx, container.packet) >= 0:defer: av_packet_unref(container.packet)let pkt = container.packetdiff --git a/src/render/burnsubs.nim b/src/render/burnsubs.nimindex fd91bdbc..8616adc6 100644--- a/src/render/burnsubs.nim+++ b/src/render/burnsubs.nim@@ -344,6 +344,8 @@ when defined(libass):dec.subtitle_header_size)let packet = container.packetvar seen: set[Script]+ # Cues are a sliver of the file: let the demuxer skip the video and audio.+ container.setActiveStream(st.index)while av_read_frame(fmt, packet) >= 0:defer: av_packet_unref(packet)if packet.stream_index != st.index: continuediff --git a/src/render/subtitle.nim b/src/render/subtitle.nimindex 68b494e9..9e21883c 100644--- a/src/render/subtitle.nim+++ b/src/render/subtitle.nim@@ -85,6 +85,9 @@ proc remuxSubtitles*(layer: seq[Clip], effects: seq[Actions],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"