def mux_packet(self, packet: av.Packet) -> None: """Mux a packet to the appropriate output stream.""" # Check for end of segment if packet.stream == self._input_video_stream: if ( packet.is_keyframe and (packet.dts - self._segment_start_dts) * packet.time_base >= MIN_SEGMENT_DURATION ): # Flush segment (also flushes the stub part segment) self.flush(packet, last_part=True) # Reinitialize self.reset(packet.dts) # Mux the packet packet.stream = self._output_video_stream self._av_output.mux(packet) self.check_flush_part(packet) self._part_has_keyframe |= packet.is_keyframe elif packet.stream == self._input_audio_stream: packet.stream = self._output_audio_stream self._av_output.mux(packet)
def image_sequence_encode(self, codec_name): try: codec = Codec(codec_name, "w") except UnknownCodecError: raise SkipTest() container = av.open(fate_suite("h264/interlaced_crop.mp4")) video_stream = container.streams.video[0] width = 640 height = 480 ctx = codec.create() pix_fmt = ctx.codec.video_formats[0].name ctx.width = width ctx.height = height ctx.time_base = video_stream.codec_context.time_base ctx.pix_fmt = pix_fmt ctx.open() frame_count = 1 path_list = [] for frame in iter_frames(container, video_stream): new_frame = frame.reformat(width, height, pix_fmt) new_packets = ctx.encode(new_frame) self.assertEqual(len(new_packets), 1) new_packet = new_packets[0] path = self.sandboxed("%s/encoder.%04d.%s" % ( codec_name, frame_count, codec_name if codec_name != "mjpeg" else "jpg", )) path_list.append(path) with open(path, "wb") as f: f.write(new_packet) frame_count += 1 if frame_count > 5: break ctx = av.Codec(codec_name, "r").create() for path in path_list: with open(path, "rb") as f: size = os.fstat(f.fileno()).st_size packet = Packet(size) size = f.readinto(packet) frame = ctx.decode(packet)[0] self.assertEqual(frame.width, width) self.assertEqual(frame.height, height) self.assertEqual(frame.format.name, pix_fmt)
def __try_add_stream(self, sample_packet: av.Packet) -> bool: if len(self.__output.streams.video) == 0: if sample_packet.stream_index == 1: self.__output.add_stream('aac') sample_frames = sample_packet.decode() if len(sample_frames) == 0: return False sample_frame = [x for x in sample_frames if isinstance(x, av.VideoFrame)][0] self.__stream = self.__output.add_stream('libx264', '30') self.__stream.pix_fmt = sample_frame.format.name self.__stream.width = sample_frame.width self.__stream.height = sample_frame.height return True return True
def iter_raw_frames(path, packet_sizes, ctx): with open(path, "rb") as f: for i, size in enumerate(packet_sizes): packet = Packet(size) read_size = f.readinto(packet) assert size assert read_size == size if not read_size: break for frame in ctx.decode(packet): yield frame while True: try: frames = ctx.decode(None) except EOFError: break for frame in frames: yield frame if not frames: break
def iter_raw_frames(path, packet_sizes, ctx): with open(path, 'rb') as f: for i, size in enumerate(packet_sizes): packet = Packet(size) read_size = f.readinto(packet) assert size assert read_size == size if not read_size: break for frame in ctx.decode(packet): yield frame while True: try: frames = ctx.decode(None) except AVError as e: if e.errno != 541478725: # EOF raise break for frame in frames: yield frame if not frames: break