0

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

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
4

0 回答 0