0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux小項(xiàng)目-行車記錄儀項(xiàng)目設(shè)計(jì)

DS小龍哥-嵌入式技術(shù) ? 來源:DS小龍哥-嵌入式技術(shù) ? 作者:DS小龍哥-嵌入式技 ? 2022-08-14 09:15 ? 次閱讀

1. 前言

行車記錄這個(gè)設(shè)備相信大家應(yīng)該都不陌生,它的功能主要是記錄車輛行駛途中的影像及聲音。

安裝行車記錄儀后,能夠記錄汽車行駛?cè)^程的視頻圖像和聲音,可為交通事故提供證據(jù),喜歡自駕游的人,還可以用它來記錄征服艱難險(xiǎn)阻的過程。開車時(shí)邊走邊錄像,同時(shí)把時(shí)間、速度、所在位置都記錄在錄像里,相當(dāng)黑匣子。現(xiàn)在橫穿馬路的行車、摩托車,不交通規(guī)則形勢(shì)的汽車也經(jīng)常遇到,萬一和他們產(chǎn)生了刮碰,有可能會(huì)被敲詐勒索,如果有了行車記錄儀,司機(jī)可為自己提供有效的證據(jù)。

這篇文章就介紹在Linux最小系統(tǒng)開發(fā)板上如何實(shí)現(xiàn)行車記錄儀的功能,開發(fā)板自帶了8G的EMMC,也可以外擴(kuò)SD卡。

首先,在設(shè)計(jì)行車記錄儀這個(gè)項(xiàng)目之前,要先了解清楚行車記錄儀的功能。

(1)行車記錄運(yùn)行起來后,需要間隔循環(huán)錄制視頻保存,一般是1~10分鐘一段視頻,這樣設(shè)計(jì)的原理是方便按時(shí)間查找視頻,也防止以為情況損壞視頻編碼, 導(dǎo)致視頻無法正常播放。

(2)當(dāng)車輛發(fā)生碰撞、急剎車等緊急情況下,自動(dòng)錄制視頻當(dāng)前時(shí)間段視頻保存,方便后續(xù)直接查看。這個(gè)功能需要加速度計(jì)的支持,檢測(cè)車輛的緊急剎車,碰撞等姿態(tài)。

當(dāng)前項(xiàng)目里攝像頭采用USB免驅(qū)攝像頭替代,視頻編碼功能采用ffmpeg實(shí)現(xiàn),所以需要交叉編譯ffmpeg到嵌入式開發(fā)板上。

image-20220123230246919image-20220123230305094

2. ffmpeg的交叉編譯

ffmpeg下載地址: www.ffmpeg.org/download.ht…

image-20220123224243484
[root@xl ffmpeg]# tar xvf ffmpeg-3.0.2.tar.bz2 
?
[root@xl ffmpeg]# ./configure --disable-shared --enable-static --prefix=_install --cross-prefix=/work/arm-linux-gcc/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux- --arch=arm --target-os=linux --enable-gpl --disable-bzlib --disable-zlib --extra-cflags=-I/work/ffmpeg/x264/x264-snapshot-20160527-2245/_install/include/ --extra-ldflags=-L/work/ffmpeg/x264/x264-snapshot-20160527-2245/_install/lib --enable-ffserver --enable-ffmpeg --enable-libx264
?
[root@xl ffmpeg-3.0.2]# make && make install
復(fù)制代碼

編譯ffmpeg還需要x264的庫,編譯ffmpeg前,需要先編譯x264庫。

x264庫下載地址: www.videolan.org/developers/…

image-20220123225249487

編譯的步驟如下:

