Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

This paper gives an overview of

  • In this article, part 5 of JavaCV’s Webcam In Action, let’s consider a question: How can local webcam content be seen by others on the network?

  • This involves pushing the stream, as shown in the picture below. The JavacV-based application pushes the camera’s video frame to the media server, and viewers can remotely connect the media server with the player software to watch the camera’s content:

  • Today’s main work is to develop the JavaCV application shown above, and then verify whether the function is normal;

coding

  • “JavaCV camera one of actual combat: basis” to create simple – grab – had written the superclass AbstractCameraApplication push the project and we continue to use the project, create subclasses implement the abstract methods

  • Before coding, we should review the basic structure of the parent class, as shown in the figure below. The bold is the method defined by the parent class, and the red block is the method that needs to be subclassed to implement the abstract method. Therefore, we should implement the three red methods with the goal of local window preview:

  • New file RecordCamera Java, this is a subclass of AbstractCameraApplication, its code is very simple, the next order, according to the above instructions

  • The media server has been deployed in JavaCV Camera: Basic. Here we define a member variable to save the media server push stream address. Please adjust it according to your own situation:

private static final String RECORD_ADDRESS = "RTMP: / / 192.168.50.43:21935 / HLS/camera";
Copy the code
  • Also prepare a member variable to add a timestamp to the frame when pushing the stream:
protected long startRecordTime = 0L;
Copy the code
  • The function of pushing video frames to the media server comes from FrameRecorder, which is an abstract class. This article uses its subclass FFmpegFrameRecorder, so we define FrameRecorder member variables:
	// Frame recorder
    protected FrameRecorder recorder;
Copy the code
  • Next is the initialization operation, please pay attention to the parameter Settings (in the case of 1280*720 resolution camera) :
    @Override
    protected void initOutput(a) throws Exception {
        // instantiate FFmpegFrameRecorder to pass in the SRS push address
        recorder = FrameRecorder.createDefault(RECORD_ADDRESS, getCameraImageWidth(), getCameraImageHeight());

        // Reduce startup latency, see
        // https://trac.ffmpeg.org/wiki/StreamingGuide)
        recorder.setVideoOption("tune"."zerolatency");
        // Choose a suitable solution between video quality and encoding speed, including these options:
        // ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, veryslow
        // ultrafast offers us the least amount of compression (lower encoder
        // CPU) at the cost of a larger stream size
        // at the other end, veryslow provides the best compression (high
        // encoder CPU) while lowering the stream size
        // (see: https://trac.ffmpeg.org/wiki/Encode/H.264)
        // Ultrafast has the lowest CPU consumption
        recorder.setVideoOption("preset"."ultrafast");
        // Constant Rate Factor (see: https://trac.ffmpeg.org/wiki/Encode/H.264)
        recorder.setVideoOption("crf"."28");
        // 2000 kb/s, reasonable "sane" area for 720
        recorder.setVideoBitrate(2000000);

        // Set the encoding format
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);

        // Set the encapsulation format
        recorder.setFormat("flv");

        // FPS (frames per second)
        // The number of frames per second
        recorder.setFrameRate(getFrameRate());
        // Key frame interval, in our case every 2 seconds -> 30 (fps) * 2 = 60
        // Keyframe interval
        recorder.setGopSize((int)getFrameRate()*2);

        // The frame recorder starts initialization
        recorder.start();
    }
Copy the code
  • Output = output; output = output; output = output; output = output
    @Override
    protected void output(Frame frame) throws Exception {
        if (0L==startRecordTime) {
            startRecordTime = System.currentTimeMillis();
        }

        / / timestamp
        recorder.setTimestamp(1000 * (System.currentTimeMillis()-startRecordTime));

        / / save
        recorder.record(frame);
    }
Copy the code
  • Finally, there is something to do before the program exits from the video loop, which is to close the frame grabber:
    @Override
    protected void releaseOutputResource(a) throws Exception {
        recorder.close();
    }
Copy the code
  • Also pay attention to the delay between frames, because the push stream involves the network, so it can’t be calculated strictly according to the frame rate like local preview, the actual interval is smaller:
    @Override
    protected int getInterval(a) {
        // The time between two frames is shorter when pushing streams compared to local preview
        return super.getInterval()/4;
    }
Copy the code
  • Note that the parameter 600 indicates that the capture and recording operation is executed for 600 seconds:
    public static void main(String[] args) {
        new RecordCamera().action(600);
    }
Copy the code
  • Run the main method and wait until the console outputs the following red box to indicate that it has started pushing the stream:

  • With this machine or another computer local area network (LAN), push open just flow with VLC software address RTMP: / / 192.168.50.43:21935 / HLS/camera, wait a few seconds later began to normal play:

  • You can also use VLC’s tools to view the encoding information:

  • So far, we have completed the push stream function, verify the remote playback is normal, thanks to the powerful JavaCV, the whole process is so easy and happy, please continue to pay attention to The original “JavaCV camera actual battle” series will present more rich applications;

  • At this point, you must have spotted a problem: Just tweet videos? Not even a sound. Is that it? That’s right. Next thing we know, it’s time for audio processing

Download the source code

  • The full source code for JavaCV Camera In Action is available on GitHub at github.com/zq2599/blog…
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  • The Git project has multiple folders. The source code for this project is in the Javacv-tutorials folder, as shown in the red box below:

  • Javacv-tutorials have many subprojects. The Code for the JavacV Camera Hands-on series was based on the Simply-grab-push project:

You are not alone, Xinchen original accompany all the way

Github.com/zq2599/blog…