예제 #1
0
파일: h264.py 프로젝트: AlexVestin/cloneaio
    def decode(self, encoded_frame):
        try:
            packet = av.Packet(encoded_frame.data)
            packet.pts = encoded_frame.timestamp
            packet.time_base = VIDEO_TIME_BASE
            frames = self.codec.decode(packet)
        except av.AVError as e:
            logger.warning('failed to decode, skipping package: ' + str(e))
            return []

        return frames
예제 #2
0
파일: h264.py 프로젝트: opsantenna/aiortc
    def decode(self, encoded_frame: JitterFrame) -> List[Frame]:
        try:
            packet = av.Packet(encoded_frame.data)
            packet.pts = encoded_frame.timestamp
            packet.time_base = VIDEO_TIME_BASE
            frames = self.codec.decode(packet)
        except av.AVError as e:
            logger.warning(
                "H264Decoder() failed to decode, skipping package: " + str(e))
            return []

        return frames
예제 #3
0
파일: h264.py 프로젝트: ai-mocap/aiortc
    def decode(self, encoded_frame: JitterFrame) -> List[Frame]:
        try:
            packet = av.Packet(encoded_frame.data)
            packet.pts = encoded_frame.timestamp
            packet.time_base = VIDEO_TIME_BASE
            frames = self.codec.decode(packet)
        except av.AVError as e:
            logger.warning("failed to decode, skipping package: " + str(e))
            return []

        return [
            VideoFrameExt(
                frame,
                ntp_timestamp=encoded_frame.ntp_timestamp,
                encoded_frame=encoded_frame,
            ) for frame in frames
        ]
예제 #4
0
    def write(self, message):
        """Write a message to the file, assuming it was opened for writing or appending."""
        #        if message.type == Message.VIDEO:
        #            self.videostarted = True
        #        elif not hasattr(self, "videostarted"): return
        if message.type == Message.AUDIO or message.type == Message.VIDEO:
            length, ts = message.size, message.time
            ts_antigo = ts
            # if self._debug: print 'FLV.write()', message.type, ts
            if self.tsr0 is None:
                self.tsr0 = ts - self.tsr1
            self.tsr, ts = ts, ts - self.tsr0
            print(length, ts, ts_antigo, self.tsr0, self.tsr, self.tsr1)
            # if message.type == Message.AUDIO: print 'w', message.type, ts
            data = (struct.pack(
                ">BBHBHB",
                message.type,
                (length >> 16) & 0xFF,
                length & 0x0FFFF,
                (ts >> 16) & 0xFF,
                ts & 0x0FFFF,
                (ts >> 24) & 0xFF,
            ) + b"\x00\x00\x00" + message.data)
            data += struct.pack(">I", len(data))
            if message.type == Message.VIDEO:
                # print("data", data)
                packet = av.Packet(message.data)
                byte = packet.to_bytes()
                # print(byte, len(byte))
                # print(packet)
                packet.pts = packet.pts
                packet.stream = self.stream
                packet.pts = ts
                # import fractions

                # packet.time_base = fractions.Fraction(1, 1000)
                packet.dts = ts
                packet.time_base = fractions.Fraction(1, 1000)

                # for frame in packet.decode():
                #     print(frame)
                print(packet)
                self.maldicao.mux(packet)
            self.fp.write(data)
예제 #5
0
    def decode(self, encoded_frame: JitterFrame) -> List[Frame]:
        try:
            packet = av.Packet(encoded_frame.data)
            packet.pts = encoded_frame.timestamp
            packet.time_base = VIDEO_TIME_BASE
            frames = self.codec.decode(packet)
            if hasattr(encoded_frame, 'extensions'):
                t = frames
                frames = []
                for v in t:
                    frames.append({
                        'frame': v,
                        'extensions': encoded_frame.extensions
                    })
        except av.AVError as e:
            logger.warning("failed to decode, skipping package: " + str(e))
            return []

        return frames
예제 #6
0
    def _decodeGOP(self, n, frames, c):
        key_pts = self.index[n, 0]
        index = self.frameIndexFromPts(key_pts)

        if n < len(self.index) - 1:
            next_key_pts = self.index[n + 1, 0]
            next_key_index = self.frameIndexFromPts(next_key_pts)
            last_pts = self.pts[next_key_index - 1]

        else:
            last_pts = None

        packets = self.iterPackets(key_pts)
        iterpts = iter(self.pts[index:])

        decoder = av.CodecContext.create(self.codec, "r")

        try:
            if self.extradata:
                decoder.extradata = self.extradata

            for packet in packets:
                if len(packet.data) == 0:
                    continue

                avpacket = av.Packet(packet.data)
                avpacket.pts = packet.pts
                avpacket.time_base = self.time_base

                for frame, pts in zip(decoder.decode(avpacket), iterpts):
                    if self.type == "audio":
                        pts = int(
                            int((pts - self.pts[0]) / self.defaultDuration +
                                0.5) * self.defaultDuration +
                            0.5) + self.pts[0]

                    frame.pts = pts

                    if self.type == "video":
                        frame.pict_type = 0

                    with c:
                        if frame.format.name == "nv12":
                            frame = frame.reformat(format="yuv420p")

                        frames.append((frame.to_ndarray(), frame.format.name,
                                       frame.pict_type.name, frame.pts))
                        c.notifyAll()

                    if last_pts is not None and pts >= last_pts:
                        return

            for frame, pts in zip(decoder.decode(), iterpts):
                if self.type == "audio":
                    pts = int(
                        int((pts - self.pts[0]) / self.defaultDuration + 0.5) *
                        self.defaultDuration + 0.5) + self.pts[0]

                frame.pts = pts
                frame.time_base = self.time_base

                if self.type == "video":
                    frame.pict_type = 0

                with c:
                    if frame.format.name == "nv12":
                        frame = frame.reformat(format="yuv420p")

                    frames.append((frame.to_ndarray(), frame.format.name,
                                   frame.pict_type.name, frame.pts))
                    c.notifyAll()

                if last_pts is not None and pts >= last_pts:
                    return

        except Exception as exc:
            frames.append(exc)
            c.notifyAll()

        finally:
            decoder.extradata = b""
            decoder.close()

            with c:
                if n in self._gop_read_conditions:
                    del self._gop_read_conditions[n]

                c.notifyAll()