[root@xl x264]# tar -xjvf last_x264.tar.bz2 
?
[root@xl x264-snapshot-20160527-2245]# ./configure --prefix=$PWD/_install --disable-asm
?
修改配置文件
[root@xl x264-snapshot-20160527-2245]# gedit config.mak 
?
SYS_ARCH=X86改為ARCH=ARM
CC=gcc改為CC=arm-linux-gcc
LD=gcc -o改為LD=arm-linux-gcc -o
RANLIB=ranlib改為RANLIB=arm-linux-ranlib
AR=ar rc 改為AR=arm-linux-ar rc 
?
取消兩個(gè)選項(xiàng)
CFLAGS=-Wshadow -O3 -ffast-math -m32 -Wall -I. -I$(SRCPATH) -std=gnu99 -D_GNU_SOURCE -mpreferred-stack-boundary=5 -fomit-frame-pointer -fno-tree-vectorize
改為
CFLAGS=-Wshadow -O3 -ffast-math -Wall -I. -I$(SRCPATH) -std=gnu99 -D_GNU_SOURCE -fomit-frame-pointer -fno-tree-vectorize
?
?
[root@xl x264-snapshot-20160527-2245]# make && make install
復(fù)制代碼

3. 項(xiàng)目代碼

下面的代碼較多,直接將整份代碼放在一個(gè).c文件里,關(guān)于功能的解釋在代碼里都寫了注釋。

涉及到的技術(shù)有:ffmpeg的編碼錄制、聲卡PCM數(shù)據(jù)采集,USB攝像頭數(shù)據(jù)采集。

聲卡的采集采用了alsa框架接口,USB攝像頭使用的是V4L2框架接口。

下面代碼實(shí)現(xiàn)的功能是10秒錄制一段視頻保存,視頻文件名稱使用時(shí)間命名,只是貼出了應(yīng)用層的代碼,加速度計(jì)mma766的驅(qū)動(dòng)代碼在上幾篇文章里已經(jīng)講過了,這里就不再重復(fù)貼出來了。

具體實(shí)現(xiàn)的代碼如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

#define STREAM_DURATION   10.0   /*錄制10秒的視頻,由于緩沖的原因,一般只有8秒*/
#define STREAM_FRAME_RATE 15     /* 15 images/s   avfilter_get_by_name */
#define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
#define SCALE_FLAGS SWS_BICUBIC

//固定攝像頭輸出畫面的尺寸
#define VIDEO_WIDTH  640
#define VIDEO_HEIGHT 480

//存放從攝像頭讀出轉(zhuǎn)換之后的數(shù)據(jù)
unsigned char YUV420P_Buffer[VIDEO_WIDTH*VIDEO_HEIGHT*3/2];
unsigned char YUV420P_Buffer_temp[VIDEO_WIDTH*VIDEO_HEIGHT*3/2];

/*一些攝像頭需要使用的全局變量*/
unsigned char *image_buffer[4];
int video_fd;
pthread_mutex_t mutex;
pthread_cond_t cond;

/*一些audio需要使用的全局變量*/
pthread_mutex_t mutex_audio;

extern int capture_audio_data_init( char *audio_dev);
extern int capture_audio_data(snd_pcm_t *capture_handle,int buffer_frames);
/*
 進(jìn)行音頻采集,采集pcm數(shù)據(jù)并直接保存pcm數(shù)據(jù)
 音頻參數(shù): 
	 聲道數(shù):		2
	 采樣位數(shù):	16bit、LE格式
	 采樣頻率:	44100Hz
*/
#define AudioFormat SND_PCM_FORMAT_S16_LE  //指定音頻的格式,其他常用格式:SND_PCM_FORMAT_U24_LE、SND_PCM_FORMAT_U32_LE
#define AUDIO_CHANNEL_SET   1  			  //1單聲道   2立體聲
#define AUDIO_RATE_SET 44100   //音頻采樣率,常用的采樣頻率: 44100Hz 、16000HZ、8000HZ、48000HZ、22050HZ
FILE *pcm_data_file=NULL;

int buffer_frames;
snd_pcm_t *capture_handle;
snd_pcm_format_t format=AudioFormat;


//保存音頻數(shù)據(jù)鏈表
struct AUDIO_DATA
{
	unsigned char* audio_buffer;
	struct AUDIO_DATA *next;
};

