JAVACV the rtsp of the Hai Kang webcam to the rtmp stream

Source: Internet
Author: User
Tags deprecated

Objective:

Using the javacv1.2 version, refer to the code on the Internet, encountered a number of very serious problems, the last one to eliminate the solution. The main issues to be addressed are:

Problem one, ffmpeg default is the way to use UDP, often the problem of packet loss, you need to change the UDP to TCP can be resolved, the following is the content of the error:

Input #0, RTSP, from ' RTSP://ADMIN:123123@192.168.1.64:554/STREAMING/CHANNELS/1 ': Metadata:title:Media Presentation duration:n/a, start:0.040000, bitrate:n/a Stream #0:0: video:h264 (Main), yuvj420p (PC, bt709), 2560 x1440, FPS, TBR, 90k TBN [Swscaler @ 000000001be02720] deprecated pixel format used, make sure you did set range Cor rectly [rtsp @ 0000000000F8C9A0] Max Delay reached. Need to consume packet [rtsp @ 0000000000F8C9A0] rtp:missed 4188 packets [rtsp @ 0000000000F8C9A0] Rtp:pt=60:bad cseq 6 1ae expected=5152 [rtsp @ 0000000000F8C9A0] Max Delay reached. Need to consume packet [rtsp @ 0000000000F8C9A0] rtp:missed 4189-Packets [rtsp @ 0000000000F8C9A0] Max Delay reached. Need to consume packet [rtsp @ 0000000000F8C9A0] rtp:missed-Packets [H264 @ 000000001A9FBFE0] left block unavailable f  or requested intra mode [H264 @ 000000001A9FBFE0] Error while decoding MB 0, ByteStream 23926 [H264 @ 000000001A9FBFE0] Concealing 3729 DC, 3729 AC, 3729 MV errors in P frame [Swscaler @ 000000001be02720] deprecated pixel format used, make sure you did set range correctly [r TSP @ 0000000000f8c9a0] max Delay reached. Need to consume packet [rtsp @ 0000000000F8C9A0] rtp:missed

Problem two, after TCP has not been set to switch, but has been around 50 frames can not get the frame, the return is always null, the problem is caused by the size of my Hai Kang camera is too big reason. Concrete has not yet been studied, but to solve the problem. But backstage still reported countless mistakes, do not understand what is caused. But the function is ready to use. The content of the error:

[Swscaler @ 000000001be02720] deprecated pixel format used, make sure you did set range correctly



Okay, go straight to the code. Help people please point a praise, study for a few days is not easy ah.



Package test;
Import Org.bytedeco.javacpp.avcodec;
Import Org.bytedeco.javacv.FFmpegFrameGrabber;
Import Org.bytedeco.javacv.FFmpegFrameRecorder;
Import Org.bytedeco.javacv.Frame;
Import Org.bytedeco.javacv.OpenCVFrameConverter;
	public class Testrtmpgrabberrecorder {static Boolean exit = FALSE;
		public static void Main (string[] args) throws Exception {System.out.println ("start ...");
	String Rtmppath = "Rtmp://casic207-pc1/live360p/ss1"; String Rtsppath = "RTMP://LIVE.HKSTV.HK.LXDNS.COM/LIVE/HKS";
		Hong Kong ratings//string Rtsppath = "Rtsp://184.72.239.149/vod/mp4://bigbuckbunny_175k.mov";
		String Rtsppath = "Rtsp://admin:123123@192.168.1.64:554/h264/ch1/main/av_stream"; Ffmpeg-f rtsp-rtsp_transport tcp-i Rtsp://admin:leeking123@192.168.1.64:554/h264/ch1/main/av_stream rtmp:// CASIC207-PC1/LIVE360P/SS1//ffmpeg-i Rtsp://admin:123123@192.168.1.64:554/h264/ch1/main/av_stream-vcodec copy- ACODEC copy-f flv rtmp://casic207-pc1/live360p/ss1 int audiorecord = 0; 0 = Do not record, 1 = record Boolean savevideo = false;
		Test (Rtmppath,rtsppath,audiorecord,savevideo);
	System.out.println ("End ..."); public static void Test (String rtmppath,string rtsppath,int Audiorecord,boolean savevideo) throws Exception {//fr Amegrabber grabber = framegrabber.createdefault (0);
		The native camera defaults//uses the RTSP when need to use Ffmpegframegrabber, can no longer use framegrabber int width = 640,height = 480; 
		Ffmpegframegrabber grabber = Ffmpegframegrabber.createdefault (Rtsppath); Grabber.setoption ("Rtsp_transport", "TCP"); Use TCP, or you will lose the packet is very serious//always the cause of the error ...
		Just because it's 2560 * 1440 is too big.
		Grabber.setimagewidth (width);
		Grabber.setimageheight (height);
		System.out.println ("grabber start");
		Grabber.start ();
		Framerecorder Recorder = Framerecorder.createdefault (Rtmppath, 640,480,0); Streaming media output address, resolution (long, high), whether to record audio (0: Do not record/1: record) ffmpegframerecorder recorder = new Ffmpegframerecorder (rtmppath, width, height,
		Audiorecord);
		Recorder.setinterleaved (TRUE);
		Recorder.setvideooption ("CRF", "28"); Recorder.setvIdeocodec (Avcodec. av_codec_id_h264); Recorder.setformat ("flv");
	 	RTMP type recorder.setframerate (25); Recorder.setpixelformat (0);
		yuv420p System.out.println ("recorder start");
		Recorder.start ();
		Opencvframeconverter.toiplimage conveter = new Opencvframeconverter.toiplimage ();
		System.out.println ("All start!!");
		int count = 0;
			while (!exit) {count++;
			Frame frame = grabber.grabimage ();
			if (frame = = null) {continue;
			} if (count% = 0) {System.out.println ("count=" +count); 
		} Recorder.record (frame);
		} grabber.stop ();
		Grabber.release ();
		Recorder.stop ();
	Recorder.release ();
 }
}

To add a very serious problem is
Frame frame = grabber.grabimage ();

The memory problem. The frame's memory has not been released, and eventually the memory crash caused the application to die.

The Javacpp.jar version needs to be more than 1.3.1. I have used 1.3.3 test. The problem has been solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.