예제 #7
0
    def _iterAudioFrames(self, start=0, end=None, whence="pts"):
        if whence == "framenumber":
            startindex = start
            startpts = self.pts[start]
            endindex = end and min(end, len(self.pts))

            try:
                endpts = end and self.pts[endindex]

            except IndexError:
                endpts = None

        elif whence == "pts":
            if start >= self.pts[0]:
                startindex = self.frameIndexFromPts(
                    start, "-" if self.type is "audio" else "+")

            else:
                startindex = 0

            startpts = self.pts[startindex]

            try:
                endindex = end and self.frameIndexFromPts(end)
                endpts = end and self.pts[endindex]

            except IndexError:
                endindex = None
                endpts = None

        elif whence == "seconds":
            if start / self.time_base + 0.01 >= self.pts[0]:
                startindex = self.frameIndexFromPts(
                    start / self.time_base + 0.01,
                    "-" if self.type is "audio" else "+")

            else:
                startindex = 0

            startpts = start / self.time_base

            try:
                endindex = end and self.frameIndexFromPts(
                    end / self.time_base + 0.01)
                endpts = end and self.pts[endindex]

            except IndexError:
                endindex = None
                endpts = None

        if isinstance(self.index, numpy.ndarray) and self.index.ndim == 2 \
                and self.index.size and startpts >= self.index[0, 0]:
            key_index = self.keyIndexFromPts(startpts)

        else:
            key_index = 0

        key_pts = self.index[key_index, 0]
        index = self.frameIndexFromPts(key_pts)

        packets = self.iterPackets(key_pts)

        if hasattr(self, "defaultDuration") and self.defaultDuration:
            packets = WorkaheadIterator(
                packets,
                int(10 / self.defaultDuration / self.time_base) + 1)

        else:
            packets = WorkaheadIterator(packets)

        decoder = av.CodecContext.create(self.codec, "r")

        iterpts1 = iter(self.pts[index:])
        iterpts2 = iter(self.pts[index + 1:])

        try:
            if self.extradata:
                decoder.extradata = self.extradata

            framesdelivered = 0

            for packet in packets:
                if len(packet.data) == 0:
                    continue

                avpacket = av.Packet(packet.data)
                avpacket.pts = packet.pts
                avpacket.time_base = self.time_base

                for frame, pts1, pts2 in zip(decoder.decode(avpacket),
                                             iterpts1, iterpts2):
                    if self.type == "audio":
                        pts1 = int(
                            int((pts1 - self.pts[0]) / self.defaultDuration +
                                0.5) * self.defaultDuration +
                            0.5) + self.pts[0]
                        pts2 = int(
                            int((pts2 - self.pts[0]) / self.defaultDuration +
                                0.5) * self.defaultDuration +
                            0.5) + self.pts[0]

                    if pts2 <= startpts:
                        continue

                    if endpts is not None and pts1 >= endpts:
                        raise StopIteration

                    framesdelivered += 1
                    frame.pts = pts1
                    frame.time_base = self.time_base

                    yield frame

            for frame, pts in zip(decoder.decode(), iterpts1):
                if pts < startpts:
                    continue

                if endpts is not None and pts >= endpts:
                    raise StopIteration

                framesdelivered += 1
                frame.pts = pts
                frame.time_base = self.time_base
                yield frame

        finally:
            decoder.close()
예제 #8
0
        size = 0

        for packet in input_container.demux(video_stream):
            packet: av.Packet

            size += packet.buffer_size
            import fractions

            # show_all_object_props(packet)
            # print("size", size)
            # print(packet.to_bytes())
            # print(packet)
            # packet.update(b"maldicao")

            pacote = av.Packet(packet.to_bytes())
            # pacote.stream_index = 0

            pacote.pts = packet.pts
            pacote.stream = stream
            # pacote.time_base = fractions.Fraction(1, 1000)
            pacote.dts = packet.dts
            pacote.time_base = fractions.Fraction(1, 1000)
            # pacote.time_base = packet.time_base

            # pacote.duration = packet.duration
            # pacote.update(b"1" * 100)
            print("pacote", pacote)

            if pacote.dts is None:
                print("pacote inutil")