//定義一個(gè)鏈表頭
struct AUDIO_DATA *list_head=NULL;
struct AUDIO_DATA *List_CreateHead(struct AUDIO_DATA *head);
void List_AddNode(struct AUDIO_DATA *head,unsigned char* audio_buffer);
void List_DelNode(struct AUDIO_DATA *head,unsigned char* audio_buffer);
int List_GetNodeCnt(struct AUDIO_DATA *head);

// 單個(gè)輸出AVStream的包裝器
typedef struct OutputStream {
    AVStream *st;
    AVCodecContext *enc;

    /* 下一幀的點(diǎn)數(shù)*/
    int64_t next_pts;
    int samples_count;

    AVFrame *frame;
    AVFrame *tmp_frame;

    float t, tincr, tincr2;

    struct SwsContext *sws_ctx;
    struct SwrContext *swr_ctx;
} OutputStream;


static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
    /*將輸出數(shù)據(jù)包時(shí)間戳值從編解碼器重新調(diào)整為流時(shí)基 */
    av_packet_rescale_ts(pkt, *time_base, st->time_base);
    pkt->stream_index = st->index;
		
	/*將壓縮的幀寫入媒體文件*/
    return av_interleaved_write_frame(fmt_ctx, pkt);
}


/* 添加輸出流。 */
static void add_stream(OutputStream *ost, AVFormatContext *oc,
                       AVCodec **codec,
                       enum AVCodecID codec_id)
{
    AVCodecContext *c;
    int i;

    /* find the encoder */
    *codec = avcodec_find_encoder(codec_id);
    if (!(*codec)) {
        fprintf(stderr, "Could not find encoder for '%s'\n",
                avcodec_get_name(codec_id));
        exit(1);
    }

    ost->st = avformat_new_stream(oc, NULL);
    if (!ost->st) {
        fprintf(stderr, "Could not allocate stream\n");
        exit(1);
    }
    ost->st->id = oc->nb_streams-1;
    c = avcodec_alloc_context3(*codec);
    if (!c) {
        fprintf(stderr, "Could not alloc an encoding context\n");
        exit(1);
    }
    ost->enc = c;

    switch ((*codec)->type) {
    case AVMEDIA_TYPE_AUDIO:
        c->sample_fmt  = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
        c->bit_rate    = 64000;  //設(shè)置碼率
        c->sample_rate = 44100;  //音頻采樣率
        c->channels= av_get_channel_layout_nb_channels(c->channel_layout);
        c->channel_layout = AV_CH_LAYOUT_MONO; ////AV_CH_LAYOUT_MONO 單聲道   AV_CH_LAYOUT_STEREO 立體聲
        c->channels        = av_get_channel_layout_nb_channels(c->channel_layout);
        ost->st->time_base = (AVRational){ 1, c->sample_rate };
        break;

    case AVMEDIA_TYPE_VIDEO:
        c->codec_id = codec_id;
		//碼率:影響體積,與體積成正比:碼率越大,體積越大;碼率越小,體積越小。
        c->bit_rate = 400000; //設(shè)置碼率 400kps
        /*分辨率必須是2的倍數(shù)。 */
        c->width    =VIDEO_WIDTH;
        c->height   = VIDEO_HEIGHT;
        /*時(shí)基:這是基本的時(shí)間單位(以秒為單位)
		 *表示其中的幀時(shí)間戳。 對(duì)于固定fps內(nèi)容,
		 *時(shí)基應(yīng)為1 / framerate,時(shí)間戳增量應(yīng)為
		 *等于1。*/
        ost->st->time_base = (AVRational){1,STREAM_FRAME_RATE};
        c->time_base       = ost->st->time_base;
        c->gop_size      = 12; /* 最多每十二幀發(fā)射一幀內(nèi)幀 */
        c->pix_fmt       = STREAM_PIX_FMT;
        c->max_b_frames = 0;  //不要B幀
        if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) 
		{
            c->mb_decision = 2;
        }
    break;

    default:
        break;
    }

    /* 某些格式希望流頭分開。 */
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
        c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}

/**************************************************************/
/* audio output */

static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
                                  uint64_t channel_layout,
                                  int sample_rate, int nb_samples)
{
    AVFrame *frame = av_frame_alloc();
    frame->format = sample_fmt;
    frame->channel_layout = channel_layout;
    frame->sample_rate = sample_rate;
    frame->nb_samples = nb_samples;
    if(nb_samples)
	{
        av_frame_get_buffer(frame, 0);
    }
    return frame;
}

static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
    AVCodecContext *c;
    int nb_samples;
    int ret;
    AVDictionary *opt = NULL;
    c = ost->enc;
    av_dict_copy(&opt, opt_arg, 0);
    ret = avcodec_open2(c, codec, &opt);
    av_dict_free(&opt);
	

    /*下面3行代碼是為了生成虛擬的聲音設(shè)置的頻率參數(shù)*/
    ost->t     = 0;
    ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
    ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;

	//AAC編碼這里就固定為1024
    nb_samples = c->frame_size;

    ost->frame     = alloc_audio_frame(c->sample_fmt, c->channel_layout,
                                       c->sample_rate, nb_samples);
    ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
                                       c->sample_rate, nb_samples);

    /* copy the stream parameters to the muxer */
    avcodec_parameters_from_context(ost->st->codecpar, c);

    /* create resampler context */
    ost->swr_ctx = swr_alloc();

	/* set options */
    printf("c->channels=%d\n",c->channels);
	av_opt_set_int       (ost->swr_ctx, "in_channel_count",   c->channels,       0);
	av_opt_set_int       (ost->swr_ctx, "in_sample_rate",     c->sample_rate,    0);
	av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt",      AV_SAMPLE_FMT_S16, 0);
	av_opt_set_int       (ost->swr_ctx, "out_channel_count",  c->channels,       0);
	av_opt_set_int       (ost->swr_ctx, "out_sample_rate",    c->sample_rate,    0);
	av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt",     c->sample_fmt,     0);
    
	/* initialize the resampling context */
	swr_init(ost->swr_ctx);
}

/* 毫秒級(jí) 延時(shí) */
void Sleep(int ms)
{
	struct timeval delay;
	delay.tv_sec = 0;
	delay.tv_usec = ms * 1000; // 20 ms
	select(0, NULL, NULL, NULL, &delay);
}


