音视频开发---linux环境基于ffmpeg实现拍照功能

本机摄像头可直接输出mjpeg格式, 将摄像头采集的数据保存为jpg图片即可。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "avformat.h"
#include "avcodec.h"
#include "avdevice.h"
 
const char* input_name= "video4linux2";
const char* file_name = "/dev/video0";
const char* out_file  = "output.jpg";
 
int main(int argc, char * argv[])
{    

    AVFormatContext *fmtCtx = NULL;    
    AVPacket *packet; 
    AVInputFormat *inputFmt;
    FILE *fp; 
	int ret;
 
 
    avdevice_register_all();    
    if( (inputFmt = av_find_input_format (input_name)) == NULL){
		printf("av_find_input_format failed\n");
		return -1;
	}  
    if (avformat_open_input ( &fmtCtx, file_name, inputFmt, NULL) < 0){
        printf("avformat_open_input failed\n");         
		return -1;    
    }
	av_dump_format(fmtCtx, 0, file_name, 0); 
    packet = (AVPacket *)av_malloc(sizeof(AVPacket));  
    av_read_frame(fmtCtx, packet); 
 
    fp = fopen(out_file, "wb");    
    if (fp < 0)    {        
        printf("open frame data file failed\n");        
        return -1;    
    }    
    
    fwrite(packet->data, 1, packet->size, fp);    
    fclose(fp);    
    av_packet_free(&packet);    
    avformat_close_input(&fmtCtx);
	return 0;
 } 
 

编译: gcc  main.c -I/usr/local/include -lavformat  -lavdevice -lavcodec -lavutil

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值