Python 从子进程标准输出解码原始H264帧

Python 从子进程标准输出解码原始H264帧,python,ffmpeg,adb,h.264,pyav,Python,Ffmpeg,Adb,H.264,Pyav,我正在尝试使用python从子流程的原始h264流中提取帧。此代码旨在用于流式传输android屏幕,它基于fallowing命令 adb exec-out screenrecord --output-format h264 --size 640x310 - | ffmpeg -i - -f sdl - python代码如下所示: import av import subprocess as sp import logging adbCmd = ['adb', 'exec-out', 's

我正在尝试使用python从子流程的原始h264流中提取帧。此代码旨在用于流式传输android屏幕,它基于fallowing命令

adb exec-out screenrecord --output-format h264 --size 640x310 - | ffmpeg -i - -f sdl -
python代码如下所示:

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
输出是衰减

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

我成功地使用了c++和libavcodec:我也很想知道这个答案!