/*
準(zhǔn)備虛擬音頻幀
這里可以替換成從聲卡讀取的PCM數(shù)據(jù)
*/
static AVFrame *get_audio_frame(OutputStream *ost)
{
    AVFrame *frame = ost->tmp_frame;
    int j, i, v;
    int16_t *q = (int16_t*)frame->data[0];
    /* 檢查我們是否要生成更多幀,用于判斷是否結(jié)束*/
    if (av_compare_ts(ost->next_pts, ost->enc->time_base,STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
        return NULL;

   #if 1
	//獲取鏈表節(jié)點(diǎn)數(shù)量
	int cnt=0;
	while(cnt<=0)
	{
		cnt=List_GetNodeCnt(list_head);
	}
	
	pthread_mutex_lock(&mutex_audio); /*互斥鎖上鎖*/

	//得到節(jié)點(diǎn)數(shù)據(jù)
	struct AUDIO_DATA *tmp=list_head;
	unsigned char *buffer;

	tmp=tmp->next;
	if(tmp==NULL)
	{
		printf("數(shù)據(jù)為NULL.\n");
		exit(0);
	}
	buffer=tmp->audio_buffer;
	
	//1024*16*1
	memcpy(q,buffer,frame->nb_samples*sizeof(int16_t)*ost->enc->channels);//將音頻數(shù)據(jù)拷貝進(jìn)入frame緩沖區(qū)
	
	List_DelNode(list_head,buffer);
	free(buffer);			
    pthread_mutex_unlock(&mutex_audio); /*互斥鎖解鎖*/
	#endif
	
    frame->pts = ost->next_pts;
    ost->next_pts  += frame->nb_samples;
    return frame;
}


/*
 *編碼一個(gè)音頻幀并將其發(fā)送到多路復(fù)用器
 *編碼完成后返回1,否則返回0
 */
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
{
    AVCodecContext *c;
    AVPacket pkt = { 0 };
    AVFrame *frame;
    int ret;
    int got_packet;
    int dst_nb_samples;

    av_init_packet(&pkt);
    c = ost->enc;

    frame = get_audio_frame(ost);

    if(frame)
	{
        /*使用重采樣器將樣本從本機(jī)格式轉(zhuǎn)換為目標(biāo)編解碼器格式*/
		 /*計(jì)算樣本的目標(biāo)數(shù)量*/
		dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
										c->sample_rate, c->sample_rate, AV_ROUND_UP);
		av_assert0(dst_nb_samples == frame->nb_samples);
        av_frame_make_writable(ost->frame);
        /*轉(zhuǎn)換為目標(biāo)格式 */
        swr_convert(ost->swr_ctx,
                    ost->frame->data, dst_nb_samples,
                    (const uint8_t **)frame->data, frame->nb_samples);
        frame = ost->frame;
        frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
        ost->samples_count += dst_nb_samples;
    }

    avcodec_encode_audio2(c, &pkt, frame, &got_packet);

    if (got_packet) 
	{
        write_frame(oc, &c->time_base, ost->st, &pkt);
    }
    return (frame || got_packet) ? 0 : 1;
}


static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
    AVFrame *picture;
    int ret;
    picture = av_frame_alloc();
    picture->format = pix_fmt;
    picture->width  = width;
    picture->height = height;

    /* allocate the buffers for the frame data */
    av_frame_get_buffer(picture, 32);
    return picture;
}


static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
    AVCodecContext *c = ost->enc;
    AVDictionary *opt = NULL;
    av_dict_copy(&opt, opt_arg, 0);
    /* open the codec */
    avcodec_open2(c, codec, &opt);
    av_dict_free(&opt);
    /* allocate and init a re-usable frame */
    ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
    ost->tmp_frame = NULL;
    /* 將流參數(shù)復(fù)制到多路復(fù)用器 */
    avcodec_parameters_from_context(ost->st->codecpar, c);
}


/*
準(zhǔn)備圖像數(shù)據(jù)
YUV422占用內(nèi)存空間 = w * h * 2
YUV420占用內(nèi)存空間 = width*height*3/2
*/
static void fill_yuv_image(AVFrame *pict, int frame_index,int width, int height)
{
	int y_size=width*height;
	/*等待條件成立*/
	pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond,&mutex);
	memcpy(YUV420P_Buffer_temp,YUV420P_Buffer,sizeof(YUV420P_Buffer));
	/*互斥鎖解鎖*/
	pthread_mutex_unlock(&mutex);
	
    //將YUV數(shù)據(jù)拷貝到緩沖區(qū)  y_size=wXh
	memcpy(pict->data[0],YUV420P_Buffer_temp,y_size);
	memcpy(pict->data[1],YUV420P_Buffer_temp+y_size,y_size/4);
	memcpy(pict->data[2],YUV420P_Buffer_temp+y_size+y_size/4,y_size/4);
}


