ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

Multiple Ffmpeg stream into a single subprocess pipeline

Open seabass1217 opened this issue 2 years ago • 1 comments

Hi,

Thanks for the packages! I just discovered it while doing research on an ffmpeg issue I have.

I need to process both video stream and the metadata within OpenCV Python. I'm passing the video and data to OpenCV with the subprocess module.

My problem is I cannot figure out how to map both the video and metadata to the same subprocess pipe. Although my code is still in native ffmpeg, any help on creating a ffmpeg-python pipeline to accomplish this goal is greatly appreciated.

Currently my code:

#!/usr/bin/env python3
import sys, json, klvdata;
from subprocess import PIPE
import subprocess as sp
import cv2
import numpy
import ffmpeg

command = ['ffmpeg',
    '-i', 'DayFlight.mpg',
    '-map', '0:0',
    '-map', '0:d',        
    '-pix_fmt', 'bgr24',
    '-c:v', 'rawvideo',      
    '-an','-sn',              
    '-f', 'image2pipe', '-',
    '-c:d', 'copy',
    '-f','data',
    ]

pipe = sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, bufsize=10**8)

while True:
   raw_image = pipe.stdout.read(1280*720*3)
   image =  numpy.fromstring(raw_image, dtype='uint8')
   image = image.reshape((720,1280,3))          
   if image is not None:
      cv2.imshow('Video', image)
   if cv2.waitKey(1) & 0xFF == ord('q'):
      break
   for packet in klvdata.StreamParser(pipe.stdout): 
      metadata = packet.MetadataList()
      print(metadata)
pipe.stdout.flush()
cv2.destroyAllWindows()

produces this error:

Traceback (most recent call last):
  File "test_cv.py", line 37, in <module>
    metadata = packet.MetadataList()
AttributeError: 'UnknownElement' object has no attribute 'MetadataList'

Thanks in advance.

seabass1217 avatar Mar 14 '22 08:03 seabass1217

I know this is old, but I need to do something similar. Did you solve it?

dlang123 avatar Nov 09 '23 21:11 dlang123