JavaCV: save rtsp streaming as jpg to disk

Note: The content of the article is to record the meanings of some common units or nouns, etc. If there are any mistakes, please point them out in the comments, thank you very much.


foreword

Image: An image is composed of pixels and color information of pixels. Common pixel data include rgb, yuv, rbga, etc.

Image capacity: For example, if a color image (RGB) has a resolution of 256×512 and each color is represented by 8 bits, then the data volume of the color image is 256 512 8*3 bits.

Video frame: The image is formed by encoding (such as H264, etc., image data is compressed), and the video frame is composed of key frames (i frames) and B/P frames.

Frame rate: Unit FPS (how many frames per second), affects the smoothness of the picture, and is directly proportional to the fluency of the picture.

Bit rate: the size of data encoded by the encoder per second, in kbps, for example, 800kbps means that the encoder generates 800kb (or 100KB) of data per second.

1. BufferedImage

Located in the java.awt.image package, it is mainly used to load an image into memory. (specifically use the doc of jdk)

2. JavaCV operates rtsp streaming media

1. Dependency library

<dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv</artifactId>
      <version>1.4.4</version>
    </dependency>

    <dependency>
      <groupId>org.bytedeco.javacpp-presets</groupId>
      <artifactId>ffmpeg-platform</artifactId>
      <version>4.1-1.4.4</version>
    </dependency>

2. Code

public class RtspTest {
    
    
  private FFmpegFrameGrabber grabber;
  private String rtsp;
  private boolean isStart = false;
  
  @Test
  public void test01() throws Exception {
    
    
    rtsp = "rtsp://admin:[email protected]";
    grabber = FFmpegFrameGrabber.createDefault(rtsp);         
    grabber.setOption("rtsp_transport","tcp");                  //tcp方式连接
    grabber.setFrameRate(10);                                   //设置帧率
    grabber.setImageWidth(740);                                 //设置获取的视频宽度
    grabber.setImageHeight(480);                                //设置获取的视频高毒
    grabber.setVideoBitrate(2000000);                           //设置视频比特率
   
    Java2DFrameConverter java2DFrameConverter = new Java2DFrameConverter();
    while(true){
    
    
      try{
    
    
        if(grabber != null && !isStart) {
    
    
          grabber.start();
          isStart = true;
          System.out.println("启动成功");
        }
        if(grabber != null){
    
    
          Frame frame = grabber.grabImage();
          if(null == frame){
    
    
            continue;
          }
          BufferedImage bufferedImage = java2DFrameConverter.getBufferedImage(frame);
          String fileName = UUID.randomUUID().toString();
          File file = new File("E:\\jpg\\"+fileName+"."+"jpg");
          ImageIO.write(bufferedImage,"jpg",file);
          bufferedImage.flush();
        }
      }catch(FrameGrabber.Exception | RuntimeException e){
    
    
        e.printStackTrace();
      } catch (IOException e) {
    
    
        e.printStackTrace();
      }
    }
  }

3. Results

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42717117/article/details/121033828