linux FFMPEG 摄像头采集数据推流

Linux下用 FFMPEG 采集 usb摄像头视频 和 摄像头内置麦克风音频 到RTMP服务

ffmpeg -f video4linux2 -qscale 10 -r 12 -s 640x480 -i /dev/video0 -f alsa -i hw:1 -ab 16 -ar 22050 -ac 1 -f mp3 -f flv rtmp://127.0.0.1/rtmpsvr/rtmp1
./ffmpeg -f video4linux2 -s  640x480 -i /dev/video0  -f flv rtmp://127.0.0.1:1935/live/live

  首先说一下推流,主要使用ffmpeg命令进行本地摄像头的推流,为了实现首屏秒开使用-g设置gop大小,同时使用-b降低网络负载,保证流畅度。

linux

ffmpeg -r 30  -i /dev/video0 -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/test1

 window

ffmpeg -r 30  -f vfwcap -i 0 -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/test1

ffmpeg -list_devices true -f dshow -i dummy
ffmpeg -r 30  -f dshow -i video="1.3M HD WebCam" -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/tes
t1

  其次是収流,収流最开始的时候,有很大的延迟,大约5秒,后来通过优化,现在延时保证在1s以内,还是可以接收的,直接上収流的程序


AVFormatContext *pFormatCtx;
    int i, videoindex;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;
    AVFrame *pFrame, *pFrameRGB;
    uint8_t *out_buffer;
    AVPacket *packet;
    //int y_size;
    int ret, got_picture;
    struct SwsContext *img_convert_ctx;
    //输入文件路径
//    char filepath[] = "rtmp://219.216.87.170/vod/test.flv";
    char filepath[] = "rtmp://219.216.87.170/live/test1";
    int frame_cnt;

    printf("wait for playing %s\n", filepath);
    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();
    printf("size %ld\tduration %ld\n", pFormatCtx->probesize,
            pFormatCtx->max_analyze_duration);
    pFormatCtx->probesize = 20000000;
    pFormatCtx->max_analyze_duration = 2000;
//    pFormatCtx->interrupt_callback.callback = timout_callback;
//    pFormatCtx->interrupt_callback.opaque = pFormatCtx;
//    pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;

    AVDictionary* options = NULL;
    av_dict_set(&options, "fflags", "nobuffer", 0);
//    av_dict_set(&options, "max_delay", "100000", 0);
//    av_dict_set(&options, "rtmp_transport", "tcp", 0);
//    av_dict_set(&options, "stimeout", "6", 0);

    printf("wating for opening file\n");
    if (avformat_open_input(&pFormatCtx, filepath, NULL, &options) != 0) {
        printf("Couldn't open input stream.\n");
        return -1;
    }
    av_dict_free(&options);
    printf("wating for finding stream\n");
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
        printf("Couldn't find stream information.\n");
        return -1;
    }
    videoindex = -1;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoindex = i;
            break;
        }
    if (videoindex == -1) {
        printf("Didn't find a video stream.\n");
        return -1;
        }

    pCodecCtx = pFormatCtx->streams[videoindex]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL) {
        printf("Codec not found.\n");
        return -1;
        }
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        printf("Could not open codec.\n");
        return -1;
        }
    /*
     * 在此处添加输出视频信息的代码
     * 取自于pFormatCtx,使用fprintf()
     */
    pFrame = av_frame_alloc();
    pFrameRGB = av_frame_alloc();
    out_buffer = (uint8_t *) av_malloc(
            avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width,
                    pCodecCtx->height));
    avpicture_fill((AVPicture *) pFrameRGB, out_buffer, AV_PIX_FMT_BGR24,
            pCodecCtx->width, pCodecCtx->height);
    packet = (AVPacket *) av_malloc(sizeof(AVPacket));
    //Output Info-----------------------------
    printf("--------------- File Information ----------------\n");
    av_dump_format(pFormatCtx, 0, filepath, 0);
    printf("-------------------------------------------------\n");
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
            pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
            AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
    CvSize imagesize;
    imagesize.width = pCodecCtx->width;
    imagesize.height = pCodecCtx->height;
    IplImage *image = cvCreateImageHeader(imagesize, IPL_DEPTH_8U, 3);
    cvSetData(image, out_buffer, imagesize.width * 3);
    cvNamedWindow(filepath, CV_WINDOW_AUTOSIZE);

    frame_cnt = 0;
    int num = 0;
    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == videoindex) {
            /*
             * 在此处添加输出H264码流的代码
             * 取自于packet,使用fwrite()
             */
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
                    packet);
            if (ret < 0) {
                printf("Decode Error.\n");
                return -1;
            }
            if (got_picture) {
                sws_scale(img_convert_ctx,
                        (const uint8_t* const *) pFrame->data, pFrame->linesize,
                        0, pCodecCtx->height, pFrameRGB->data,
                        pFrameRGB->linesize);

                printf("Decoded frame index: %d\n", frame_cnt);

                /*
                 * 在此处添加输出YUV的代码
                 * 取自于pFrameYUV,使用fwrite()
                 */

                frame_cnt++;
                cvShowImage(filepath, image);
                cvWaitKey(30);

            }
        }
        av_free_packet(packet);
        }

    sws_freeContext(img_convert_ctx);

    av_frame_free(&pFrameRGB);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    return 0;

  将解压后的数据区与opencv的IplImage的数据区映射,实现opencv显示。

  

  检测部分,主要使用IplImage与yolo中的图像进行对接,在图像转换方面,进行了部分优化,缩减一些不必要的步骤。然后使用线程区接收ffmepg流,主循环里区做检测并显示。需要做线程同步处理,只有当收到新流时,才去检测。

原文地址:https://www.cnblogs.com/enumx/p/12346711.html

时间: 2024-08-01 21:46:32

linux FFMPEG 摄像头采集数据推流的相关文章

linux使用UVC采集数据

V4L2是V4L的升级版本,linux下视频设备程序提供了一套接口规范. 常用的结构体在内核目录include/linux/videodev2.h中定义 struct v4l2_requestbuffers  //申请帧缓冲,对应命令VIDIOC_REQBUFS struct v4l2_capability      //视频设备的功能,对应命令VIDIOC_QUERYCAP struct v4l2_input           //视频输入信息,对应命令VIDIOC_ENUMINPUT st

linux 服务器脚本采集数据中文无法执行错误

问题描述:在 RHEL6 版本的服务器上使用脚本操作数据库,其中一个SQL的字段值为中文,每次使用 crontab定时 执行该脚本无法获取数据,实现预期效果,而手动执行正常. oracle client使用的11.2.0版本 解决方法:编辑 /etc/profile文件(普通权限就行),添加如下内容: export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL PATH=$PATH:$HOME/bin export PATH expor

性能测试 基于Python结合InfluxDB及Grafana图表实时采集Linux多主机性能数据

基于Python结合InfluxDB及Grafana图表实时采集Linux多主机性能数据   by:授客 QQ:1033553122 实现功能 测试环境 环境搭建 使用前提 使用方法 运行程序 效果展示 实现功能 无需在被监控主机上安装代理,一键对Linux远程服务器不同主机执行性能监控.性能数据采集命令,并实时展示 支持跨堡垒机收集实时性能数据(注:定制化开发,非通用) 支持docker容器(因为程序实现是从docker容器内部获取性能数据,所以目前仅支持 CPU,内存,I/O) 使用前提 可

基于opencv和mfc的摄像头采集代码(GOMFCTemplate2)

基于opencv和mfc的摄像头采集框架(GOMFCTemplate2) 编写带界面的图像处理程序,选择opencv+mfc是一种很好的选择:在读取摄像头数据方面,网上的方法很多,其中shiqiyu的camerads的方法是较好的. 基于现有资料,通过在实际项目中的积累,我总结出来一套结合opencv和mfc的摄像头采集框架.具有以下特点: 1.基于directshow,兼容性好,速度快.到目前为止,无论是工业相机还是普通相机,没发现不兼容的: 2.摄像头部分通过线程读取,保证界面的运行流畅:

Linux使用nmon采集性能系统信息

1.nmon下载地址:https://sourceforge.net/projects/nmon/files/ 创建文件nmon:# mkdir nmon 解压文件夹:# tar -zxvf nmon_linux.tar.gz 赋予权限:# chmod -R nmon 启动nmon:# ./nmon_x86_64_sles12 2.配置环境变量: 更改文件名:# mv nmon_x86_64_sles12 nmon 添加环境变量:# vi /etc/profile PATH=$PATH:/usr

cmdb客户端采集数据的完善

file文件自己去拷贝(这里不提供) custom_settings.py import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 获取到根路径 MODE = 'agent' SSH_USERNAME = 'root' SSH_PASSWORD = '123' SHH_PORT = 22 DEBUG = True #代码调试,如果为True就会读取file中文件的内容,如果不是会执行linux

C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

转自原文C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子) 阅读目录 1.HtmlAgilityPack简介 2.XPath技术介绍与使用 3.采集天气网站案例 4.资源 第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel,是真尼玛的累,虽然那个时候C#还很菜,也想能不能通过程序来批量获取(所以平时想法要多才好).几

基于PHP采集数据入库程序(二)

在上篇基于PHP采集数据入库程序(二) 中提到采集新闻信息页的列表数据,接下来讲讲关于采集新闻具体内容 这是上篇博客的最终数据表截图: 接下来要做的操作就是从数据库中读取所需要采集的URL,进行页面抓取就行 新建一个content表 不过需要注意的一点是,不能再采用采集URL这种id递增的方法去采集,因为数据表中可能出现id断续,比如id=9,id=11,当采集到id=10的时候,URL是空白的,这样可能会导致采集到了空字段. 这里用到的一个技巧是数据库的查询语句,在我们采集完第一条数据的时候,

linux + ffmpeg + eclipse 调试

使用linux + ffmpeg + eclipse调试步骤OS : ubuntu 12.04Eclipse : 3.7.2 为Eclipse安装cdt插件,使其支持c/c++ 导入ffmpeg项目 File --> Import --> c / c++ --> Existing Code as Makefile Project 输入 Project Name, 选择ffmpeg所在目录, Toolchain选择Linux GCC 项目右键 --> Debug As -->