抓取在线视频流存到本地,同时截视频图
package ;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class videoFile {
public static Logger logger = LoggerFactory.getLogger(videoFile.class);
@Resource
videoFile videoFile;
public static AtomicBoolean running = new AtomicBoolean(true);
public String streamURL;
public String filePath;
public String imagePath;
public void setStreamURL(String streamURL) {
this.streamURL = streamURL;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public static void main(String[] args) throws InterruptedException {
String dir= "videoAddress/";
Boolean result = getStreamScreenshot(dir,dir,
"视频流地址");
System.out.println(result);
videoFile videoUtil = new videoFile();
Thread.sleep(500000);
System.out.println("结束录制");
videoUtil.running.set(false);
}
public static Boolean getStreamScreenshot(String filePath,String imagePath,String streamURL){
return getStream(filePath,imagePath,streamURL,true);
}
public static Boolean getStream(String filePath,String imagePath,String streamURL,boolean screenshot){
Boolean b = false ;
videoFile videoUtil = new videoFile();
videoUtil.setFilePath(filePath);
videoUtil.setImagePath(imagePath);
videoUtil.setStreamURL(streamURL);
try {
Thread t0 = new Thread(() -> {
logger.info("开始录制");
videoUtil.run(screenshot);
});
t0.start();
b = true;
} catch (Throwable t) {
logger.error(t.getMessage());
b = false;
t.printStackTrace();
}
return b;
}
public Boolean run(boolean screenshot) {
Boolean b = false;
Map<String,Object> map = new LinkedHashMap<>();
System.out.println(streamURL);
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(streamURL);
FFmpegFrameRecorder recorder = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SS");
System.out.println("录制中..1");
try {
grabber.start();
System.out.println("录制中..2");
Frame frame = grabber.grabFrame();
if (frame != null) {
File outFile = new File(filePath);
System.out.println("录制中..3");
if (!outFile.isFile()) {
try {
outFile.createNewFile();
} catch (IOException e) {
System.out.println(e.toString());
logger.error(e.getMessage());
e.printStackTrace();
return false;
}
}
Date date = Calendar.getInstance().getTime();
String strDate = dateFormat.format(date);
recorder = new FFmpegFrameRecorder(filePath+strDate+".mp4", 1920, 1440, 1);
System.out.println("录制中..4");
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("MP4");
recorder.setFrameRate(30);
recorder.setVideoBitrate(800000);
System.out.println("录制中..5");
recorder.start();
int flag = 0;
int i=0;
while (frame != null && videoFile.running.get()) {
if(screenshot) {
if (flag%30==0&&i<20) {
date = Calendar.getInstance().getTime();
strDate = dateFormat.format(date);
frame = grabber.grabImage();
String fileName = imagePath + strDate +".jpg";
File outPut = new File(fileName);
ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
map.put(fileName, outPut);
if (i == 20){
screenshot = false;
}
i++;
}
}
recorder.record(frame);
frame = grabber.grabFrame();
flag++;
System.out.println("录制中...");
}
recorder.record(frame);
System.out.println("录制中..6");
recorder.stop();
grabber.stop();
b = true;
}
} catch (Exception e) {
System.out.println(e.toString());
logger.error(e.getMessage());
return false;
} finally {
if (null != grabber) {
try {
grabber.stop();
} catch (FrameGrabber.Exception e) {
System.out.println(e.toString());
logger.error(e.getMessage());
return false;
}
}
if (recorder != null) {
try {
recorder.stop();
} catch (FrameRecorder.Exception e) {
System.out.println(e.toString());
e.printStackTrace();
logger.error(e.getMessage());
return false;
}
}
}
videoFile.running.set(true);
return b ;
}
public static BufferedImage FrameToBufferedImage(Frame frame) {
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
return bufferedImage;
}
public static Boolean getVideoStream(String filePath,String imagePath,String streamURL){
return getStream(filePath,imagePath,streamURL,false);
}
}