悄摸直播(一)—— 推流器的实现(获取笔记本摄像头画面,转流推流到rtmp服务器)

编程入门 行业动态 更新时间:2024-10-23 01:59:05

悄摸直播(一)—— 推流器的实现(获取笔记本摄像头画面,转流推<a href=https://www.elefans.com/category/jswz/34/1656977.html style=流到rtmp服务器)"/>

悄摸直播(一)—— 推流器的实现(获取笔记本摄像头画面,转流推流到rtmp服务器)

悄摸直播 —— JavaCV实现本机摄像头画面远程直播

推流器

一、功能说明
获取pc端的摄像头流数据 + 展示直播效果 + 推流到rtmp服务器
二、代码实现
/*** 推流器* @param devicePath   摄像头的地址。可以是摄像头rtsp地址,也可以是设备号码,本机摄像头是0* @param outputPath   接收路径* @param v_rs         帧率* @throws Exception* @throws org.bytedeco.javacv.FrameRecorder.Exception* @throws InterruptedException*/public static void recordPush(String outputPath,int v_rs) throws Exception, org.bytedeco.javacv.FrameRecorder.Exception, InterruptedException {Loader.load(opencv_objdetect.class);//创建采集器OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  //本地摄像头默认为0//开启采集器try {grabber.start();  } catch (Exception e) {try {grabber.restart();  //一次重启尝试} catch (Exception e2) {throw e;}}OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();  //转换器  Frame grabframe = grabber.grab();  //获取一帧IplImage grabbedImage = null;if (grabframe!=null) {grabbedImage = converter.convert(grabframe); //将这一帧转换为IplImage}//创建录制器FrameRecorder recorder;recorder = FrameRecorder.createDefault(outputPath, 1280, 720);   //输出路径,画面高,画面宽recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);  //设置编码格式recorder.setFormat("flv");   recorder.setFrameRate(v_rs);recorder.setGopSize(v_rs);//开启录制器try {recorder.start();} catch (java.lang.Exception e) {System.out.println("recorder开启失败");System.out.println(recorder);try {if (recorder != null) {  //尝试重启录制器recorder.stop();recorder.start();}} catch (java.lang.Exception e1) {// TODO Auto-generated catch blocke.printStackTrace();}}//直播效果展示窗口CanvasFrame frame = new CanvasFrame("直播效果",CanvasFrame.getDefaultGamma() / grabber.getGamma());frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setAlwaysOnTop(true);//推流while(frame.isVisible() && (grabframe=grabber.grab()) != null) {frame.showImage(grabframe);   //展示直播效果grabbedImage = converter.convert(grabframe);Frame rotatedFrame = converter.convert(grabbedImage);if (rotatedFrame != null) {recorder.record(rotatedFrame);  }Thread.sleep(50);  //50毫秒/帧}}
三、测试推流器
public static void main(String[] args) throws Exception, org.bytedeco.javacv.FrameRecorder.Exception, InterruptedException {//设置rtmp服务器地址String outputPath = "rtmp://192.168.1.48:1935/live/stream";recordPush(outputPath, 25);}

如果出现“直播效果”的swing窗口,并能够播放摄像头画面,则说明推流器成功。

更多推荐

悄摸直播(一)—— 推流器的实现(获取笔记本摄像头画面,转流推流到rtmp服务器)

本文发布于:2024-03-09 04:30:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1723809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流到   摄像头   画面   笔记本   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!