FFmpeg 摄像头采集
FFmpeg 摄像头采集
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "SDL/SDL.h"
}; int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec; av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context(); //Register Device
avdevice_register_all(); // Win32
//Show Dshow Device
show_dshow_device();
{
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_devices","true",0);
AVInputFormat *iformat = av_find_input_format("dshow");
printf("========Device Info=============\n");
avformat_open_input(&pFormatCtx,"video=dummy",iformat,&options);
printf("================================\n");
}
//Show Device Options
show_dshow_device_option();
{
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_options","true",0);
AVInputFormat *iformat = av_find_input_format("dshow");
printf("========Device Option Info======\n");
avformat_open_input(&pFormatCtx,"video=Integrated Camera",iformat,&options);
printf("================================\n");
}
//Show VFW Options
show_vfw_device();
{
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVInputFormat *iformat = av_find_input_format("vfwcap");
printf("========VFW Device Info======\n");
avformat_open_input(&pFormatCtx,"list",iformat,NULL);
printf("=============================\n");
} //Show AVFoundation Device
void show_avfoundation_device()
{
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_devices","true",0);
AVInputFormat *iformat = av_find_input_format("avfoundation");
printf("==AVFoundation Device Info===\n");
avformat_open_input(&pFormatCtx,"",iformat,&options);
printf("=============================\n");
} // DSHOW
AVInputFormat *ifmt=av_find_input_format("dshow");
//Set own video device's name
if(avformat_open_input(&pFormatCtx,"video=Integrated Camera",ifmt,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
// VFW
AVInputFormat *ifmt=av_find_input_format("vfwcap");
if(avformat_open_input(&pFormatCtx,"0",ifmt,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
// LINUX
AVInputFormat *ifmt=av_find_input_format("video4linux2");
if(avformat_open_input(&pFormatCtx,"/dev/video0",ifmt,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
// MAC
show_avfoundation_device();
//Mac
AVInputFormat *ifmt=av_find_input_format("avfoundation");
//Avfoundation
//[video]:[audio]
if(avformat_open_input(&pFormatCtx,"0",ifmt,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
} 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("Couldn'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;
} AVFrame *pFrame,*pFrameYUV;
pFrame=av_frame_alloc();
pFrameYUV=av_frame_alloc(); struct SwsContext *img_convert_ctx;
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); for (;;) {
//Wait
SDL_WaitEvent(&event);
if(event.type==SFM_REFRESH_EVENT){
//------------------------------
if(av_read_frame(pFormatCtx, packet)>=0){
if(packet->stream_index==videoindex){
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < 0){
printf("Decode Error.\n");
return -1;
}
if(got_picture){
SDL_LockYUVOverlay(bmp);
pFrameYUV->data[0]=bmp->pixels[0];
pFrameYUV->data[1]=bmp->pixels[2];
pFrameYUV->data[2]=bmp->pixels[1];
pFrameYUV->linesize[0]=bmp->pitches[0];
pFrameYUV->linesize[1]=bmp->pitches[2];
pFrameYUV->linesize[2]=bmp->pitches[1];
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); SDL_UnlockYUVOverlay(bmp); SDL_DisplayYUVOverlay(bmp, &rect); }
}
av_free_packet(packet);
}else{
//Exit Thread
thread_exit=1;
break;
}
}else if(event.type==SDL_QUIT){
thread_exit=1;
break;
} } sws_freeContext(img_convert_ctx); //av_free(out_buffer);
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx); return 0;
}
FFmpeg 摄像头采集的更多相关文章
- ffmpeg摄像头采集h264编码RTP发送
一. 相关API说明 1. av_register_all 2. avformat_network_init 不管是流媒体发送还是流媒体接收, 需要先执行该函数. 3. avformat_alloc_ ...
- [转载] ffmpeg摄像头视频采集-采集步骤概述并采集一帧视频
近期由于工作任务,需要开发一个跨平台视频聊天系统,其中就用到了ffmpeg进行采集与编码,网上找了一大堆的资料,虽然都有一些有用的东西,但实在太碎片化了,这几天一直在整理和实验这些资料,边整理,边做一 ...
- 利用ffmpeg一步一步编程实现摄像头采集编码推流直播系统
了解过ffmpeg的人都知道,利用ffmpeg命令即可实现将电脑中摄像头的画面发布出去,例如发布为UDP,RTP,RTMP等,甚至可以发布为HLS,将m3u8文件和视频ts片段保存至Web服务器,普通 ...
- 如何用FFmpeg API采集摄像头视频和麦克风音频,并实现录制文件的功能
之前一直用Directshow技术采集摄像头数据,但是觉得涉及的细节比较多,要开发者比较了解Directshow的框架知识,学习起来有一点点难度.最近发现很多人问怎么用FFmpeg采集摄像头图像,事实 ...
- 基于opencv和mfc的摄像头采集代码(GOMFCTemplate2)
编写带界面的图像处理程序,选择opencv+mfc是一种很好的选择:在读取摄像头数据方面,网上的方法很多,其中shiqiyu的camerads的方法是较好的. 基于现有资料 ...
- dshow,Sample Grabber 从摄像头采集
char* CCameraDS::QueryFrame() { long evCode, size = 0; #if CALLBACKMODE static double lastSampleTime ...
- 基于opencv和mfc的摄像头采集代码(GOMFCTemplate2)持续更新
编写带界面的图像处理程序,选择opencv+mfc是一种很好的选择:在读取摄像头数据方面,网上的方法很多,其中shiqiyu的camerads的方法是较好的. 基于现有资料 ...
- Linux 下V4l2摄像头采集图片,实现yuyv转RGB,RGB转BMP,RGB伸缩,jpeglib 库实现压缩RGB到内存中,JPEG经UDP发送功(转)
./configure CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld --host=arm-linux --prefix=/usr/loca ...
- [转]GStreamer资料(摄像头采集,视频保存,远程监控)DVR
http://blog.csdn.net/wzwxiaozheng/archive/2010/12/26/6099397.aspx GStreamer资料整理(包括摄像头采集,视频保存,远程监控,流媒 ...
- QT学习之usb摄像头采集(Opencv+QT)[cvCapture,IplImage,QImage]
参考自:http://blog.chinaunix.net/uid-23381466-id-3826748.html 将Opencv中的cvCaptureFromCAM不断获得摄像头采集到的图像数据, ...
随机推荐
- hutool调用第三方接口上传文件和下载文件
1.上传文件接口例子(本地接口) 2.第三方接口 3.调用第三方接口下载文件 4.第三方接口 5.多个文件生成压缩文件
- jdbc中druid连接池遇到的问题和jdbcTemplate
无效的源发行版11 这是jdk版本不一致,去项目结构里排查一下 严重: init datasource error com.mysql.jdbc.exceptions.jdbc4.MySQLNonTr ...
- spark structured streaming (结构化流) join 操作( 官方文档翻译)
spark 结构化流 join 连接 结构化流支持将流dataset/DataFrame与静态dataset/DataFrame,或者另一个流数据集-DataFrame连接起来.流式连接的结果是增量生 ...
- SAP S/4HANA Cloud的功能亮点以及大中型企业为何更倾向选择它
SAP-System Applications and Products,是一家来自德国的大型跨国软件公司,成立于1972年.作为全球企业管理和协同化商务解决方案供应商,世界第三大独立软件供应商和全球 ...
- vscore 中 vim 常用快捷键
谷歌浏览器 ctrl + T 新建一个页面 ctrl + J 查看下载界面 F6 直接搜索 vscore 在 vscore 中使用 vim 建议去掉 ctrl 键的功能捆绑,不然会覆盖掉很多的 vsc ...
- selenium执行下载多个文件操作,谷歌浏览器弹出"xxx想要下载多个文件"的处理方法
背景: 使用selenium框架,批量下载多个目录的不同文件,而下载多个文件时,浏览器会弹出如下窗口 解决方案有2个:1.代码定位到元素并点击[允许].2.修改浏览器的设置,使其能够拥有自动下载的 ...
- pytest学习总结
官方pytest文档:Full pytest documentation - pytest documentation 一.pytest以及辅助插件的安装 1.pytest安装 pip install ...
- 【EBS】总账YTD\PTD等概念
PTD 余额是指给定期间账户中的净活动.周期通常等于一个月. YTD 余额是指账户从时间开始到给定期间结束的净活动. 例如 假设您在 1 月 15 日声明了一项业务.您在 1 月 15 日从现金账户中 ...
- linux 查看product_uuid,即主板id,机器识别码,需要root权限
1. 用命令 sudo dmidecode | grep UUID 或者 sudo lshw | grep uuid 2. 直接查看文件描述 sudo cat /sys/class/dmi/id/ ...
- eclipse (4.10.0)安装sts
1.离线安装 下载对应版本 https://spring.io/tools3/sts/all 打开Eclipse,点击help下的install new software,选择Add..,再点击Arc ...