I have a code that filter a video frame by frame using JavaCV. The code is like below
try (FFmpegFrameGrabber grabber = createGrabber()) {
grabber.start();
try (FFmpegFrameRecorder recorder = createRecorder(grabber)) {
recorder.start();
Frame frame;
while ((frame = grabber.grab()) != null) {
Frame editedFrame = filterFrame(frame); //This takes a long time.
recorder.setTimestamp(grabber.getTimestamp());
recorder.record(editedFrame);
}
}
}
Since the line Frame editedFrame = filterFrame(frame);
takes quite a long time, is it possible to utilize multithreading so the whole process can be much faster? I was thinking using something like ExecutorService
or LinkedBlockingQueue
to process several frames in one time and then record the frame back according to the timestamp.
It's pattern producer :
Producer class is for executing long task(filterFrame):
import java.util.concurrent.Callable;
public class Producer implements Callable<Frame> {
private final Frame frame;
public Producer(Frame frame) {
this.frame = frame;
}
@Override
public Frame call() throws Exception {
return filterFrame(frame);
}
}
Consumer class is for store frames:
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Consumer implements Runnable {
private final BlockingQueue<Map.Entry<Long, Future<Frame>>> queue;
public Boolean continueProducing = Boolean.TRUE;
private final FFmpegFrameRecorder recorder;
public Consumer(FFmpegFrameRecorder recorder,
BlockingQueue<Map.Entry<Long, Future<Frame>>> queue) {
this.recorder = recorder;
this.queue = queue;
}
@Override
public void run() {
try {
Map.Entry<Long, Future<Frame>> entry = this.queue.poll(2, TimeUnit.SECONDS);
Frame editedFrame = entry.getValue().get();
recorder.setTimestamp(entry.getKey());
recorder.record(editedFrame);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And the main part which manage tasks and shutdown threadPool
try (FFmpegFrameGrabber grabber = createGrabber()) {
grabber.start();
try (FFmpegFrameRecorder recorder = createRecorder(grabber)) {
recorder.start();
Frame frame;
ExecutorService threadPool = Executors.newWorkStealingPool();
BlockingQueue<Map.Entry<Long, Future<Frame>>> queue = new LinkedBlockingQueue<>();
threadPool.execute(new Consumer(recorder, queue));
while ((frame = grabber.grab()) != null) {
queue.put(new Map.Entry<Long, Future<Frame>>() {
@Override
public Long getKey() {
return grabber.getTimestamp();
}
@Override
public Future<Frame> getValue() {
return threadPool.submit(new Producer(frame)); // Frame editedFrame = filterFrame(frame); //This takes a long time.
}
@Override
public Future<Frame> setValue(Future<Frame> value) {
return null;
}
});
}
threadPool.shutdownNow();
}
}
Note: It's no copy-paste solution and need some customization for your code. If you share more information i'll change it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.