0

What I would like to do is this:

ffmpeg -i udp://224.10.10.10:15004 -qscale:v 2 sttest%04d.jpg

And then pass the output image paths of this to a python script, which individually takes in the images and manipulates them, as ffmpeg is running (eventually I will use ffmpy to call ffmpeg rather than using the command line tool, but for now testing purposes, I'm starting here).

I suppose a way to do this would be to look into the directory of the ffmpeg output every time I've processed an image to see if there is a new one, and then load that into python (using OpenCV)--and then waiting a certain time interval if there are no new images to process.

But is there a better way of doing this, like sending the paths of the images directly to some sort of queue which the python script can then process one by one?

2 Answers 2

1

As FFmpeg doesn't have an output that streams "file names I've generated", your only option is to use the file system you have.

Depending on your OS, there might be a way to get notified when a new file is touched in a directory. I don't know whether that works for linux, windows, os x, but it'd be worth looking into.

In fact, this looks simple enough that you wouldn't have to do it by calling ffmpeg externally. ffmpeg is basically a user frontend for libavcodec, a library made for de- and encoding video, audio etc.

So, you might simply want to use libavcodec inside your application. I'd actually think that what you're trying to do would be much easier if using an actual media streaming architecture. GStreamer would probably be your tool of choice, and you can directly work with it from python, but I've got no experience on how easy that is.

Your goal should probably be to get the frames directly in your python program, without the detour of writing them to disk.

A simple MJPEG (Motion JPEG) converter, simply piped into the stdin of your Program might be the simplest way, if you don't want to use either libav* or gstreamer.

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

Comments

0

For anyone curious, here was my solution. It was using OpenCV. I was unaware of this at the time that I asked the question, but OpenCV has certain features that use ffmpeg behind the scenes. And since my program was already using OpenCV, which I mentioned only in passing in the question (as I thought it was unimportant), it wasn't very difficult to use the features built in to OpenCV to do what I wanted. Here is the code:

cap = cv2.VideoCapture("video.mpg")
count = 0
frame_q = queue.Queue()

while cap.grab():
    success, frame = cap.read()

    if count % 5 == 0 and success:
            frame_alpha = np.insert(frame, 3, 255, axis=2)
            frame_q.put(frame_alpha)

    count += 1

As you can see, I am just putting every fifth frame into a Queue of frames. The core of this is these three lines:

cap = cv2.VideoCapture("video.mpg")
while cap.grab():
    success, frame = cap.read()

The first line opens the capture, the while clause checks if there are any more frames using cap.grab() (returning True if there is a next frame) and then I read that frame into a numpy array using cap.read(). success is a boolean that indicates whether the operation succeeded. Later I add an alpha channel to the frame and put it into the frame_q.

Then, another thread of the program gets frames out of that Queue. This way, I don't have to deal with the file system at all--like Marcus said, I'm getting them directly into the program.

Note: for cv2.VideoCapture() to work the opencv_ffmpeg.dll needs to be in the system path or in the root directory of whichever installation of Python you're using. You might have to rename it to opencv_ffmpegXYZ.dll where XYZ is the OpenCV version number minus the decimal points. So for me, it's opencv_ffmpeg330.dll because I have version 3.3.0. It also might be called opencv_ffmpeg330_64.dll on an x64 system.

The location of opencv_ffmpeg.dll depends on how you installed OpenCV, I would just search in the OpenCV directory for opencv_ffmpeg and see where it comes up. Before you copy it into the Python directory, make sure to check if it's already there. It might be, depending on how you installed OpenCV.

Comments

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.