Skip to content

Commit 96395cf

Browse files
committed
Fix video seeking issue
1 parent a3f6d6d commit 96395cf

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/ffmpeg.nim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ type
360360
time_base*: AVRational # time base of the packet
361361

362362
# https://ffmpeg.org/doxygen/7.0/structAVFrame.html
363-
AVFrame* {.importc: "AVFrame", header: "<libavutil/frame.h>",
364-
bycopy.} = object
363+
AVFrame* {.importc: "AVFrame", header: "<libavutil/frame.h>", bycopy.} = object
365364
data*: array[8, ptr uint8]
366365
linesize*: array[8, cint]
367366
extended_data*: ptr ptr uint8
@@ -371,23 +370,20 @@ type
371370
pict_type*: AVPictureType
372371
sample_aspect_ratio*: AVRational
373372
pts*: int64
374-
pkt_dts*: int64
375373
time_base*: AVRational
376374
quality*: cint
377375
opaque*: pointer
378376
repeat_pict*: cint
379377
sample_rate*: cint
378+
duration*: int64
380379
# buf*: array[8, ptr AVBufferRef]
381380
# extended_buf*: ptr ptr AVBufferRef
382381
nb_extended_buf*: cint
383-
# side_data*: ptr ptr AVFrameSideData
384-
# nb_side_data*: cint
385382
flags*: cint
386383
color_range*: AVColorRange
387384
color_primaries*: AVColorPrimaries
388385
color_trc*: AVColorTransferCharacteristic
389386
colorspace*: AVColorSpace
390-
best_effort_timestamp*: int64
391387
metadata*: ptr AVDictionary
392388
decode_error_flags*: cint
393389
crop_top*: csize_t

src/render/video.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs):
152152
153153
if src notin cns:
154154
cns[src] = av.open(src[])
155-
decoders[src] = initDecoder(cns[src].video[0].codecpar)
155+
156+
let decoderCtx = initDecoder(cns[src].video[0].codecpar)
157+
decoderCtx.thread_type = FF_THREAD_FRAME or FF_THREAD_SLICE
158+
decoders[src] = decoderCtx
156159

157160
var targetWidth: cint = cint(tl.res[0])
158161
var targetHeight: cint = cint(tl.res[1])
@@ -204,13 +207,14 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs):
204207

205208
for src, cn in cns:
206209
if len(cn.video) > 0:
210+
let stream = cn.video[0]
207211
if args.noSeek:
208212
seekCost[src] = int(high(uint32) - 1)
209213
tous[src] = 1000
210214
else:
211215
# Keyframes are usually spread out every 5 seconds or less.
212216
seekCost[src] = toInt(targetFps * AVRational(num: 5, den: 1))
213-
tous[src] = 1000
217+
tous[src] = int(stream.time_base.den div stream.avg_frame_rate)
214218

215219
if src == firstSrc and encoderCtx.pix_fmt != AV_PIX_FMT_NONE:
216220
pix_fmt = AVPixelFormat(cn.video[0].codecpar.format)
@@ -348,6 +352,7 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs):
348352

349353
frame.pts = index.int64
350354
frame.time_base = av_inv_q(tl.tb)
355+
frame.duration = index.int64
351356
yield (frame, index)
352357

353358
if scaleGraph != nil:

0 commit comments

Comments
 (0)