@@ -117,7 +117,6 @@ Flags2 = define_enum("Flags2", __name__, (
117117
118118
119119cdef class CodecContext:
120-
121120 @staticmethod
122121 def create (codec , mode = None ):
123122 cdef Codec cy_codec = codec if isinstance (codec, Codec) else Codec(codec, mode)
@@ -130,7 +129,7 @@ cdef class CodecContext:
130129
131130 self .options = {}
132131 self .stream_index = - 1 # This is set by the container immediately.
133- self ._is_open = False
132+ self .is_open = False
134133
135134 cdef _init(self , lib.AVCodecContext * ptr, const lib.AVCodec * codec):
136135 self .ptr = ptr
@@ -217,10 +216,6 @@ cdef class CodecContext:
217216 def extradata_size (self ):
218217 return self .ptr.extradata_size
219218
220- @property
221- def is_open (self ):
222- return self ._is_open
223-
224219 @property
225220 def is_encoder (self ):
226221 if self .ptr is NULL :
@@ -234,27 +229,29 @@ cdef class CodecContext:
234229 return lib.av_codec_is_decoder(self .ptr.codec)
235230
236231 cpdef open (self , bint strict = True ):
237- if self ._is_open :
232+ if self .is_open :
238233 if strict:
239234 raise ValueError (" CodecContext is already open." )
240235 return
241236
242237 cdef _Dictionary options = Dictionary()
243238 options.update(self .options or {})
244239
245- # Assert we have a time_base for encoders.
246240 if not self .ptr.time_base.num and self .is_encoder:
247- self ._set_default_time_base()
241+ if self .type == " video" :
242+ self .ptr.time_base.num = self .ptr.framerate.den or 1
243+ self .ptr.time_base.den = self .ptr.framerate.num or lib.AV_TIME_BASE
244+ elif self .type == " audio" :
245+ self .ptr.time_base.num = 1
246+ self .ptr.time_base.den = self .ptr.sample_rate
247+ else :
248+ self .ptr.time_base.num = 1
249+ self .ptr.time_base.den = lib.AV_TIME_BASE
248250
249251 err_check(lib.avcodec_open2(self .ptr, self .codec.ptr, & options.ptr))
250- self ._is_open = True
252+ self .is_open = True
251253 self .options = dict (options)
252254
253- cdef _set_default_time_base(self ):
254- self .ptr.time_base.num = 1
255- self .ptr.time_base.den = lib.AV_TIME_BASE
256-
257-
258255 def __dealloc__ (self ):
259256 if self .ptr and self .extradata_set:
260257 lib.av_freep(& self .ptr.extradata)
@@ -564,7 +561,7 @@ cdef class CodecContext:
564561
565562 @thread_count.setter
566563 def thread_count (self , int value ):
567- if self ._is_open :
564+ if self .is_open :
568565 raise RuntimeError (" Cannot change thread_count after codec is open." )
569566 self .ptr.thread_count = value
570567
@@ -579,7 +576,7 @@ cdef class CodecContext:
579576
580577 @thread_type.setter
581578 def thread_type (self , value ):
582- if self ._is_open :
579+ if self .is_open :
583580 raise RuntimeError (" Cannot change thread_type after codec is open." )
584581 self .ptr.thread_type = ThreadType[value].value
585582
0 commit comments