Decoding raw H264 frame from subprocess stdout

1.1k Views Asked by At

I'm trying to extract frames from raw h264 stream from a subprocess using python. This code is meant to be used to stream android screen and it's is based on the fallowing command

adb exec-out screenrecord --output-format h264 --size 640x310 - | ffmpeg -i - -f sdl -

The python code can be seen below:

import av
import subprocess as sp
import logging


adbCmd = ['adb', 'exec-out', 'screenrecord', '--output-format=h264', '-']
stream = sp.Popen(adbCmd, stdout = sp.PIPE, universal_newlines = True, errors='replace')

class H264Decoder:
    def __init__(self):
        self.codec = av.CodecContext.create("h264", "r")

    def decode(self, encoded_frame):
        try:
            packet = av.Packet(encoded_frame)
            # 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


while True:
  line = stream.stdout.readline(4096)
  decoder = H264Decoder()
  decoder.decode(u' '.join(line).encode('utf-8').strip())
  if not line:
    break

The output is the fallowing

No start code is found.
Error splitting the input into NAL units.
failed to decode, skipping package: [Errno 1094995529] Invalid data found when processing input (16: h264)
line
0

There are 0 best solutions below