static AVFrame *get_video_frame(OutputStream *ost)
{
    AVCodecContext *c = ost->enc;

    /* 檢查我們是否要生成更多幀---判斷是否結(jié)束錄制 */
      if(av_compare_ts(ost->next_pts, c->time_base,STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
        return NULL;

    /*當(dāng)我們將幀傳遞給編碼器時(shí),它可能會(huì)保留對(duì)它的引用
    *內(nèi)部; 確保我們?cè)谶@里不覆蓋它*/
    if (av_frame_make_writable(ost->frame) < 0)
        exit(1);

	//制作虛擬圖像
	//DTS(解碼時(shí)間戳)和PTS(顯示時(shí)間戳)
    fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
    ost->frame->pts = ost->next_pts++;
    return ost->frame;
}

/*
*編碼一個(gè)視頻幀并將其發(fā)送到多路復(fù)用器
*編碼完成后返回1,否則返回0
*/
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
{
    int ret;
    AVCodecContext *c;
    AVFrame *frame;
    int got_packet = 0;
    AVPacket pkt = { 0 };
    c=ost->enc;
	//獲取一幀數(shù)據(jù)
    frame = get_video_frame(ost);
    av_init_packet(&pkt);

    /* 編碼圖像 */
    ret=avcodec_encode_video2(c, &pkt, frame, &got_packet);

    if(got_packet) 
	{
        ret=write_frame(oc, &c->time_base, ost->st, &pkt);
    }
	else
    {
        ret = 0;
    }
    return (frame || got_packet) ? 0 : 1;
}


static void close_stream(AVFormatContext *oc, OutputStream *ost)
{
    avcodec_free_context(&ost->enc);
    av_frame_free(&ost->frame);
    av_frame_free(&ost->tmp_frame);
    sws_freeContext(ost->sws_ctx);
    swr_free(&ost->swr_ctx);
}


//編碼視頻和音頻
int video_audio_encode(char *filename)
{
    OutputStream video_st = { 0 }, audio_st = { 0 };
    AVOutputFormat *fmt;
    AVFormatContext *oc;
    AVCodec *audio_codec, *video_codec;
    int ret;
    int have_video = 0, have_audio = 0;
    int encode_video = 0, encode_audio = 0;
    AVDictionary *opt = NULL;
    int i;

    /* 分配輸出環(huán)境 */
    avformat_alloc_output_context2(&oc,NULL,NULL,filename);
    fmt=oc->oformat;
	
     /*使用默認(rèn)格式的編解碼器添加音頻和視頻流,初始化編解碼器。 */
    if(fmt->video_codec != AV_CODEC_ID_NONE)
	{
        add_stream(&video_st,oc,&video_codec,fmt->video_codec);
        have_video = 1;
        encode_video = 1;
    }
    if(fmt->audio_codec != AV_CODEC_ID_NONE)
	{
        add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
        have_audio = 1;
        encode_audio = 1;
    }

  /*現(xiàn)在已經(jīng)設(shè)置了所有參數(shù),可以打開音頻視頻編解碼器,并分配必要的編碼緩沖區(qū)。 */
    if (have_video)
        open_video(oc, video_codec, &video_st, opt);

    if (have_audio)
        open_audio(oc, audio_codec, &audio_st, opt);

    av_dump_format(oc, 0, filename, 1);

    /* 打開輸出文件(如果需要) */
    if(!(fmt->flags & AVFMT_NOFILE)) 
	{
        ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0)
		{
            fprintf(stderr, "無法打開輸出文件: '%s': %s\n", filename,av_err2str(ret));
            return 1;
        }
    }

    /* 編寫流頭(如果有)*/
    avformat_write_header(oc,&opt);

    while(encode_video || encode_audio)
	{
        /* 選擇要編碼的流*/
        if(encode_video &&(!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,audio_st.next_pts, audio_st.enc->time_base) <= 0))
        {
			//printf("視頻編碼一次----->\n");
            encode_video = !write_video_frame(oc,&video_st);
        }
		else 
		{
			//printf("音頻編碼一次----->\n");
            encode_audio = !write_audio_frame(oc,&audio_st);
        }
    }
	
    av_write_trailer(oc);
	
    if (have_video)
        close_stream(oc, &video_st);
    if (have_audio)
        close_stream(oc, &audio_st);

    if (!(fmt->flags & AVFMT_NOFILE))
        avio_closep(&oc->pb);
    avformat_free_context(oc);
    return 0;
}


