From c22a5ee8ad857c213844fd311df54f52f939de92 Mon Sep 17 00:00:00 2001 From: Mike Boers Date: Wed, 8 Jun 2016 21:05:48 -0400 Subject: [PATCH] Rescale packet times when re-assigning the stream --- av/packet.pyx | 13 +++++++++++++ include/libavutil/avutil.pxd | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/av/packet.pyx b/av/packet.pyx index 58d925393..70598f94b 100644 --- a/av/packet.pyx +++ b/av/packet.pyx @@ -1,3 +1,5 @@ +cimport libav as lib + cdef class Packet(object): @@ -47,11 +49,22 @@ cdef class Packet(object): def __get__(self): return self.stream def __set__(self, Stream value): + + # Rescale times. + cdef lib.AVStream *old = self.stream._stream + cdef lib.AVStream *new = value._stream + self.struct.pts = lib.av_rescale_q_rnd(self.struct.pts, old.time_base, new.time_base, + lib.AV_ROUND_NEAR_INF | lib.AV_ROUND_PASS_MINMAX) + self.struct.dts = lib.av_rescale_q_rnd(self.struct.dts, old.time_base, new.time_base, + lib.AV_ROUND_NEAR_INF | lib.AV_ROUND_PASS_MINMAX) + self.struct.duration = lib.av_rescale_q(self.struct.duration, old.time_base, new.time_base) + self.stream = value self.struct.stream_index = value.index property pts: def __get__(self): return None if self.struct.pts == lib.AV_NOPTS_VALUE else self.struct.pts + property dts: def __get__(self): return None if self.struct.dts == lib.AV_NOPTS_VALUE else self.struct.dts diff --git a/include/libavutil/avutil.pxd b/include/libavutil/avutil.pxd index 7be7993ef..42635a7d0 100644 --- a/include/libavutil/avutil.pxd +++ b/include/libavutil/avutil.pxd @@ -85,7 +85,14 @@ cdef extern from "libavutil/avutil.pyav.h" nogil: int64_t a, int64_t b, int64_t c, - AVRounding r + int r # should be AVRounding, but then we can't use bitwise logic. + ) + + cdef int64_t av_rescale_q_rnd( + int64_t a, + AVRational bq, + AVRational cq, + int r # should be AVRounding, but then we can't use bitwise logic. ) cdef int64_t av_rescale(