05-流媒体-摄像头采集YUV

整体方案:
采集端:摄像头采集(YUV)->编码(YUV转H264)->写封装(H264转FLV)->RTMP推流
客户端:RTMP拉流->解封装(FLV转H264)->解码(H264转YUV)->YUV显示(SDL2)
主程序:

#include <math.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavutil/frame.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavdevice/version.h>
#include<sys/types.h>
#include<sys/stat.h>
//#include<fchl.h>
#ifdef __cplusplus
}
#endif

#include "stdio.h"
#include "string.h"

 


//#include "ffmpeg_function.h"
void ffmpeg_video(void)
{
    char errors[1024]={0};
    //int file_fd=0;
    //create file
    FILE* file_fd=fopen("./video.yuv","wb");
    //ctx
    AVFormatContext *fmt_ctx=NULL;
    AVDictionary *option =NULL;
    //packet
    int count =0;
    AVPacket pkt;
    char devicename[1024]="/dev/video0";
    //注册设备
    avdevice_register_all();
    AVInputFormat *iformat=av_find_input_format("video4linux2");
    av_dict_set(&option,"video_size","640x480",0);
    av_dict_set(&option,"framerate","30",0);      //指定帧率
    av_dict_set(&option,"pixel_format","nv12",0); //指定格式
    //打开设备
    int ret=avformat_open_input(&fmt_ctx,devicename,iformat,&option);
    if (ret<0)
    {
        av_strerror(ret,errors,1024);
        return;
    }
    av_init_packet(&pkt);
    while (ret=av_read_frame(fmt_ctx,&pkt)==0&&count++<30)
    {
        printf("packet size is %d(%p),count=%d\n",pkt.size,pkt.data,count);
        fwrite((pkt.data), 1,pkt.size, file_fd);
    }
    av_packet_unref(&pkt); 
    fclose(file_fd);
    avformat_close_input(&fmt_ctx);
    av_log_set_level(AV_LOG_DEBUG);
    av_log( NULL, AV_LOG_DEBUG,"hello,word\n");
}


int main(int argc, char **argv)
{
    
	ffmpeg_video();
    return 0;
}

Makefile:

PRJ_PATH	= .
TARGETS = video_prj
DIR_INC = ./inc
DIR_SRC = ./src
DIR_OBJ = ./obj
CXX = g++
 
