Skip to content

Decoding raw H264 frame from subprocess stdout #578

@bfabricio

Description

@bfabricio

Overview

I have been working to get PyAV to decode raw h264 output from adb(android debug bridge) + screenrecord open as a python subprocess. This code is meant to be used to stream android screen and it's is based on the fallowing ffmpeg command:

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

The fallowing python code was used. The main idea is to read Popen stdout and provide to pyAV decode the h264 frame.

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

Actual behavior

Traceback:

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

Investigation

I got this code working using c++ and libavcodec, where the main idea is to decode raw h264 output from adb subprocess and save each frame as ppg image.
gist.github.com/fabriciopk/6f583f079bd929ccc708aa3d3cb37b59

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions