Created
June 3, 2022 04:38
-
-
Save ufxpri/a88214589723d1d5aa12d27f7b379db8 to your computer and use it in GitHub Desktop.
play klv stream with ffmpeg and klvdata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ffmpeg # https://github.com/kkroening/ffmpeg-python # pip3 install ffmpeg-python | |
| import klvdata # https://github.com/paretech/klvdata # pip3 install klvdata | |
| import cv2 | |
| import numpy as np | |
| stream_path = "klv_stream_sample.ts" | |
| probe = ffmpeg.probe(stream_path) | |
| video_streams = [stream for stream in probe['streams'] if stream['codec_type'] == "video"] | |
| klv_streams = [stream for stream in probe['streams'] if stream['codec_type'] == "data" and stream['codec_name'] == "klv"] | |
| stream_input = ffmpeg.input(stream_path) | |
| # video stream | |
| frame_width = video_streams[0]['width'] | |
| frame_height = video_streams[0]['height'] | |
| video_process = ffmpeg.output(stream_input['v'], 'pipe:', fflags='nobuffer', format='rawvideo', pix_fmt='bgr24') | |
| video_process = video_process.run_async(pipe_stdout=True) | |
| # klv stream | |
| klv_process = ffmpeg.output(stream_input['d'], 'pipe:', codec='copy', f='data') | |
| klv_process = klv_process.run_async(pipe_stdout=True) | |
| klv_parser = klvdata.StreamParser(klv_process.stdout) | |
| while True: | |
| # get video frame | |
| in_bytes = video_process.stdout.read(frame_width*frame_height*3) | |
| in_frame = np.frombuffer(in_bytes, dtype=np.uint8).reshape((frame_height, frame_width, 3)) | |
| # get klv data | |
| metadata = next(klv_parser).MetadataList() | |
| # show data | |
| print(metadata) | |
| cv2.imshow("in_frame", in_frame) | |
| if cv2.waitKey(1)==ord('q'): | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment