FFMPEG可以直接使用摄像头录像,并支持对其进行编解码。

使用FFMPEG获取当前的摄像头设备

cmd命令:

ffmpeg -list_devices true -f dshow -i dummy

结果如下:

详细说明见上一篇文章链接:https://tonybi.blog.csdn.net/article/details/117849930

使用摄像头录像并编码成YUV420P格式

程序代码

#include <iostream>extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
}#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avdevice.lib")using namespace std;int main()
{av_register_all();avdevice_register_all();//获取设备,初始换上下文AVFormatContext* pFormat = NULL;AVInputFormat* ifmt = av_find_input_format("dshow");AVDictionary *dict = NULL;//设置参数(适合设备的处理能力)av_dict_set(&dict, "video_size", "320x180", 0);int ret = avformat_open_input(&pFormat, "video=Integrated Camera", ifmt, &dict);if (ret < 0){cout << "avformat_open_input error" << endl;}//查找流信息-获取音+视频的基本信息,宽高,duration,rate等ret = avformat_find_stream_info(pFormat, NULL);if (ret < 0){cout << "avformat_find_stream_info error:" << ret;return -1;}//获取流int avVideoStream = -1;int avAudioStream = -1;avVideoStream = av_find_best_stream(pFormat, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, NULL);//avAudio = av_find_best_stream(pFormat, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, NULL);AVCodecContext* videoCodecCtx = pFormat->streams[avVideoStream]->codec;//获取解码器AVCodec* videoCodec = avcodec_find_decoder(videoCodecCtx->codec_id);if (!videoCodec){cout << "avcodec_find_decoder error" << endl;return -1;}//打开编码器ret = avcodec_open2(videoCodecCtx, videoCodec, NULL);if (ret < 0){cout << "avcodec_open2" << endl;return -1;}//创建帧空间AVFrame* frame = av_frame_alloc();AVFrame* frameYUV = av_frame_alloc();int width = videoCodecCtx->width;int height = videoCodecCtx->height;cout << "input frame w=" << width << ", h=" << height << endl;AVPixelFormat fmt = (AVPixelFormat)videoCodecCtx->pix_fmt;int nSize = avpicture_get_size(AV_PIX_FMT_YUV420P, width, height);uint8_t* buff = (uint8_t*)av_malloc(nSize);avpicture_fill((AVPicture*)frameYUV, buff, AV_PIX_FMT_YUV420P, width, height);AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));SwsContext* swsCtx = sws_getContext(width, height, fmt, width, height, AV_PIX_FMT_YUV420P,SWS_BICUBIC, NULL, NULL, NULL);int go = 0;int frameIndex = 0;FILE* f1 = fopen("11.yuv", "wb+");while(av_read_frame(pFormat, packet) >= 0){frameIndex++;if (packet->stream_index == AVMEDIA_TYPE_VIDEO){ret = avcodec_decode_video2(videoCodecCtx, frame, &go, packet);if (ret < 0){cout << "avcodec_decode_video2 error" << endl;return -1;}if (go){sws_scale(swsCtx, (const uint8_t**)frame->data, frame->linesize, 0,height, frameYUV->data, frameYUV->linesize);int size = width * height;//写文件fwrite(frameYUV->data[0], 1, size, f1);fwrite(frameYUV->data[1], 1, size/4, f1);fwrite(frameYUV->data[2], 1, size/4, f1);cout << "frame index=" << frameIndex << ", size=" << size << endl;}}av_free_packet(packet);}fclose(f1);sws_freeContext(swsCtx);av_frame_free(&frame);av_frame_free(&frameYUV);avformat_close_input(&pFormat);return 0;
}

运行结果

最后,在程序运行目录下找yuv文件,使用播放器进行播放。

