- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
。
。
1 、 char * outUrl = " rtmp://localhost/live/livestream " ; 这个地址,是AMS(Adeobe Media Server)的默认地址。 2 、 // 注册所有的编解码器 avcodec_register_all(); // 注册所有的封装器 av_register_all(); // 注册所有网络协议 avformat_network_init(); // 打开摄像头 VideoCapture cam; namedWindow( " video " ); Mat frame; // 像素格式转换上下文 SwsContext* vsc = NULL; // 输出的数据结构 AVFrame* yuv = NULL; // 编码器上下文 AVCodecContext* vc = NULL; // rtmp flv 封装器 AVFormatContext* ic = NULL; 声明好多变量,是OpenCV & MMPEAG 正常运行所需要的。 3 、 try { /// 1. Open Cam // 这里默认打开的是摄像头0,并获得摄像头参数 cam.open( 0 ); if (! cam.isOpened()) { throw exception( " cam open failed " ); } cout << " cam open sucess " << endl; int inWidth = cam. get (CAP_PROP_FRAME_WIDTH); int inHeight = cam. get (CAP_PROP_FRAME_HEIGHT); int fps = cam. get (CAP_PROP_FPS); if (fps == 0 ) { fps = 25 ; } cout << fps<< endl; /// 2. 初始化 SwsContext(转换格式上下文) vsc = sws_getCachedContext(vsc, inWidth, inHeight, AV_PIX_FMT_BGR24, inWidth, inHeight, AV_PIX_FMT_YUV420P, SWS_BICUBIC, 0 , 0 , 0 ); if (! vsc) { throw exception( " sws_getCachedContext failed " ); } /// 3.初始化输出的数据结构 yuv = av_frame_alloc(); yuv ->format = AV_PIX_FMT_YUV420P; yuv ->width = inWidth; yuv ->height = inHeight; yuv ->pts = 0 ; // 分配 YUV 空间 int ret = av_frame_get_buffer(yuv, 32 ); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } /// 4. 初始化编码器上下文 // a. 找到编码器,这里全部基于MMPEAG AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (! codec) { throw exception( " Can't find H.264 encoder " ); } // b. 创建编码器上下文 vc = avcodec_alloc_context3(codec); if (! vc) { throw exception( " avcodec_alloc_context3 failed " ); } // c. 配置编码器参数 vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; vc ->codec_id = codec-> id; vc ->thread_count = 8 ; vc ->bit_rate = 50 * 1024 * 8 ; // video size(bits) per second: 50kByte vc->width = inWidth; vc ->height = inHeight; vc ->time_base = { 1 ,fps }; // used to calculate pts: pts*time_base = second vc->framerate = { fps, 1 }; vc ->gop_size = 50 ; // for how many frames there is a I frame(关键帧) vc->max_b_frames = 0 ; // if these is no B frames, the orders of both decoding and presentation will be the same vc->pix_fmt = AV_PIX_FMT_YUV420P; // d. 打开编码器上下文(Open encodder context) ret = avcodec_open2(vc, 0 , 0 ); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } cout << " avcodec_open2 successed! " << endl; /// 5. 输出封装器和频流配置 // a. Create context for MUX ret = avformat_alloc_output_context2(&ic, 0 , " flv " , outUrl); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } // b. Add video stream AVStream* vs = avformat_new_stream(ic, NULL); if (! vs) { throw exception( " avformat_new_stream failed " ); } vs ->codecpar->codec_tag = 0 ; // copy parameter from Encoder to MUX avcodec_parameters_from_context(vs-> codecpar, vc); av_dump_format(ic, 0 , outUrl, 1 ); /// 6. Open rtmp output IO(打开输出IO) ret = avio_open(&ic-> pb, outUrl, AVIO_FLAG_WRITE); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } // write mux header ret = avformat_write_header(ic, NULL); // after this operation the stream's time_base will also be changed, not vc->time_base anymore if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } AVPacket pack; memset( &pack, 0 , sizeof (pack)); int vpts = 0 ; // 住循环,读入->显示->转码 for (;;) { /// 从cam中读取书 if (! cam.grab()) { continue ; } if (! cam.retrieve(frame)) { continue ; } imshow( " video " , frame); waitKey( 1 ); /// convert RGB to YUV // Input data structure--RGB uint8_t* indata[AV_NUM_DATA_POINTERS] = { 0 }; // srcStride indata[ 0 ] = frame.data; int inlinesize[AV_NUM_DATA_POINTERS] = { 0 }; // srcSlice // 一行(宽)数据的字节数 inlinesize[ 0 ] = frame.cols * frame.elemSize(); int h = sws_scale(vsc, indata, inlinesize, 0 , frame.rows, yuv ->data, yuv-> linesize); if (h <= 0 ) { continue ; } /// Mux YUV to flv h.264 yuv->pts = vpts; vpts ++ ; ret = avcodec_send_frame(vc, yuv); if (ret != 0 ) { continue ; } ret = avcodec_receive_packet(vc, & pack); if (ret != 0 || pack.size > 0 ) { cout << ' * ' <<pack.size<< flush; } else { continue ; } /// 推流 pack.pts = av_rescale_q(pack.pts, vc->time_base, vs-> time_base); pack.dts = av_rescale_q(pack.dts, vc->time_base, vs-> time_base); ret = av_interleaved_write_frame(ic, & pack); if (ret == 0 ) { cout << ' # ' << flush; } } } catch (exception & ex) { if (cam.isOpened()) cam.release(); if (vsc) { sws_freeContext(vsc); vsc = NULL; } if (vc) { avio_closep( &ic-> pb); avcodec_free_context( & vc); } cerr << ex.what() << endl; } getchar();
。
#include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <iostream> #include " XMediaEncode.h " #include " XRtmp.h " extern " C " { #include <libswscale/swscale.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> } using namespace cv; using namespace std; // 初始化像素格式上下文 void test004() { // 相机的rtsp url char *inUrl = " rtsp://admin:@192.168.10.30:554/ch0_0.264 " ; VideoCapture cam; namedWindow( " video " ); // 像素格式转换上下文 SwsContext *vsc = NULL; try { /////////////////////////////////////////////////////////////// / /// 1 使用opencv打开rtsp相机 cam.open(inUrl); if (! cam.isOpened()) { throw exception( " cam open failed! " ); } cout << inUrl << " cam open success " << endl; int inWidth = ( int )cam. get (CAP_PROP_FRAME_WIDTH); int inHeight = ( int )cam. get (CAP_PROP_FRAME_HEIGHT); int fps = ( int )cam. get (CAP_PROP_FPS); /// 2 初始化格式转换上下文 vsc = sws_getCachedContext(vsc, inWidth, inHeight, AV_PIX_FMT_BGR24, // 源宽、高、像素格式 inWidth, inHeight, AV_PIX_FMT_YUV420P, // 目标宽、高、像素格式 SWS_BICUBIC, // 尺寸变化使用算法 0 , 0 , 0 ); if (! vsc) { throw exception( " sws_getCachedContext failed! " ); } Mat frame; for (;;) { /// 读取rtsp视频帧,解码视频帧 if (! cam.grab()) { continue ; } /// yuv转换为rgb if (! cam.retrieve(frame)) { continue ; } imshow( " video " , frame); waitKey( 1 ); } } catch (exception & ex) { if (cam.isOpened()) cam.release(); if (vsc) { sws_freeContext(vsc); vsc = NULL; } cerr << ex.what() << endl; } getchar(); } // rtsp数据源到rtmp推流 要重点复习 void test005() { cout << " void test005()! " << endl; // 相机的rtsp url char *inUrl = " rtsp://admin:@192.168.10.30:554/ch0_0.264 " ; // nginx-rtmp 直播服务器rtmp推流URL char *outUrl = " rtmp://192.168.10.181/live " ; // 注册所有的编解码器 avcodec_register_all(); // 注册所有的封装器 av_register_all(); // 注册所有网络协议 avformat_network_init(); VideoCapture cam; Mat frame; namedWindow( " video " ); // 像素格式转换上下文 SwsContext *vsc = NULL; // 输出的数据结构 AVFrame *yuv = NULL; // 编码器上下文 AVCodecContext *vc = NULL; // rtmp flv 封装器 AVFormatContext *ic = NULL; try { /////////////////////////////////////////////////////////////// / /// 1 使用opencv打开rtsp相机 cam.open(inUrl); if (! cam.isOpened()) { throw exception( " cam open failed! " ); } cout << inUrl << " cam open success " << endl; int inWidth = ( int )cam. get (CAP_PROP_FRAME_WIDTH); int inHeight = ( int )cam. get (CAP_PROP_FRAME_HEIGHT); int fps = ( int )cam. get (CAP_PROP_FPS); /// 2 初始化格式转换上下文 vsc = sws_getCachedContext(vsc, inWidth, inHeight, AV_PIX_FMT_BGR24, // 源宽、高、像素格式 inWidth, inHeight, AV_PIX_FMT_YUV420P, // 目标宽、高、像素格式 SWS_BICUBIC, // 尺寸变化使用算法 0 , 0 , 0 ); if (! vsc) { throw exception( " sws_getCachedContext failed! " ); } /// 3 初始化输出的数据结构 yuv = av_frame_alloc(); yuv ->format = AV_PIX_FMT_YUV420P; yuv ->width = inWidth; yuv ->height = inHeight; yuv ->pts = 0 ; // 分配yuv空间 int ret = av_frame_get_buffer(yuv, 32 ); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } /// 4 初始化编码上下文,分为以下三步 // a 找到编码器 AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (! codec) { throw exception( " Can`t find h264 encoder! " ); } // b 创建编码器上下文 vc = avcodec_alloc_context3(codec); if (! vc) { throw exception( " avcodec_alloc_context3 failed! " ); } // c 配置编码器参数 vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; // 全局参数 vc->codec_id = codec-> id; vc ->thread_count = 8 ; vc ->bit_rate = 50 * 1024 * 8 ; // 压缩后每秒视频的bit位大小 50kB vc->width = inWidth; vc ->height = inHeight; vc ->time_base = { 1 ,fps }; vc ->framerate = { fps, 1 }; // 画面组的大小,多少帧一个关键帧 vc->gop_size = 50 ; vc ->max_b_frames = 0 ; vc ->pix_fmt = AV_PIX_FMT_YUV420P; // d 打开编码器上下文 ret = avcodec_open2(vc, 0 , 0 ); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } cout << " avcodec_open2 success! " << endl; /// 5 输出封装器和视频流配置 // a 创建输出封装器上下文 ret = avformat_alloc_output_context2(&ic, 0 , " flv " , outUrl); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } // b 添加视频流 AVStream *vs = avformat_new_stream(ic, NULL); if (! vs) { throw exception( " avformat_new_stream failed " ); } vs ->codecpar->codec_tag = 0 ; // 从编码器复制参数 avcodec_parameters_from_context(vs-> codecpar, vc); av_dump_format(ic, 0 , outUrl, 1 ); /// 打开rtmp 的网络输出IO ret = avio_open(&ic-> pb, outUrl, AVIO_FLAG_WRITE); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } // 写入封装头 ret = avformat_write_header(ic, NULL); if (ret != 0 ) { char buf[ 1024 ] = { 0 }; av_strerror(ret, buf, sizeof (buf) - 1 ); throw exception(buf); } AVPacket pack; memset( &pack, 0 , sizeof (pack)); int vpts = 0 ; // 死循环 for (;;) { /// 读取rtsp视频帧,解码视频帧 if (! cam.grab()) { continue ; } /// yuv转换为rgb if (! cam.retrieve(frame)) { continue ; } imshow( " video " , frame); waitKey( 1 ); /// rgb to yuv // 输入的数据结构 uint8_t *indata[AV_NUM_DATA_POINTERS] = { 0 }; // indata[0] bgrbgrbgr // plane indata[0] bbbbb indata[1]ggggg indata[2]rrrrr indata[ 0 ] = frame.data; int insize[AV_NUM_DATA_POINTERS] = { 0 }; // 一行(宽)数据的字节数 insize[ 0 ] = frame.cols * frame.elemSize(); int h = sws_scale(vsc, indata, insize, 0 , frame.rows, // 源数据 yuv->data, yuv-> linesize); if (h <= 0 ) { continue ; } cout << h << " " << flush; /// h264编码 yuv->pts = vpts; vpts ++ ; ret = avcodec_send_frame(vc, yuv); if (ret != 0 ) continue ; ret = avcodec_receive_packet(vc, & pack); if (ret != 0 || pack.size > 0 ) { cout << " * " << pack.size << flush; } else { continue ; } // 推流 pack.pts = av_rescale_q(pack.pts, vc->time_base, vs-> time_base); pack.dts = av_rescale_q(pack.dts, vc->time_base, vs-> time_base); pack.duration = av_rescale_q(pack.duration, vc->time_base, vs-> time_base); ret = av_interleaved_write_frame(ic, & pack); if (ret == 0 ) { cout << " # " << flush; } } } catch (exception & ex) { if (cam.isOpened()) cam.release(); if (vsc) { sws_freeContext(vsc); vsc = NULL; } if (vc) { avio_closep( &ic-> pb); avcodec_free_context( & vc); } cerr << ex.what() << endl; } getchar(); } // opencv_rtsp_to_rtmp_class封装重构代码 要重点复习 void test006() { cout << " void test006()! " << endl; // 相机的rtsp url char *inUrl = " rtsp://admin:@192.168.10.30:554/ch0_0.264 " ; // nginx-rtmp 直播服务器rtmp推流URL char *outUrl = " rtmp://192.168.10.181/live " ; // 编码器和像素格式转换 XMediaEncode *me = XMediaEncode::Get( 0 ); // 封装和推流对象 XRtmp *xr = XRtmp::Get( 0 ); VideoCapture cam; Mat frame; namedWindow( " video " ); int ret = 0 ; try { /////////////////////////////////////////////////////////////// / /// 1 使用opencv打开rtsp相机 cam.open(inUrl); if (! cam.isOpened()) { throw exception( " cam open failed! " ); } cout << inUrl << " cam open success " << endl; int inWidth = ( int )cam. get (CAP_PROP_FRAME_WIDTH); int inHeight = ( int )cam. get (CAP_PROP_FRAME_HEIGHT); int fps = ( int )cam. get (CAP_PROP_FPS); /// 2 初始化格式转换上下文 /// 3 初始化输出的数据结构 me->inWidth = inWidth; me ->inHeight = inHeight; me ->outWidth = inWidth; me ->outHeight = inHeight; me -> InitScale(); /// 4 初始化编码上下文 // a 找到编码器 if (!me-> InitVideoCodec()) { throw exception( " InitVideoCodec failed! " ); } /// 5 输出封装器和视频流配置 xr-> Init(outUrl); // 添加视频流 xr->AddStream(me-> vc); xr -> SendHead(); for (;;) { /// 读取rtsp视频帧,解码视频帧 if (! cam.grab()) { continue ; } /// yuv转换为rgb if (! cam.retrieve(frame)) { continue ; } // imshow("video", frame); // waitKey(1); /// rgb to yuv me->inPixSize = frame.elemSize(); AVFrame *yuv = me->RGBToYUV(( char * )frame.data); if (!yuv) continue ; /// h264编码 AVPacket *pack = me-> EncodeVideo(yuv); if (!pack) continue ; xr -> SendFrame(pack); } } catch (exception & ex) { if (cam.isOpened()) cam.release(); cerr << ex.what() << endl; } getchar(); } int main( int argc, char * argv[]) { // test000(); // test001(); // test002(); // test003(); // test004(); // test005(); test006(); return 0 ; }
。
。
。
。
。
。
。
https://files.cnblogs.com/files/blogs/758212/opencv_rtsp2rtmp-master.rar https://files.cnblogs.com/files/blogs/758212/main.js 。
最后此篇关于如何使用OpenCV+MMPEAG打开摄像头,显示的同时推送RTMP流。的文章就讲到这里了,如果你想了解更多关于如何使用OpenCV+MMPEAG打开摄像头,显示的同时推送RTMP流。的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
避免必须自己创建整个相机应用程序,我调用: Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); this.startActivit
我使用这种方法从前置摄像头录制视频: Recording video via Mediarecorder 它在我的 Nexus 4 上运行良好,但有人说有很多手机的前置摄像头无法录制视频,只能拍照。我
我正在使用 Android 手机的摄像头作为输入来测试成像算法,并且需要一种方法来始终如一地测试算法。理想情况下,我想获取预先录制的视频源并让手机“假装”视频源是来自相机的实时视频。 我理想的解决方案
我想在 android 上通过 v4l 访问外部 USB 摄像头。 我试过了 SimpleWebCam .在对原始源代码进行一些细微修改后,我实现了使其在 Root过的 android 设备上运行。然
我正在尝试连接两个连接到单个 USB 端口的 USB 网络摄像头。问题是当时只有一个摄像头工作...我在 python 中使用 OpenCV。这可能吗?我的目标是将多台相机连接到一台计算机以进行机器视
我想知道如何在 virtualbox 中使用笔记本电脑的内置网络摄像头和 android x86。 我已经尝试启动默认的“相机”应用程序,它告诉我必须配置 SDCard,我在本教程中所做的:SD ca
我在 64 位华硕的 Ubuntu 12.10 上安装了 ARToolKit。安装没有错误,所以我觉得我没问题。但是当我想尝试一个例子时,它找不到相机。如果我在 char *vconf = ""; 没
我想以编程方式移动 webvr 场景中 View 的位置。为此,我使用了position.add 方法。 以下是我如何以编程方式移动相机: 摄像机移至此处: var obj3d = docume
我正在使用 Camera 2 API 将 JPEG 图像保存在磁盘上。我的 Nexus 5X 目前有 3-4 fps,我想将其提高到 20-30。可能吗? 将图像格式更改为 YUV 我设法生成 30
Baby Monitor (http://www.babymonitor3g.com/) 等应用程序可让两台 iOS 设备相互连接。连接后,一台设备可以激活另一台设备上的摄像头、灯光和麦克风,即使该应
我有一个论坛帖子表单,允许发帖人附加录音和/或网络摄像头快照。这两个都是使用 navigator.getUserMedia() 实现的应用程序接口(interface)。对于音频,我建立了 varia
我对 Opencv 和 Python 有疑问。当我尝试从相机中查看帧时,它无法识别 USB 相机,我使用了带有两个 USB 相机的书籍中的标准代码,问题是只有一个相机工作,我不知道。我在 window
我编写了一个程序,基本上使用步进电机 + a4988 驱动程序将托盘放在连接到 Raspberry Pi 的相机下方。代码将托盘带到起始位置,迈出一步,拍照并重复 10 次。然后托盘返回到起始位置。我
我的 uEye 相机遇到了一个问题。使用我的笔记本电脑摄像头(id 0)或 usb 上的网络摄像头(id 1)这条线完美运行: TheVideoCapturer.open(1); (TheVideoC
我是 Android 版 openCV 的新手,我需要一个在后台运行的图像处理应用(检测图像的线条)。 我已经制作了一个应用程序来完成我需要的所有图像处理(使用 jni),但它不能在后台运行并且它使用
我正在尝试使用 OpenCV 从 USB 摄像头捕获视频。 #include #include using namespace std; using namespace cv; int main(
我正在寻找启用和禁用默认 iPhone 相机的方法,例如在特定时间或纬度/经度。有些地方是禁止摄像头的,所以我们可以在到达这样的地方时关闭它,这里只是举个例子。好吧,我认为在 iPhone 中禁用和启
有人有这种“东西”的工作样本吗? 在理论上,以这种方式实现它是个好主意,但我没有看到任何相关的代码 fragment 来说明如何实现它。 我不要花哨的东西,越简单越好。我只想在单独的线程上实现与相机控
我正在开发一个新网站。您可以加入房间通话并进行语音通话,因此可以使用您的网络摄像头,但您也可以共享您的屏幕。 问题是当我将轨道添加到流中时,对等点不再工作......我不知道如何解决这个问题。我还尝试
我需要在 Flutter 中创建一个考试应用程序,我们需要每隔一段时间拍摄用户的照片和视频,而在执行此操作时我们不想显示相机屏幕。 我尝试使用 Flutter 的 Camera 插件,但我无法找到任何
我是一名优秀的程序员,十分优秀!