0

I am trying to pipe output from FFmpeg in Python. I am reading images from a video grabber card and I am successful in reading this to an output file from the command line using dshow. I am trying to grab the images from the card to my OpenCv code to be able to further play with the data. Unfortunately, when I pipe out the images, I just get a display of the video as shown in the link:

link: s000.tinyupload.com/?file_id=15940665795196022618.

The code I used is as shown below:

import cv2
import subprocess as sp
import numpy
import sys
import os
old_stdout=sys.stdout
log_file=open("message.log","w")
sys.stdout=log_file

FFMPEG_BIN = "C:/ffmpeg/bin/ffmpeg.exe"
command = [ FFMPEG_BIN, '-y',
            '-f', 'dshow', '-rtbufsize', '100M',
            '-i', 'video=Datapath VisionAV Video 01' ,
             '-video_size', '640x480',
              '-pix_fmt', 'bgr24', '-r','25',  
          '-f', 'image2pipe', '-' ]    
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)

while True:
    # Capture frame-by-frame
    raw_image = pipe.stdout.read(640*480*3)
      # transform the byte read into a numpy array
    image =  numpy.frombuffer(raw_image, dtype='uint8')
    print(image)
    image = image.reshape((480,640,3))          
    if image is not None:
        cv2.imshow('Video', image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    pipe.stdout.flush()
sys.stdout=old_stdout
log_file.close()
cv2.destroyAllWindows()

Please do provide me some pointers to fix this issue. Help is greatly appreciated.

3
  • Piping input AND output of ffmpeg in python Commented Mar 5, 2019 at 11:02
  • @Muhammad Furqan adding -f image2pipe in the end before '-' in the ffmpeg command does not seem to give me the video still. Do you have any other suggestions? Commented Mar 5, 2019 at 12:40
  • Try to modify the comand as follow [ FFMPEG_BIN, '-y', '-f', 'dshow', '-rtbufsize', '100M', '-i', 'video=Datapath VisionAV Video 01' ,'-f', 'image2pipe', '-video_size', '640x480', '-pix_fmt', 'bgr24', '-r','25', '-' ] Commented Mar 5, 2019 at 16:56

4 Answers 4

1

Try:

command = [ FFMPEG_BIN,
            '-rtbufsize', '100M',
            '-i', '/dev/video0' , #change to here to your camera device
            '-video_size', '640x480',
            '-pix_fmt', 'bgr24',# '-r','25',
            '-f', 'image2pipe', #'-'
            '-vcodec', 'rawvideo', '-an', '-'
            ]

I don't know how '-vcodec', 'rawvideo', '-an', '-' this helps, and without it my code doesn't work.

Sign up to request clarification or add additional context in comments.

Comments

1

After you call the sp.Popen you have to communicate with it.You can use the following code:

try:    
    pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT, universal_newlines=True)`

    ffmpeg_output, _ = pipe.communicate()

except  sp.CalledProcessError as err:
     print("FFmpeg stdout output on error:\n" + err.output)

Finally, you can print the output to make sure the above commands worked:

print(ffmpeg_output)

The above statement is going to display the output returned by the communication with the process.

20 Comments

I tried this by adding these lines before the opencv read. But i get nothing printed on the command line. This program aims to get a continous stream from the video grabber card. I do not know if this is the reason if nothing seems to get printed on the command line.. Any ideas?
No, I edited my answer. I suggest you test the commands in a script apart so you make sure it's working.
this is what I got : <_io.TextIOWrapper name=3 encoding='cp1252'> @SenDjasni
My Bad, try the update, I test it on my terminal and it worked.
@user10890282 Also, put those command in a try: except: subprocess.CalledProcessError since the subprocess calls could generate exceptions.
|
0

I struggled longer with the console application FFmpeg, and finally gave up. It's easier with this extension:

pip install ffmpeg-python

Karl Kroening has published here a very good integration of FFmpeg into Python. With these examples a solution should be possible: https://github.com/kkroening/ffmpeg-python

1 Comment

It does not work with multiple INPUT named pipe
0

This works for me

import subprocess as sp
import json
import os
import numpy
import PIL
from imutils.video import FPS
import cv2 


def video_frames_ffmpeg():
    width = 640
    height = 360
    iterator = 0
    cmd = ['ffmpeg', '-loglevel', 'quiet',
           '-f', 'dshow',
           '-i', 'video=HD USB Camera',
           #'-vf','scale=%d:%d,smartblur'%(width,height),
           '-preset' ,'ultrafast', '-tune', 'zerolatency',
           '-f', 'rawvideo',
           '-pix_fmt','bgr24',
           '-']
    p = sp.Popen(cmd, stdout=sp.PIPE)

    while True:
        arr = numpy.frombuffer(p.stdout.read(width*height*3), dtype=numpy.uint8)
        iterator += 1 
        if len(arr) == 0:
            p.wait()
            print("awaiting")
            #return
        if iterator >= 1000:
            break

        frame = arr.reshape((height, width,3))
        cv2.putText(frame, "frame{}".format(iterator), (75, 70),
                cv2.cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)
        im = Image.fromarray(frame)
        im.save("ffmpeg_test/test%d.jpeg" % iterator)

        yield arr


from PIL import Image
from imutils.video import FPS
for i, frame in enumerate(video_frames_ffmpeg()):
    if i == 0:
        fps = FPS().start()
    else: fps.update()
fps.stop()
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()

2 Comments

while using "gray" instead of "bgr24" i get more fps
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.