自定義avi結(jié)構(gòu)頭文件?,F(xiàn)在不能實時顯示攝像頭畫面,準備參照fim(終端中顯示圖片),直接對顯示framebuffer操作,顯示視頻。不用qt等。
生成的視頻根據(jù)機子的性能不同,詁計要手動調(diào)一下生成視頻的幀率。
播放: $ aplay ?musicdemo.wmv
錄音: $ arecord -c 2 -r 44100 -f S16_LE musicdemo.wmv
調(diào)節(jié)音量大小: $ alsamixer?
deepin安裝 alsa-lib
sudo apt-get install libasound2-dev ??
main.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
#include "Avi.h"
int main(void){
int fd = open("/dev/video0", O_RDWR);
if(fd < 0)
{
perror("打開設(shè)備失敗");
return -1;
}
struct v4l2_format vfmt;
vfmt.type=1;
vfmt.fmt.pix.width=1680;
vfmt.fmt.pix.height=1080;
vfmt.fmt.pix.pixelformat=V4L2_PIX_FMT_MJPEG;
int ret = ioctl(fd, VIDIOC_S_FMT, &vfmt);
if(ret < 0)
{
perror("設(shè)置格式失敗");
}
struct v4l2_requestbuffers reqbuffer;
reqbuffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuffer.count = 1;
reqbuffer.memory = V4L2_MEMORY_MMAP ;
ret = ioctl(fd, VIDIOC_REQBUFS, &reqbuffer);
if(ret < 0)
{
perror("申請隊列空間失敗");
}
struct v4l2_buffer mapbuffer;
unsigned char *mptr;
unsigned int size;
mapbuffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
mapbuffer.index = 0;
ret = ioctl(fd, VIDIOC_QUERYBUF, &mapbuffer);//查詢緩沖區(qū)狀態(tài)
if(ret < 0)
{
perror("查詢內(nèi)核空間隊列失敗");
}
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fd, VIDIOC_STREAMON, &type); //啟動流
if(ret < 0)
{
perror("開啟失敗");
}
mptr= (unsigned char *)mmap(NULL, mapbuffer.length, PROT_READ|PROT_WRITE,
MAP_SHARED, fd,0); //設(shè)備映射到緩沖區(qū)內(nèi)存
size=mapbuffer.length;
ret = ioctl(fd, VIDIOC_QBUF, &mapbuffer); //把緩沖區(qū)數(shù)據(jù)放入讀隊列中
if(ret < 0)
{
perror("放回失敗");
}
//---------------------------------------------------------------------------
FILE *file=avi_ks();
while(nframes<100){
ret = ioctl(fd, VIDIOC_DQBUF, &mapbuffer); //讀當前隊列緩沖區(qū)的數(shù)據(jù)
if(ret < 0)
{
perror("提取數(shù)據(jù)失敗");
}
int size=1280*720;
unsigned char tmp[4] = {'0', '0', 'd', 'c'}; //00dc = 壓縮的視頻數(shù)據(jù)
fwrite(tmp, 4, 1, file); //寫入是否是壓縮的視頻數(shù)據(jù)信息
fwrite(&size, 4, 1, file); //寫入4字節(jié)對齊后的JPEG圖像大小
fwrite(mptr,size, 1, file); //寫入真正的JPEG數(shù)據(jù)
ret = ioctl(fd, VIDIOC_QBUF, &mapbuffer); //把緩沖區(qū)數(shù)據(jù)放入讀隊列中
if(ret < 0)
{
perror("放回失敗");
}
nframes++;
totalsize++;
}
//---------------------------------------------------------
ret = ioctl(fd, VIDIOC_STREAMOFF, &type);
avi_end(file);
munmap(mptr, size);
close(fd);
return 0;
}
avi.c文章來源:http://www.zghlxwxcb.cn/news/detail-721703.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "Avi.h"
FILE * avi_ks(void){
FILE *fp= fopen("sample.avi","wb");
fseek(fp,sizeof(HEAD),SEEK_SET);
return fp;
}
//--------------------------------------------------------
int avi_add(FILE*fp,char *data,int size){
unsigned char tmp[4] = {'0', '0', 'd', 'c'}; //00dc = 壓縮的視頻數(shù)據(jù)
fwrite(tmp, 4, 1, fp); //寫入是否是壓縮的視頻數(shù)據(jù)信息
fwrite(&size, 4, 1, fp); //寫入4字節(jié)對齊后的JPEG圖像大小
fwrite(data,size, 1, fp); //寫入真正的JPEG數(shù)據(jù)
return 0;
}
//----------------------------------------------------------------------------------
int avi_end(FILE *f_file){
int width=1280;
int height=720;
typedef struct hdrl AVI_HDRL_LIST;
typedef struct movi AVI_LIST_HEAD;
typedef struct avih AVI_AVIH_CHUNK;
typedef struct strl AVI_STRL_LIST;
typedef struct strh AVI_STRH_CHUNK;
typedef struct strf AVI_STRF_CHUNK;
typedef struct avi AVI_HEAD;
AVI_HEAD avi_head={
{
{'R', 'I', 'F', 'F'},
4 + sizeof(AVI_HDRL_LIST) + sizeof(AVI_LIST_HEAD) +nframes * 8 + totalsize,
{'A', 'V', 'I', ' '}
},
{
{'L', 'I', 'S', 'T'},
sizeof(AVI_HDRL_LIST) - 8,
{'h', 'd', 'r', 'l'},
{
{'a', 'v', 'i', 'h'},
sizeof(AVI_AVIH_CHUNK) - 8,
1000000, 25000, 0, 0,nframes, 0, 1, 1000000, width, height,
{0, 0, 0, 0}
},
{
{'L', 'I', 'S', 'T'},
sizeof(AVI_STRL_LIST) - 8,
{'s', 't', 'r', 'l'},
{
{'s', 't', 'r', 'h'},
sizeof(AVI_STRH_CHUNK) - 8,
{'v', 'i', 'd', 's'},
{'J', 'P', 'E', 'G'},
0, 0, 0, 0, 1, 15, 0, nframes, 100000, 0xFFFFFF, 0,
{0, 0, width, height}
},
{
{'s', 't', 'r', 'f'},
sizeof(AVI_STRF_CHUNK) - 8,
sizeof(AVI_STRF_CHUNK) - 8,
width, height, 1, 24,
{'J', 'P', 'E', 'G'},
width * height * 3, 0, 0, 0, 0
}
}
},
{
{'L', 'I', 'S', 'T'},
4 + nframes * 8 + totalsize,
{'m', 'o', 'v', 'i'}
}
};
fseek(f_file, 0, SEEK_SET);
fwrite(&avi_head,sizeof(HEAD),1,f_file);
fclose(f_file);
printf("end\n");
}
avi.h文章來源地址http://www.zghlxwxcb.cn/news/detail-721703.html
#ifndef AVI_H
#define AVI_H
#include <stdio.h>
static int nframes; //總幀數(shù)
static int totalsize;
FILE * avi_ks(void);
int avi_add(FILE*fp,char *data,int size);
int avi_end(FILE *f_file);
struct avi{
struct riff{
unsigned char id[4];
unsigned int size;
unsigned char type[4];
}r1;
struct hdrl{
unsigned char id[4]; //塊ID,固定為LIST
unsigned int size; //塊大小,等于struct avi_hdrl_list去掉id和size的大小
unsigned char type[4]; //塊類型,固定為hdrl
struct avih{
unsigned char id[4]; //塊ID,固定為avih
unsigned int size; //塊大小,等于struct avi_avih_chunk去掉id和size的大小
unsigned int us_per_frame; //視頻幀間隔時間(以微秒為單位)
unsigned int max_bytes_per_sec; //AVI文件的最大數(shù)據(jù)率
unsigned int padding; //設(shè)為0即可
unsigned int flags; //AVI文件全局屬性,如是否含有索引塊、音視頻數(shù)據(jù)是否交叉存儲等
unsigned int total_frames; //總幀數(shù)
unsigned int init_frames; //為交互格式指定初始幀數(shù)(非交互格式應(yīng)該指定為0)
unsigned int streams; //文件包含的流的個數(shù),僅有視頻流時為1
unsigned int suggest_buff_size; //指定讀取本文件建議使用的緩沖區(qū)大小,通常為存儲一楨圖像 //以及同步聲音所需的數(shù)據(jù)之和,不指定時設(shè)為0
unsigned int width; //視頻主窗口寬度(單位:像素)
unsigned int height; //視頻主窗口高度(單位:像素)
unsigned int reserved[4]; //保留段,設(shè)為0即可
}a1;
struct strl{
unsigned char id[4]; //塊ID,固定為LIST
unsigned int size; //塊大小,等于struct avi_strl_list去掉id和size的大小
unsigned char type[4]; //塊類型,固定為strl
struct strh{
unsigned char id[4]; //塊ID,固定為strh
unsigned int size; //塊大小,等于struct avi_strh_chunk去掉id和size的大小
unsigned char stream_type[4]; //流的類型,vids表示視頻流,auds表示音頻流
unsigned char codec[4]; //指定處理這個流需要的解碼器,如JPEG
unsigned int flags; //標記,如是否允許這個流輸出、調(diào)色板是否變化等,一般設(shè)為0即可
unsigned short priority; //流的優(yōu)先級,視頻流設(shè)為0即可
unsigned short language; //音頻語言代號,視頻流設(shè)為0即可
unsigned int init_frames; //為交互格式指定初始幀數(shù)(非交互格式應(yīng)該指定為0)
unsigned int scale; //
unsigned int rate; //對于視頻流,rate / scale = 幀率fps
unsigned int start; //對于視頻流,設(shè)為0即可
unsigned int length; //對于視頻流,length即總幀數(shù)
unsigned int suggest_buff_size; //讀取這個流數(shù)據(jù)建議使用的緩沖區(qū)大小
unsigned int quality; //流數(shù)據(jù)的質(zhì)量指標
unsigned int sample_size; //音頻采樣大小,視頻流設(shè)為0即可
struct rcFrame{ //這個流在視頻主窗口中的顯示位置,設(shè)為{0,0,width,height}即可
short left;
short top;
short right;
short bottom;
} AVI_RECT_FRAME;
}s1;
struct strf{
unsigned char id[4]; //塊ID,固定為strf
unsigned int size; //塊大小,等于struct avi_strf_chunk去掉id和size的大小
unsigned int size1; //size1含義和值同size一樣
unsigned int width; //視頻主窗口寬度(單位:像素)
unsigned int height; //視頻主窗口高度(單位:像素)
unsigned short planes; //始終為1
unsigned short bitcount; //每個像素占的位數(shù),只能是1、4、8、16、24和32中的一個
unsigned char compression[4]; //視頻流編碼格式,如JPEG、MJPG等
unsigned int image_size; //視頻圖像大小,等于width * height * bitcount / 8
unsigned int x_pixels_per_meter; //顯示設(shè)備的水平分辨率,設(shè)為0即可
unsigned int y_pixels_per_meter; //顯示設(shè)備的垂直分辨率,設(shè)為0即可
unsigned int num_colors; //含義不清楚,設(shè)為0即可
unsigned int imp_colors; //含義不清楚,設(shè)為0即可
}q1;
}w1;
}a1;
struct movi{
unsigned char id[4];
unsigned int size;
unsigned char type[4];
}movi1;
}HEAD;
#endif
到了這里,關(guān)于c 攝像頭利用v4l2直接生成avi視頻(不利用ffmpeg)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!