matham / ffpyplayer

A cython implementation of an ffmpeg based player.
GNU Lesser General Public License v3.0
134 stars 38 forks source link

Inquiries on retrieving and decoding video frame information from computer memory #144

Closed 1997shp closed 1 year ago

1997shp commented 1 year ago

I am in the process of building a tool to play H264 video streams, and I am wondering how to retrieve and parse video frame information from memory. Specifically, I am dealing with a list of bytes.

import ffpyplayer 
video_data = b'\x00\x01\x02\x03\x04 ...' 
player = ffpyplayer.MediaPlayer(????) 

I hope to receive your assistance. Many thanks.

matham commented 1 year ago

You can try streaming the data to ffmpeg. You can read about the protocols available here. You can also the available protocols in your version of ffplay by running ffplay -protocols on the ffplay packaged with your ffpyplayer installations.

A very basic example is:

from threading import Thread
from socketserver import ThreadingTCPServer, BaseRequestHandler
from ffpyplayer.player import MediaPlayer
import time

with open(r'G:\Python\libs\ffpyplayer\examples\dw11222.mp4', 'rb') as fd:
    data = fd.read()

class MyTCPHandler(BaseRequestHandler):
    def handle(self):
        # self.request is the TCP socket connected to the client
        print("{} connected".format(self.client_address[0]))
        self.request.sendall(data)

with ThreadingTCPServer(('localhost', 9054), MyTCPHandler) as server:
    server_thread = Thread(target=server.serve_forever)
    server_thread.start()

    try:
        player = MediaPlayer(f"tcp://localhost:9054")

        val = ''
        frame = None
        while val != 'eof':
            frame, val = player.get_frame()

            if val != 'eof' and frame is not None:
                img, t = frame
                print(t, img)
            else:
                time.sleep(.01)
        print('finished')
    finally:
        server.shutdown()

The one issue here I think is that ffmpeg won't know when the file is done as as long as the server is running it could potentially stream more. So you may need to figure it out yourself.

You can also look into the data protocol if you have the data ahead of time. Or maybe save it to disk.

1997shp commented 1 year ago

Thank you very much for your reply and the examples you provided It gave me a great idea : ) 👍