コード例 #1
0
ファイル: test_codec_context.py プロジェクト: PyAV-Org/PyAV
    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)
コード例 #2
0
ファイル: base.py プロジェクト: shersonb/python-transcode
    def descriptor(self):
        if not hasattr(self, "_descriptor"):
            try:
                codecobj = av.Codec(self.codec, "w")

            except Exception:
                return

            self._descriptor = codecobj.descriptor

        return self._descriptor
コード例 #3
0
    def display(self, index, obj):
        if isinstance(obj, BaseReader):
            return obj.fmtname

        elif isinstance(obj, Track):
            codec = obj.codec

            try:
                codec_long = av.Codec(codec, "r").long_name

            except Exception:
                codec_long = "Unknown"

            return f"{codec_long} ({codec})"
コード例 #4
0
    def tooltip(self, index, obj):
        if isinstance(obj, BaseReader):
            mod = obj.__class__.__module__

            if mod.split(".")[:2] == ["transcode", "containers"]:
                mod = ".".join(mod.split(".")[2:])

            return f"{obj.fmtname}\n"\
                f"[{mod}.{obj.__class__.__name__}]"

        elif isinstance(obj, Track):
            codec = obj.codec

            try:
                codec_long = av.Codec(codec, "r").long_name

            except Exception:
                codec_long = "Unknown"

            return f"{codec_long} ({codec})"
コード例 #5
0
ファイル: test_codec_context.py プロジェクト: PyAV-Org/PyAV
    def video_encoding(self, codec_name, options={}, codec_tag=None):

        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]

        pix_fmt = options.pop("pix_fmt", "yuv420p")
        width = options.pop("width", 640)
        height = options.pop("height", 480)
        max_frames = options.pop("max_frames", 50)
        time_base = options.pop("time_base",
                                video_stream.codec_context.time_base)

        ctx = codec.create()
        ctx.width = width
        ctx.height = height
        ctx.time_base = time_base
        ctx.framerate = 1 / ctx.time_base
        ctx.pix_fmt = pix_fmt
        ctx.options = options  # TODO
        if codec_tag:
            ctx.codec_tag = codec_tag
        ctx.open()

        path = self.sandboxed("encoder.%s" % codec_name)
        packet_sizes = []
        frame_count = 0

        with open(path, "wb") as f:

            for frame in iter_frames(container, video_stream):

                new_frame = frame.reformat(width, height, pix_fmt)

                # reset the picture type
                new_frame.pict_type = PictureType.NONE

                for packet in ctx.encode(new_frame):
                    packet_sizes.append(packet.size)
                    f.write(packet)

                frame_count += 1
                if frame_count >= max_frames:
                    break

            for packet in ctx.encode(None):
                packet_sizes.append(packet.size)
                f.write(packet)

        dec_codec_name = codec_name
        if codec_name == "libx264":
            dec_codec_name = "h264"

        ctx = av.Codec(dec_codec_name, "r").create()
        ctx.open()

        decoded_frame_count = 0
        for frame in iter_raw_frames(path, packet_sizes, ctx):
            decoded_frame_count += 1
            self.assertEqual(frame.width, width)
            self.assertEqual(frame.height, height)
            self.assertEqual(frame.format.name, pix_fmt)

        self.assertEqual(frame_count, decoded_frame_count)
コード例 #6
0
ファイル: client.py プロジェクト: nukush/frc-usb-camera
                            self.sendPacket(res)
                        elif sdl2.SDL_GetKeyName(
                                event.key.keysym.sym).lower() == b'd':
                            res = ChangePacket(1)
                            print("Changed camera to 1")
                            self.sendPacket(res)
                frames.pop(packet.frame_idx)
                res = AckPacket(packet.frame_idx, interarrival_time)
                self.sendPacket(res)

        elif packet.type == PacketType.RESPONSE:
            self.previous_packet_time = get_us()

        elif packet.type == PacketType.FINISH:
            reactor.stop()


fn = datetime.now().isoformat().replace(":", "-")
f = open(f"videos/{fn}.264", "wb")
h264 = av.Codec("h264")
h264_decoder = av.CodecContext.create(h264)
sdl2.ext.init()
window = sdl2.ext.Window("frc-usb-camera", size=(1280, 720))
window.show()
window_surface = sdl2.SDL_GetWindowSurface(window.window)
window_array = sdl2.ext.pixels3d(window_surface.contents)
vc = VideoClient()
reactor.listenUDP(2601, vc)
reactor.addSystemEventTrigger('before', 'shutdown', vc.finishConnection)
reactor.run()
コード例 #7
0
    def video_encoding(self, codec_name, options={}):

        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]

        pix_fmt = options.pop('pix_fmt', 'yuv420p')
        width = options.pop('width', 640)
        height = options.pop('height', 480)
        max_frames = options.pop('max_frames', 50)
        time_base = options.pop('time_base',
                                video_stream.codec_context.time_base)

        ctx = codec.create()
        ctx.width = width
        ctx.height = height
        ctx.time_base = time_base
        ctx.framerate = 1 / ctx.time_base
        ctx.pix_fmt = pix_fmt
        ctx.options = options  # TODO
        ctx.open()

        path = self.sandboxed('encoder.%s' % codec_name)
        packet_sizes = []
        frame_count = 0

        with open(path, 'wb') as f:

            for frame in iter_frames(container, video_stream):
                """
                bad_frame = frame.reformat(width, 100, pix_fmt)
                with self.assertRaises(ValueError):
                    ctx.encode(bad_frame)

                bad_frame = frame.reformat(100, height, pix_fmt)
                with self.assertRaises(ValueError):
                    ctx.encode(bad_frame)

                bad_frame = frame.reformat(width, height, "rgb24")
                with self.assertRaises(ValueError):
                    ctx.encode(bad_frame)
                """

                if frame:
                    frame_count += 1

                new_frame = frame.reformat(width, height,
                                           pix_fmt) if frame else None
                for packet in ctx.encode(new_frame):
                    packet_sizes.append(packet.size)
                    f.write(packet)

                if frame_count >= max_frames:
                    break

            for packet in ctx.encode(None):
                packet_sizes.append(packet.size)
                f.write(packet)

        dec_codec_name = codec_name
        if codec_name == 'libx264':
            dec_codec_name = 'h264'

        ctx = av.Codec(dec_codec_name, 'r').create()
        ctx.open()

        decoded_frame_count = 0
        for frame in iter_raw_frames(path, packet_sizes, ctx):
            decoded_frame_count += 1
            self.assertEqual(frame.width, width)
            self.assertEqual(frame.height, height)
            self.assertEqual(frame.format.name, pix_fmt)

        self.assertEqual(frame_count, decoded_frame_count)
コード例 #8
0
    def __init__(self, codec_name):

        self._decoder = av.Codec(codec_name, 'r').create(hwaccel=hwaccel)