ffmpeg之采样视频数据

首先获得本机支持的图像格式

 

Linux下多使用video4linux2/v4l2设备,通过 ffmpeg -h demuxer=v4l2 查看相关的操作参数

av_dict_set() 

/**
 * 在 *pm中设置给定条目,覆盖现有条目。
 *
 * Note: If AV_DICT_DONT_STRDUP_KEY or AV_DICT_DONT_STRDUP_VAL is set,
 * these arguments will be freed on error.
 *
 * Warning: Adding a new entry to a dictionary invalidates all existing entries
 * previously returned with av_dict_get.
 *
 * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL
 * a dictionary struct is allocated and put in *pm.
 * @param key entry key to add to *pm (will either be av_strduped or added as a new key depending on flags)
 * @param value entry value to add to *pm (will be av_strduped or added as a new key depending on flags).
 *        Passing a NULL value will cause an existing entry to be deleted.
 * @return >= 0 on success otherwise an error code <0
 */
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags);

 

extern "C"{
    #include "libavutil/avutil.h"
    #include "libavdevice/avdevice.h"
    #include "libavformat/avformat.h"
    #include "libavcodec/avcodec.h"
    #include "libswresample/swresample.h"
}
#include <iostream>
using namespace std;
static AVFormatContext *open_dev()
{
    int ret = 0;
    char errors[1024];
    // 创建上下文信息
    AVFormatContext *fmt_ctx = NULL;
    AVDictionary *option = NULL;
    // 打开视频信息
    char *devicename = "/dev/video0";
    // 注册设备信息
    avdevice_register_all();
    // 获得设备的信息
    AVInputFormat *iformat = av_find_input_format("video4linux2");
    // 设置音视频的属性
    av_dict_set(&option, "video_size", "640x480", 0);
    av_dict_set(&option, "pixel_format", "yuyv422", 0);
    // av_dict_set(&option, "framerate", "30", 0);
 
    // 打开设备
    if ((ret = avformat_open_input(&fmt_ctx, devicename, iformat, &option)) < 0)
    {
        av_strerror(ret, errors, 1024);
        fprintf(stderr, "Failed to open audio device, [%d]%s\n", ret, errors);
        return NULL;
    }
    return fmt_ctx;
}
 
void read_video()
{
    int ret = 0;
    AVFormatContext *fmt_ctx = NULL;
    int count = 0;
    AVPacket pkt;
    av_init_packet(&pkt);
    av_log_set_level(AV_LOG_DEBUG);
    char *out = "../source/sample_vide.yuv";
    FILE *outfile = fopen(out, "wb+");
    fmt_ctx = open_dev();
    // read data from devices
    while (count < 500 && (ret = av_read_frame(fmt_ctx, &pkt)) == 0)
    {
        av_log(NULL, AV_LOG_INFO, "packet size is %d(%p)\n",
               pkt.size, pkt.data);
        fwrite(pkt.data, pkt.size, 1, outfile);
        fflush(outfile);
        count++;
        av_packet_unref(&pkt); // release pkt
    }
    if (outfile)
    {
        // close file
        fclose(outfile);
    }
 
    // close device and release ctx
    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
    }
 
    av_log(NULL, AV_LOG_DEBUG, "finish!\n");
    return;
}
int main(int argc, char *argv[])
{
    read_video();
    return 0;
}

使用ffplay进行数据输出:

ffplay -video_size 640x480 -pix_fmt yuyv422  -i sample_vide.yuv
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值