Java+opencv 读取视频文件flv格式,按帧生成图片预览

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;

import java.io.File;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

public class MP4Image {
public void imageI() {
try {
File source1 = new File(“E:\test\flex.flv”);
VideoAttributes video1 = new VideoAttributes();
Encoder encoder1 = new Encoder();
video1.setCodec(“png”);//转图片
video1.setSize(new VideoSize(600, 500));
//获取时长
MultimediaInfo m = encoder1.getInfo(source1);
System.out.println(m.getDuration());
EncodingAttributes attrs1 = new EncodingAttributes();
long beginTime1 = System.currentTimeMillis();
beginTime1 = System.currentTimeMillis();
for(int i = 0;i < m.getDuration()/1000;i++){
File target1 = new File(“E:\test\target”+i+”.png”);//转图片
attrs1.setFormat(“image2”);//转图片
attrs1.setOffset(i+0f);//设置偏移位置,即开始转码位置(3秒)
attrs1.setDuration(0.01f);//设置转码持续时间(1秒)
attrs1.setVideoAttributes(video1);
encoder1.encode(source1, target1, attrs1);
try {
Thread.currentThread().sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
run(“E:\test\target”+i+”.png”,”E:\test\targetx”+i+”.png”);
}
System.out.println(“获取时长花费时间是:” + (System.currentTimeMillis() - beginTime1));
System.out.println(“视频转码花费时间是:” + (System.currentTimeMillis() - beginTime1));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InputFormatException e) {
e.printStackTrace();
} catch (EncoderException e) {
e.printStackTrace();
}
}
public void run(String res,String tar) {
System.out.println(“\nRunning DetectFaceDemo”);
System.out.println(getClass().getResource(“haarcascade_frontalface_alt2.xml”).getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource(“lbpcascade_frontalface.xml”).getPath());
//Mat image = Highgui.imread(getClass().getResource(“lena.png”).getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
//String xmlfilePath=getClass().getResource(“lbpcascade_frontalface.xml”).getPath().substring(1);
//效果比上面注释的好多了
String xmlfilePath=getClass().getResource(“haarcascade_frontalface_alt2.xml”).getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(res);
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format(“Detected %s faces”, faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
//String filename = “faceDetection”+i+”.png”;
System.out.println(String.format(“Writing %s”, tar));
Highgui.imwrite(tar, image);
}
public static void main(String[] args) {
// /*File source = new File(“E:\test\flex.mp4”);
// File target = new File(“E:\test\flex.flv”);// 转flv
//
// AudioAttributes audio = new AudioAttributes();
// audio.setCodec(“libmp3lame”);
// audio.setBitRate(new Integer(64000));
// audio.setChannels(new Integer(1));
// audio.setSamplingRate(new Integer(22050));
// VideoAttributes video = new VideoAttributes();
// video.setCodec(“flv”);// 转flv
// video.setBitRate(new Integer(180000));// 180kb/s比特率
// video.setFrameRate(new Integer(1));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊
// EncodingAttributes attrs = new EncodingAttributes();
// attrs.setFormat(“flv”);// 转flv
// attrs.setAudioAttributes(audio);
// attrs.setVideoAttributes(video);
// Encoder encoder = new Encoder();
// long beginTime = System.currentTimeMillis();
// try {
// // 获取时长
// MultimediaInfo m = encoder.getInfo(source);
// long sTime = m.getDuration();
// long minute = sTime / 60000;
// long second = (sTime % 60000) / 1000;
// System.out.println(“视频时长:” + minute + “:” + (second < 10 ? “0” + second : second));
// System.out.println(“获取时长花费时间是:” + (System.currentTimeMillis() - beginTime));
// beginTime = System.currentTimeMillis();
// encoder.encode(source, target, attrs);
// System.out.println(“视频转码花费时间是:” + (System.currentTimeMillis() - beginTime));
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
// } catch (InputFormatException e) {
// e.printStackTrace();
// } catch (EncoderException e) {
// e.printStackTrace();
// }
//
// */
System.loadLibrary(“opencv_java246”);
new MP4Image().imageI();

}

}

你可能感兴趣的:(java-opencv)