Catch the online video stream and save it locally, and capture the video image at the same time
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;
/**
* @ClassName testUrl1
* @Author dell-pc
* @create 2023/3/9 17:20
*/
public class videoFile {<!-- -->
public static Logger logger = LoggerFactory. getLogger(videoFile. class);
@Resource
videoFile videoFile;
//Call atomic thread judgment
public static AtomicBoolean running = new AtomicBoolean(true);
public String streamURL;// stream URL
public String filePath;// video file path
public String imagePath;// image path, store the image of a certain frame of the intercepted video
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 {<!-- -->
//getVideoStream();
String dir= "videoAddress/"; //relative path address
Boolean result = getStreamScreenshot(dir,dir,
"Video stream address");
System.out.println(result);
videoFile videoUtil = new videoFile();
Thread. sleep(500000);
System.out.println("end recording");
videoUtil.running.set(false);
}
/**
* Get video stream snapshot
* @param filePath
* @param streamURL
*/
public static Boolean getStreamScreenshot(String filePath,String imagePath,String streamURL){<!-- -->
return getStream(filePath, imagePath, streamURL, true);
}
/**
* Start to execute the pull-down stream interception video stream
*/
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("Start recording");
videoUtil.run(screenshot);
});
t0. start();
b = true;
} catch (Throwable t) {<!-- -->
logger. error(t. getMessage());
b = false;
t. printStackTrace();
}
return b;
}
/**
* Perform video stream grabbing
*/
public Boolean run(boolean screenshot) {<!-- -->
Boolean b = false;
Map<String,Object> map = new LinkedHashMap<>();
// get video source
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("Recording..1");
try {<!-- -->
grabber.start();
System.out.println("Recording..2");
Frame frame = grabber. grabFrame();
if (frame != null) {<!-- -->
File outFile = new File(filePath);
System.out.println("Recording..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);
// Streaming media output address, resolution (length, height), whether to record audio (0: no recording/1: recording)
recorder = new FFmpegFrameRecorder(filePath + strDate + ".mp4", 1920, 1440, 1);
System.out.println("Recording..4");
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);// Live stream format
recorder.setFormat("MP4");// Recorded video format
recorder.setFrameRate(30);// frame rate
//The bit rate of Baidu translation, the default is 400,000, but my 400,000 is fuzzy, so it is more appropriate to adjust it to 800,000
recorder.setVideoBitrate(800000);
System.out.println("Recording..5");
recorder. start();
int flag = 0;
int i=0;
while (frame != null & amp; & amp; videoFile. running. get()) {<!-- -->
//Video snapshot
if(screenshot) {<!-- -->
if (flag0==0 & amp; & amp;i<20) {<!-- -->
date = Calendar.getInstance().getTime();
strDate = dateFormat. format(date);
frame = grabber. grabImage();
// file storage object
String fileName = imagePath + strDate + ".jpg";
File outPut = new File(fileName);
ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
map. put(fileName, outPut);
if (i == 20){<!-- -->
// end video snapshot
screenshot = false;
}
i + + ;
}
}
recorder.record(frame);// record
frame = grabber.grabFrame();// get the next frame
flag ++ ;
System.out.println("Recording...");
}
recorder. record(frame);
System.out.println("Recording..6");
// stop recording
recorder. stop();
grabber. stop();
//Video file address
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;
}
/**
* frame to image stream
* @param frame
* @return
*/
public static BufferedImage FrameToBufferedImage(Frame frame) {<!-- -->
//Create a BufferedImage object
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter. getBufferedImage(frame);
return bufferedImage;
}
/**
* Get the video stream
* @param filePath
* @param streamURL
*/
public static Boolean getVideoStream(String filePath,String imagePath,String streamURL){<!-- -->
return getStream(filePath, imagePath, streamURL, false);
}
}