/*
函數(shù)功能: 攝像頭設(shè)備初始化
*/
int VideoDeviceInit(char *DEVICE_NAME)
{
	/*1. 打開攝像頭設(shè)備*/
	video_fd=open(DEVICE_NAME,O_RDWR);
	if(video_fd<0)return -1;

	/*2. 設(shè)置攝像頭支持的顏色格式和輸出的圖像尺寸*/
	struct v4l2_format video_formt;
	memset(&video_formt,0,sizeof(struct v4l2_format));	
	video_formt.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視頻捕獲設(shè)備*/
	video_formt.fmt.pix.height=VIDEO_HEIGHT; //480 
	video_formt.fmt.pix.width=VIDEO_WIDTH; //640
	video_formt.fmt.pix.pixelformat=V4L2_PIX_FMT_YUYV;
	if(ioctl(video_fd,VIDIOC_S_FMT,&video_formt))return -2;
	printf("當(dāng)前攝像頭尺寸:width*height=%d*%d\n",video_formt.fmt.pix.width,video_formt.fmt.pix.height);
	
	/*3.請(qǐng)求申請(qǐng)緩沖區(qū)的數(shù)量*/
	struct v4l2_requestbuffers video_requestbuffers;
	memset(&video_requestbuffers,0,sizeof(struct v4l2_requestbuffers));	
	video_requestbuffers.count=4;
	video_requestbuffers.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; /*視頻捕獲設(shè)備*/
	video_requestbuffers.memory=V4L2_MEMORY_MMAP;
	if(ioctl(video_fd,VIDIOC_REQBUFS,&video_requestbuffers))return -3;
	printf("video_requestbuffers.count=%d\n",video_requestbuffers.count);

	/*4. 獲取緩沖區(qū)的首地址*/
	struct v4l2_buffer video_buffer;
	memset(&video_buffer,0,sizeof(struct v4l2_buffer));
	int i;
	for(i=0;i;i++)>

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11161

    瀏覽量

    208460
  • 開發(fā)板
    +關(guān)注

    關(guān)注

    25

    文章

    4834

    瀏覽量

    96821
  • 行車記錄儀
    +關(guān)注

    關(guān)注

    15

    文章

    157

    瀏覽量

    45623
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    行車記錄儀--安霸方案

    采用美國安霸高清芯片A2S50,A2S60,A2S70 ,A5L, A5S主控芯片,同時(shí)也是安霸代理。方案名稱:行車記錄儀, 汽車黑匣子,仿真設(shè)計(jì)高清行車記錄儀,運(yùn)動(dòng)DV,高清攝像機(jī)(
    發(fā)表于 11-17 12:47

    【W(wǎng)RTnode2R申請(qǐng)】多功能行車記錄儀

    申請(qǐng)理由:行車記錄儀數(shù)據(jù)云端存儲(chǔ)項(xiàng)目描述:多功能行車記錄儀將車輛的速度、加速度、車載錄像上傳至云端,可調(diào)取數(shù)據(jù)監(jiān)視車輛的運(yùn)行狀態(tài)
    發(fā)表于 09-10 11:28

    【GoKit申請(qǐng)】多功能行車記錄儀

    申請(qǐng)理由:數(shù)據(jù)云端存儲(chǔ)項(xiàng)目描述:將多功能行車記錄儀里的車數(shù)、加速度、車載記錄視頻數(shù)據(jù)云端存儲(chǔ),可以進(jìn)行調(diào)用數(shù)據(jù)對(duì)車輛狀態(tài)進(jìn)行分析
    發(fā)表于 09-22 09:53

    【BPI-M64試用體驗(yàn)】基于Android系統(tǒng)的汽車導(dǎo)航和行車記錄儀項(xiàng)目(實(shí)現(xiàn)行車記錄儀功能)

    本帖最后由 xintuxing 于 2017-5-19 22:34 編輯 在開發(fā)板安裝了Android系統(tǒng)后,實(shí)現(xiàn)行車記錄儀功能是很簡(jiǎn)單的,就差一個(gè)應(yīng)用軟件了。下面是我實(shí)現(xiàn)此功能的經(jīng)過:剛開始
    發(fā)表于 05-19 22:36

    行車記錄儀的OLED屏幕

    `<p>隨著生活質(zhì)量的提高,車友們對(duì)汽車駕駛的安全性、娛樂性有了更高需求。汽車互聯(lián)網(wǎng)的時(shí)代,行車記錄儀是汽車用品不可缺少的一部分,而凌度第三代聯(lián)網(wǎng)記錄儀是互聯(lián)網(wǎng)與智能行車
    發(fā)表于 12-25 16:36

    【HarmonyOS HiSpark Wi-Fi IoT HarmonyOS 智能家居套件試用 】行車記錄儀

    項(xiàng)目名稱:行車記錄儀試用計(jì)劃:完成行車記錄儀的代碼編輯
    發(fā)表于 10-29 14:11

    【HarmonyOS HiSpark AI Camera】智能行車記錄儀

    項(xiàng)目名稱:智能行車記錄儀試用計(jì)劃:申請(qǐng)理由與內(nèi)容:1. 曾有過使用Hi3516DV300基于Linux系統(tǒng)開發(fā)戶外攝像頭經(jīng)歷2. 這次想加入車牌識(shí)別和汽車顏色識(shí)別功能3. 操作系統(tǒng)更換
    發(fā)表于 11-19 20:47

    教你如何選購行車記錄儀

    國內(nèi)的行車記錄儀市場(chǎng)魚龍混雜,山寨貨橫行。山寨貨利用低廉的價(jià)格以及湊合能用的功能,成為了行車記錄儀市場(chǎng)的銷售主力。國內(nèi)大型車載電子用品生產(chǎn)商生產(chǎn)的
    發(fā)表于 07-05 09:55 ?2.9w次閱讀
    教你如何選購<b class='flag-5'>行車</b><b class='flag-5'>記錄儀</b>

    行車記錄儀推薦_行車記錄儀什么牌子好

    現(xiàn)在家用汽車越來越多了,如果在開車時(shí)遇到了事故或是有意外的事情發(fā)生,這種情況下行車記錄儀就有了用武之地了。行車記錄儀相當(dāng)于飛機(jī)的黑盒子,可以在駕駛的同時(shí)通過攝影記
    發(fā)表于 10-13 10:16 ?9.6w次閱讀
    <b class='flag-5'>行車</b><b class='flag-5'>記錄儀</b>推薦_<b class='flag-5'>行車</b><b class='flag-5'>記錄儀</b>什么牌子好

    行車記錄儀有什么用_智能手機(jī)DIY行車記錄儀教程

    隨著家用汽車的越來越多,行車記錄儀隨之也產(chǎn)生了,很多車主已經(jīng)開始使用行車記錄儀了。但有很多車主對(duì)行車記錄
    發(fā)表于 10-13 11:50 ?1.2w次閱讀

    米家行車記錄儀正式發(fā)布,只需349元

    今天上午,小米米家行車記錄儀正式發(fā)布,號(hào)稱“晚上能拍清車牌的行車記錄儀”。
    發(fā)表于 12-29 15:36 ?3386次閱讀

    行車記錄儀的好處

    行車記錄儀記錄車輛行駛途中的影像及聲音等相關(guān)資訊的儀器。安裝行車記錄儀后,能夠記錄汽車行駛?cè)^
    的頭像 發(fā)表于 02-14 17:10 ?4675次閱讀

    行車記錄儀分幾種

    行車記錄儀主要分為便攜性行車記錄儀與后裝車機(jī)一體式DVD行車記錄儀兩大類,其中便攜性
    的頭像 發(fā)表于 02-15 08:52 ?9525次閱讀

    行車記錄儀十大排名

    本視頻主要詳細(xì)介紹了行車記錄儀十大排名,分別是papago行車記錄儀、PRTDT行車記錄儀、捷渡
    的頭像 發(fā)表于 02-15 08:55 ?5.5w次閱讀

    為什么需要行車記錄儀?

    下面我們就從需求出發(fā),聊一聊什么樣的行車記錄儀是最適合你的。
    的頭像 發(fā)表于 06-19 14:43 ?3807次閱讀