FFMPEG使用摄像头录像并编码相关推荐

  1. linux下使用ffmpeg采集摄像头数据并编码成h264文件

    本文讲述如何在linux下,使用ffmpeg采集视频数据,并编码成h264文件. 打算分成3部分讲解: 需要具备的软硬件环境 ffmpeg命令采集摄像头数据并编码成h264文件 ffmpeg代码采集摄 ...

  2. 利用ffmpeg进行摄像头提取视频编码为h264通过RTP发送数据到指定的rtp地址

    话不多说命令如下: ffmpeg -f dshow -i video="Logitech QuickCam Easy/Cool" -vcodec libx264 -preset:v ...

  3. ffmpeg推流摄像头数据至公网服务器

    完整的推流代码已经托管到个人的Gitee,如有需要请自取 https://gitee.com/MonsterAKALei/push_video.git ffmpeg推流摄像头数据 昨天实现用API分别 ...

  4. python调用摄像头做监控_《自拍教程68》Python + ffmpeg调用摄像头,实现自动化监控录像...

    案例故事: 场景一:反复重启Android终端产品100次,每重启一次录一个视频; 场景二:做压力测试比如Monkey一晚上,我们需要涉及长时间录像; 场景三:做自动化测试的时候,跑一条自动化用例,录 ...

  5. FFMPEG采集摄像头图像SDL渲染+MP4格式视频编码

    FFMPEG采集摄像头图像SDL渲染+MP4格式视频编码 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音 ...

  6. Qt实现摄像头视频h264编码和拍照功能

    Buildroot+Qt实现摄像头视频h264编码和拍照功能 https://gitee.com/zy853728579/video_test_app.git 实现h264硬编码+拍照功能,以下为关健 ...

  7. 摄像头录像以及回放工具

    很久以前就想做这一项目,但限于技术不到位,迟迟没有完成:后来因为工作上一个项目涉及到多媒体实时传输播放,利用ffmpeg这个库,很方便快速的完成了任务,尽管熟悉ffmpeg的过程稍微痛苦了点.当时项目 ...

  8. FFmpeg入门详解之122:Qt5 FFmpeg本地摄像头采集预览实战

    6.Qt5+FFmpeg本地摄像头采集预览实战 源码工程:S26_Test2 FFmpeg命令行处理摄像头 ffmpeg -list_devices true -f dshow -i dummy 命令 ...

  9. 摄像头录像 及视频保存压缩

    c# 摄像头录像 及视频保存压缩等 收藏 using System; using System.Collections.Generic; using System.Linq; using System ...

  10. 最简单的基于FFMPEG的视频编码器(YUV编码为H.264)

    ===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...

最新文章

  1. 「图像分类」从数据集和经典网络开始
  2. 机器学习(MACHINE LEARNING) 【周志华版-”西瓜书“-笔记】 DAY5-神经网络
  3. Cookie 和 Session的区别
  4. 【喜报】2016 年度最受欢迎中国开源软件TOP20出炉——JEECG、JEEWX双入围!
  5. Python项目实战:带领你爬取sexy的福利图片
  6. 数学建模方法自己归纳总结(建模参考用,包含相应例题以及MATLAB代码)
  7. 统计调查制度申请流程和申请书公文模板
  8. 用cube移植PS2手柄--HAL库
  9. WinZip for Mac注册版
  10. Android音频压缩分析
  11. 优动漫PAINT入门宝典(图层篇)——矢量图层
  12. android id设备认证失败,教大家Apple ID验证失败发生未知错误的解决方法
  13. java释放线程资源_Java线程之释放锁,释放资源,释放CPU
  14. 形式化方法 | Proof Engineering for Predicate Logic——Coq tatics 在谓词逻辑证明中的应用
  15. 407. 接雨水 II【我亦无他唯手熟尔】
  16. springboot 启动banner 打印 佛祖保佑
  17. 视线估计(Gaze Estimation)简介概述
  18. opencv实现camshift算法,以及代码详解
  19. torch.cat()函数的官方解释,详解以及例子
  20. 如何降低研究生硕士论文的查重率

热门文章

  1. CTF_ctfshow_meng新_web1-web24
  2. 压箱底的10款在线工具平台
  3. unity, Animation crossfade需要两动画在时间上确实有交叠
  4. 为什么 fac_us=SystemCoreClock/8000000?
  5. Latex给表格加脚注
  6. 云队友丨张朝阳不再狂妄,搜狐的艰难复苏路
  7. 照相馆、摄影工作室如何利用选片和底片下载来做微信公众号吸粉
  8. 网站改造为百度智能小程序教程,适合所有网站封装
  9. Linux内核文件系统7
  10. oracle 打包导出表,数据库导出表数据库