我有一个使用JavaCV逐帧过滤视频的代码。代码如下所示
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);
}
}
}
由于行Frame editedFrame = filterFrame(frame);
需要相当长的时间,是否可以利用多线程,因此整个过程可以更快?我正在考虑使用ExecutorService
或LinkedBlockingQueue
之类的东西一次处理几个帧,然后根据时间戳记录回来。
答案 0 :(得分:0)
Producer类用于执行长任务(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);
}
}
消费者类用于商店框架:
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();
}
}
}
管理任务和关闭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();
}
}
注意:这不是复制粘贴解决方案,需要为您的代码进行一些自定义。如果你分享更多信息,我会改变它