FFmpeg命令格式

1.Window32下录制屏幕

ffmpeg –f gdigrab –framerate 25 –offset_x 0 –offset_y 0 –video_size 1366*768 –i desktop out.mpg

 

2.Linux下采用FFmpeg库采集摄像头的一帧图像

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>


void captureOneFrame()
{
    AVFormatContext *fmtCtx = NULL;
    AVFormatParameters inputFmtParameter;
    AVPacket *pcaket;
       
    //输入格式(V4L2)
    AVInputFormat *inputFmt = av_find_input_format ("video4linux2");
    if (inputFmt == NULL)
    {
        printf("can not find_input_format\n");
        return;
    }

    memset (&inputFmtParameter, 0, sizeof(inputFmtParameter));
    //采集图像的高度
    inputFmtParameter.height = 240;
    //采集图像的宽度
    inputFmtParameter.width  = 320;

    //打开摄像头设备
    if (av_open_input_file ( &fmtCtx, "/dev/video0", inputFmt,
               sizeof(inputFmtParameter),&inputFmtParameter) < 0)
    {
        printf("can not open_input_file\n");
         return;
    }
    //从摄像头获取一帧图像
    av_read_frame(fmtCtx, pcaket);
    //输出图像的大小
    printf("data length = %d\n",pcaket->size);
   
    FILE *fp;
    //打开(新建)文件
    fp = fopen("out.yuv", "wb");
    if (fp < 0)
    {
        printf("open frame data file failed\n");
        return ;
    }
    //将数据写入文件
    fwrite(pcaket->data, 1, pcaket->size, fp);
    //关闭文件
    fclose(fp);

    //关闭设备文件
    av_close_input_file(fmtCtx);
}


int main()
{
    avcodec_init();   
    avcodec_register_all();
    avdevice_register_all();

    captureOneFrame();

    return 0;
}

posted @ 2016-07-13 09:37  撸码之路  阅读(327)  评论(0编辑  收藏  举报