def decodeBegin(self): # A dummy packet that is used to pass the output buffer to avcodec_decode_audio3 self._dummyPkt = decls.AVPacket() avcodec.av_init_packet(self._dummyPkt) # Allocate a buffer for the decoded audio frame size = 192000 self._sampleBuf = (size*ctypes.c_short)() self._audioData = AudioData(self, channels=self._codecCtx.channels, framerate=self._codecCtx.sample_rate, samples=self._sampleBuf) return True
def iterData(self, streams=None): """Iterate over the frames of the first video stream. """ # Use the first video stream by default... if streams is None: if len(self.videoStreams)==0: return videoStream = self.videoStreams[0] streams = [videoStream] if len(streams)==0: return # Initialize the streams... # streamDict: Key:Stream index - Value:Stream object streamDict = {} for stream in streams: # Check the type of the stream if not isinstance(stream, StreamBase_ffmpeg): raise TypeError("Stream object expected") # Is this a stream from someone else? if stream not in self._streams: raise ValueError("Invalid stream (stream is from a different source)") stream.decodeBegin() streamDict[stream.index] = stream try: # Iterate over the packets in the file and decode the frames... for pkt in self.iterPackets(): stream = streamDict.get(pkt.stream_index, None) if stream is not None: for data in stream.handlePacket(pkt): yield data # Some codecs have a delay between input and output, so go on # feeding empty packets until we receive no more frames. pkt = decls.AVPacket() avcodec.av_init_packet(pkt) for stream in streamDict.values(): hasData = True while hasData: hasData = False for data in stream.handlePacket(pkt): hasData = True yield data finally: for stream in streamDict.values(): stream.decodeEnd()
def writeFrame__dummy(self): # Create a packet struct and initialize it pkt = AVPacket() avcodec.av_init_packet(pkt) #pkt.stream_index = outputstream.index #big_picture.pts= ost->sync_opts; # Encode the frame bytes = avcodec.avcodec_encode_video(codecCtx, buf, bufsize, picture) if bytes>0: pkt.data = buf pkt.size = bytes # pkt.pts = ... if codecCtx.coded_frame.key_frame: pkt.flags |= PKT_FLAG_KEY avformat.av_interleaved_write_frame(formatCtx, pkt)