Convert an h264 byte string to OpenCV images

后端 未结 2 405
既然无缘
既然无缘 2021-01-07 11:58

In Python, how do I convert an h264 byte string to images OpenCV can read, only keeping the latest image?

Long version:

Hi everyone.

Working in Pytho

相关标签:
2条回答
  • 2021-01-07 12:48

    Use any of these:

    ffmpeg -i - -pix_fmt bgr24 -f rawvideo -
    ffmpeg -i pipe: -pix_fmt bgr24 -f rawvideo pipe:
    ffmpeg -i pipe:0 -pix_fmt bgr24 -f rawvideo pipe:1
    
    • You didn't provide much info about your input so you may need to add additional input options.

    • You didn't specify your desired output format so I just chose rawvideo. You can see a list of supported output formats (muxers) with ffmpeg -muxers (or ffmpeg -formats if your ffmpeg is outdated). Not all are suitable for piping, such as MP4.

    • See FFmpeg Protocols: pipe.

    0 讨论(0)
  • 2021-01-07 12:54

    It works well just a minor changes: This will read the stream in a loop and show each time the last image

    adbCmd = ['adb', 'exec-out', 'screenrecord', '--output-format=h264', '-']
    stream = sp.Popen(adbCmd, stdout = sp.PIPE)
    
    ffmpegCmd =['ffmpeg', '-i', '-', '-f', 'rawvideo', '-vf', 'scale=324:576', 
    '-vcodec', 'bmp',  '-']
    ffmpeg = sp.Popen(ffmpegCmd, stdin = stream.stdout, stdout = sp.PIPE)
    
    while True:
        fileSizeBytes = ffmpeg.stdout.read(6)
        fileSize = 0
        for i in xrange(4):
            fileSize += array.array('B',fileSizeBytes[i + 2])[0] * 256 ** i
        bmpData = fileSizeBytes + ffmpeg.stdout.read(fileSize - 6)
        image = cv2.imdecode(np.fromstring(bmpData, dtype = np.uint8), 1)
        cv2.imshow("im",image) 
        cv2.waitKey(25)
    
    0 讨论(0)
提交回复
热议问题