SRC := $(wildcard  ${DIR_SRC}/*.cpp)
OBJ := $(patsubst ${DIR_SRC}/%.cpp,$(DIR_OBJ)/%.o,$(SRC))
 
FFMPEG_LIB_DIR	= $(PRJ_PATH)/third_lib/ffmpeg/lib
FFMPEG_INC_DIR	= $(PRJ_PATH)/third_lib/ffmpeg/include
 
INCLUDES	= -I$(DIR_INC)
INCLUDES	+= -I$(FFMPEG_INC_DIR)

LDFLAGS = -ldl -lpthread -lm 
LDFLAGS += -lX11 -lasound -lmp3lame -lz -lrt
LDFLAGS += -L$(FFMPEG_LIB_DIR) -lavformat -lavdevice  -lavcodec -lavutil  -lavfilter -lpostproc  -lswresample -lswscale

CXXFLAGS = -fpermissive
CXXFLAGS += -fPIC 
 
CFLAGS += $(INCLUDES)
 
 
$(TARGETS):$(OBJ)
	$(CXX) $^ -o $@ $(LDFLAGS) $(CXXFLAGS)
	
$(DIR_OBJ)/%.o:$(DIR_SRC)/%.cpp
	$(CXX) $(CFLAGS) -c $< -o $@ $(CXXFLAGS)
clean:
	rm -f $(TARGETS)
	rm -f $(DIR_OBJ)/*.o

工程结构(详细的工程目录,参考YUV转H264章节):

$ tree .
.
├── inc
├── Makefile
├── obj
│   ├── dev2yuv.o
│   └── yuv2h264.o
├── src
│   └── dev2yuv.cpp
├── third_lib
│   └── ffmpeg
│       ├── include
│       │   ├── libavcodec
│       │   │   ├── ac3_parser.h
│       │   │   ├── adts_parser.h
│       │   │   ├── avcodec.h
│       │   │   ├── avdct.h
│       │   │   ├── avfft.h
│       │   │   ├── d3d11va.h
│       │   │   ├── dirac.h
│       │   │   ├── dv_profile.h
│       │   │   ├── dxva2.h
│       │   │   ├── jni.h
│       │   │   ├── mediacodec.h
│       │   │   ├── qsv.h
│       │   │   ├── vaapi.h
│       │   │   ├── vdpau.h
│       │   │   ├── version.h
│       │   │   ├── videotoolbox.h
│       │   │   ├── vorbis_parser.h
│       │   │   └── xvmc.h
│       │   ├── libavdevice
│       │   │   ├── avdevice.h
│       │   │   └── version.h
│       │   ├── libavfilter
│       │   │   ├── avfilter.h
│       │   │   ├── buffersink.h
│       │   │   ├── buffersrc.h
│       │   │   └── version.h
│       │   ├── libavformat
│       │   │   ├── avformat.h
│       │   │   ├── avio.h
│       │   │   └── version.h
│       │   ├── libavutil
│       │   │   ├── adler32.h
│       │   │   ├── aes_ctr.h
│       │   │   ├── aes.h
│       │   │   ├── attributes.h
│       │   │   ├── audio_fifo.h
│       │   │   ├── avassert.h
│       │   │   ├── avconfig.h
│       │   │   ├── avstring.h
│       │   │   ├── avutil.h
│       │   │   ├── base64.h
│       │   │   ├── blowfish.h
│       │   │   ├── bprint.h
│       │   │   ├── bswap.h
│       │   │   ├── buffer.h
│       │   │   ├── camellia.h
│       │   │   ├── cast5.h
│       │   │   ├── channel_layout.h
│       │   │   ├── common.h
│       │   │   ├── cpu.h
│       │   │   ├── crc.h
│       │   │   ├── des.h
│       │   │   ├── dict.h
│       │   │   ├── display.h
│       │   │   ├── downmix_info.h
│       │   │   ├── encryption_info.h
│       │   │   ├── error.h
│       │   │   ├── eval.h
│       │   │   ├── ffversion.h
│       │   │   ├── fifo.h
│       │   │   ├── file.h
│       │   │   ├── frame.h
│       │   │   ├── hash.h
│       │   │   ├── hdr_dynamic_metadata.h
│       │   │   ├── hmac.h
│       │   │   ├── hwcontext_cuda.h
│       │   │   ├── hwcontext_d3d11va.h
│       │   │   ├── hwcontext_drm.h
│       │   │   ├── hwcontext_dxva2.h
│       │   │   ├── hwcontext.h
│       │   │   ├── hwcontext_mediacodec.h
│       │   │   ├── hwcontext_qsv.h
│       │   │   ├── hwcontext_vaapi.h
│       │   │   ├── hwcontext_vdpau.h
│       │   │   ├── hwcontext_videotoolbox.h
│       │   │   ├── imgutils.h
│       │   │   ├── intfloat.h
│       │   │   ├── intreadwrite.h
│       │   │   ├── lfg.h
│       │   │   ├── log.h
│       │   │   ├── lzo.h
│       │   │   ├── macros.h
│       │   │   ├── mastering_display_metadata.h
│       │   │   ├── mathematics.h
│       │   │   ├── md5.h
│       │   │   ├── mem.h
│       │   │   ├── motion_vector.h
│       │   │   ├── murmur3.h
│       │   │   ├── opt.h
│       │   │   ├── parseutils.h
│       │   │   ├── pixdesc.h
│       │   │   ├── pixelutils.h
│       │   │   ├── pixfmt.h
│       │   │   ├── random_seed.h
│       │   │   ├── rational.h
│       │   │   ├── rc4.h
│       │   │   ├── replaygain.h
│       │   │   ├── ripemd.h
│       │   │   ├── samplefmt.h
│       │   │   ├── sha512.h
│       │   │   ├── sha.h
│       │   │   ├── spherical.h
│       │   │   ├── stereo3d.h
│       │   │   ├── tea.h
│       │   │   ├── threadmessage.h
│       │   │   ├── timecode.h
│       │   │   ├── time.h
│       │   │   ├── timestamp.h
│       │   │   ├── tree.h
│       │   │   ├── twofish.h
│       │   │   ├── tx.h
│       │   │   ├── version.h
│       │   │   └── xtea.h
│       │   ├── libpostproc
│       │   │   ├── postprocess.h
│       │   │   └── version.h
│       │   ├── libswresample
│       │   │   ├── swresample.h
│       │   │   └── version.h
│       │   └── libswscale
│       │       ├── swscale.h
│       │       └── version.h
│       └── lib
│           ├── libavcodec.so
│           ├── libavdevice.so
│           ├── libavfilter.so
│           ├── libavformat.so
│           ├── libavutil.so
│           ├── libpostproc.so
│           ├── libswresample.so
│           └── libswscale.so
└── video_prj
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值