public class VideoToFrames implements Runnable {
private static final String TAG = "VideoToFrames";
private static final boolean VERBOSE = false;
private static final long DEFAULT_TIMEOUT_US = 10000;
private static final int COLOR_FormatI420 = 1;
private static final int COLOR_FormatNV21 = 2;
private final int decodeColorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible;
private LinkedBlockingQueue<byte[]> mQueue;
private OutputImageFormat outputImageFormat;
private String OUTPUT_DIR;
private boolean stopDecode = false;
private String videoFilePath;
private Throwable throwable;
private Thread childThread;
private Callback callback;
public interface Callback {
void onFinishDecode();
void onDecodeFrame(byte[] data);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
public void setEnqueue(LinkedBlockingQueue<byte[]> queue) {
mQueue = queue;
}
public void setSaveFrames(String dir, OutputImageFormat imageFormat) throws IOException {
outputImageFormat = imageFormat;
File theDir = new File(dir);
if (!theDir.exists()) {
theDir.mkdirs();
} else if (!theDir.isDirectory()) {
throw new IOException("Not a directory");
}
OUTPUT_DIR = theDir.getAbsolutePath() + "/";
}
public void stopDecode() {
stopDecode = true;
}
public void decode(String videoFilePath) throws Throwable {
this.videoFilePath = videoFilePath;
if (childThread == null) {
childThread = new Thread(this, "decode");
childThread.start();
if (throwable != null) {
throw throwable;
}
}
}
public void run() {
try {
videoDecode(videoFilePath);
} catch (Throwable t) {
throwable = t;
}
}
public void videoDecode(String videoFilePath) throws IOException {
MediaExtractor extractor = null;
MediaCodec decoder = null;
try {
File videoFile = new File(videoFilePath);
extractor = new MediaExtractor();
extractor.setDataSource(videoFile.toString());
int trackIndex = selectTrack(extractor);
if (trackIndex < 0) {
throw new RuntimeException("No video track found in " + videoFilePath);
}
extractor.selectTrack(trackIndex);
MediaFormat mediaFormat = extractor.getTrackFormat(trackIndex);
String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
decoder = MediaCodec.createDecoderByType(mime);
showSupportedColorFormat(decoder.getCodecInfo().getCapabilitiesForType(mime));
if (isColorFormatSupported(decodeColorFormat, decoder.getCodecInfo().getCapabilitiesForType(mime))) {
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, decodeColorFormat);
Log.i(TAG, "set decode color format to type " + decodeColorFormat);
} else {
Log.i(TAG, "unable to set decode color format, color format type " + decodeColorFormat + " not supported")
Android 使用硬解码快速获取视频每一帧的数据并保存在本地
最新推荐文章于 2026-04-19 04:52:33 发布


1585

被折叠的 条评论
为什么被折叠?



