http://www.tuicool.com/articles/jiUzua

主题 FFmpeg
 

任务:需要把一个视频文件的每一帧提取出来,每帧存储成单独的文件。

以前用Matlab处理过这个问题,可是感觉比较慢,而且最近正在逐步转向使用开源的东西。因此搜到ffmpeg这个好东西。

ffmpeg可用来处理视频文件的提取和各种转换,跨平台,官网上有Linux,WINDOWS和MAC版本。

以下是windows 命令行下使用ffmpeg提取视频帧的方法:

SET PATH=%PATH%;[path_to_ffmpeg]
SET VIDEOFILE=demo.mp4
SET DESTDIR=video_frame\
ffmpeg -i %VIDEOFILE% -q:v 2 -f image2 %DESTDIR%%07d.jpeg
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

其中-i 后面是输入文件,-q:v 2 q代表质量quality, v代表视频流,2是控制质量的参数。-f指定输出的格式是image2. %07d是图片命名的pattern

//--------------------------------------------------------------------------------------------

第一次接触ffmpeg,可以算是hello world程序。

下面的代码全部都是直接可以使用的,借鉴了官方学习样例,也算是翻译吧。

但是解决了,保存bmp图像时,图像颠倒和色彩异常问题。

// x_ffmpeg.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <iosfwd>
#include <fstream>
using namespace std; #define FILE_OUT
#ifdef FILE_OUT
std::ofstream file_debugout("frameandpacketinfo.txt");
#endif static int av_create_bmp(char* filename,uint8_t *pRGBBuffer,int width,int height,int bpp)
{
BITMAPFILEHEADER bmpheader;
BITMAPINFO bmpinfo;
FILE *fp; fp = fopen(filename,"wb");
if(!fp)return -1; bmpheader.bfType = ('M'<<8)|'B';
bmpheader.bfReserved1 = 0;
bmpheader.bfReserved2 = 0;
bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = width;
/*----注意,这里的bmpinfo.bmiHeader.biHeight变量的正负决定bmp文件的存储方式,如果
为负值,表示像素是倒过来的*/
bmpinfo.bmiHeader.biHeight = -height;
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = bpp;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 100;
bmpinfo.bmiHeader.biYPelsPerMeter = 100;
bmpinfo.bmiHeader.biClrUsed = 0;
bmpinfo.bmiHeader.biClrImportant = 0; fwrite(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pRGBBuffer,width*height*bpp/8,1,fp);
fclose(fp); return 0;
} static int av_create_bmp(char* filename, AVFrame *pRGBBuffer,int width,int height,int bpp)
{
BITMAPFILEHEADER bmpheader;
BITMAPINFO bmpinfo;
FILE *fp; fp = fopen(filename, "wb");
if(!fp)return -1; bmpheader.bfType = ('M'<<8)|'B';
bmpheader.bfReserved1 = 0;
bmpheader.bfReserved2 = 0;
bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = width;
bmpinfo.bmiHeader.biHeight = -height;
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = 24;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 100;
bmpinfo.bmiHeader.biYPelsPerMeter = 100;
bmpinfo.bmiHeader.biClrUsed = 0;
bmpinfo.bmiHeader.biClrImportant = 0;
fwrite(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
//fwrite(pRGBBuffer,width*height*bpp/8,1,fp);
for(int y=0; y<height; y++)
fwrite(pRGBBuffer->data[0] + y*pRGBBuffer->linesize[0], 1, width*3, fp);
fclose(fp);
return 0;
} static void print_packet_info(AVPacket info)
{
#ifdef FILE_OUT
file_debugout << "print_packet_info" << " convergence_duration:" << info.convergence_duration
<< " dts:" << info.dts
<< " duration:" << info.duration
<< " flags:" << info.flags
<< " pos:" << info.pos
<< " pts:" << info.pts
<< " size:" << info.size
<< " stream_index:" << info.stream_index << endl;
#else
cout << "print_packet_info" << " convergence_duration:" << info.convergence_duration
<< " dts:" << info.dts
<< " duration:" << info.duration
<< " flags:" << info.flags
<< " pos:" << info.pos
<< " pts:" << info.pts
<< " size:" << info.size
<< " stream_index:" << info.stream_index << endl;
#endif
} static void print_frame_info(AVFrame* pinfo)
{
#ifdef FILE_OUT
file_debugout << "print_frame_info" << " coded_picture_number:" << pinfo->coded_picture_number
<< " display_picture_number:" << pinfo->display_picture_number
<< " type:" << pinfo->type << endl;
#else
cout << "print_frame_info" << " coded_picture_number:" << pinfo->coded_picture_number
<< " display_picture_number:" << pinfo->display_picture_number
<< " type:" << pinfo->type << endl;
#endif } int decode_video_packet(AVFormatContext * fmt_ctx_for_decode, \
AVCodecContext* dec_ctx, int video_stream_index)
{
int ret = 0; AVFrame* pFrame=avcodec_alloc_frame();
AVFrame* pFrameRGB = avcodec_alloc_frame(); int numBytes=avpicture_get_size(PIX_FMT_BGR24, dec_ctx->width,dec_ctx->height);
uint8_t* buffer = new(std::nothrow) uint8_t[numBytes]; avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_BGR24,\
dec_ctx->width, dec_ctx->height); SwsContext *pSWSCtx = sws_getContext(dec_ctx->width, dec_ctx->height, \
dec_ctx->pix_fmt, dec_ctx->width, dec_ctx->height, PIX_FMT_BGR24,\
SWS_BICUBIC, NULL, NULL, NULL); if (NULL == pFrame || NULL == pFrameRGB || NULL == buffer || NULL == pSWSCtx)
{
ret = -1;
goto exit;
} AVPacket packet;
int key_frame_picture_count = 0; while (av_read_frame(fmt_ctx_for_decode, &packet) >= 0)
{
if (packet.stream_index == video_stream_index)
{
int got_frame = 0;
avcodec_decode_video2(dec_ctx, pFrame,&got_frame, &packet); if (got_frame) //一个完整的帧
{
if (pFrame->key_frame)
{
sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, \
dec_ctx->height, pFrameRGB->data, pFrameRGB->linesize); // 保存到磁盘
char pic[200];
sprintf(pic,"keyframe%d.bmp", ++key_frame_picture_count);
av_create_bmp(pic,pFrameRGB->data[0],dec_ctx->width,dec_ctx->height,24);
//av_create_bmp(pic, pFrameRGB, dec_ctx->width,dec_ctx->height,24);
}
print_frame_info(pFrame); } print_packet_info(packet);
} } exit:
avcodec_free_frame(&pFrame);
avcodec_free_frame(&pFrameRGB);
delete [] buffer;
sws_freeContext(pSWSCtx);
return ret;
} static int open_input_file(const char *filename)
{
int ret;
bool video_codec_init = false;
int video_stream_index = -1;
AVCodecContext* suitable_dec_ctx = NULL;
AVFormatContext *video_fmt_ctx = NULL; if ((ret = avformat_open_input(&video_fmt_ctx, filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
} if ((ret = avformat_find_stream_info(video_fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
avformat_close_input(&video_fmt_ctx);
return ret;
} for (int i = 0; i < video_fmt_ctx->nb_streams; i++)
{
// 找到视频码流
if (video_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_stream_index = i;
// 初始化解码器信息
if (!video_codec_init)
{
suitable_dec_ctx = video_fmt_ctx->streams[i]->codec; AVCodec* pcodec = avcodec_find_decoder(suitable_dec_ctx->codec_id); if (NULL == pcodec)
{
printf("cannot find decoder");
avformat_close_input(&video_fmt_ctx);
return 1;
} if(0 != avcodec_open2(suitable_dec_ctx, pcodec, NULL))
{
printf("open codecer failed");
avformat_close_input(&video_fmt_ctx);
return 1;
} video_codec_init = true;
} }
} // 解码视频
if (video_codec_init && suitable_dec_ctx)
{
decode_video_packet(video_fmt_ctx, suitable_dec_ctx, video_stream_index);
} // 关闭文件
avformat_close_input(&video_fmt_ctx); return 0;
} int _tmain(int argc, _TCHAR* argv[])
{
// 注册库中所有可能有用的文件格式和编码器
av_register_all();
open_input_file("C:\\Users\\xukaijun.HIK\\Desktop\\hikvison.mp4"); #ifdef FILE_OUT
file_debugout.close();
#endif return 0;
}

【学习ffmpeg】打开视频文件,帧分析,并bmp保存关键帧的更多相关文章

  1. Java使用FFmpeg处理视频文件的方法教程

    这篇文章主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 前言 本文主要 ...

  2. Java使用FFmpeg处理视频文件指南

    Java使用FFmpeg处理视频文件指南 本文主要讲述如何使用Java + FFmpeg实现对视频文件的信息提取.码率压缩.分辨率转换等功能: 之前在网上浏览了一大圈Java使用FFmpeg处理音视频 ...

  3. python实现调用摄像头或打开视频文件

    目录: (一)调用摄像头或打开视频文件代码实现 (二)说明和补充 (一)调用摄像头或打开视频文件代码实现 1 # -*- coding=GBK -*- 2 import cv2 as cv 3 4 5 ...

  4. opencv打开视频文件出错

    使用C#调用mingw的so文件,在C++端使用opencv打开视频.这样的项目完成过了一个,第二次做的时候,发现opencv打开视频文件出错. 首先怀疑是opencv的opencv_ffmpeg24 ...

  5. php ffmpeg截取视频第一帧保存为图片的方法

    php ffmpeg截取视频第一帧保存为图片的方法 <pre> $xiangmupath = $this->getxiangmupath(); $filename = 'chengs ...

  6. 实战FFmpeg--iOS平台使用FFmpeg将视频文件转换为YUV文件

    做播放器的开发这里面涉及的东西太多,我只能一步步往前走,慢慢深入.播放器播放视频采用的是渲染yuv文件.首先,要知道yuv文件是怎么转换得来的,其次,要知道怎么把视频文件保存为yuv文件.雷神的文章1 ...

  7. 使用ffmpeg合并视频文件的三种方法

    ffmpeg合并视频的方法有三种.国内大多数仅介绍了其中之一.于是觉得有必要翻译一下.其实在ffmpeg的 FAQ文档中有比较详细的说明. 使用concat协议进行视频文件的合并 这种方式的适用场景是 ...

  8. 使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整

    安装 略. 转码 最简单命令如下: ffmpeg -i out.ogv -vcodec h264 out.mp4ffmpeg -i out.ogv -vcodec mpeg4 out.mp4ffmpe ...

  9. ffmpeg实现视频文件合并/截取预览视频/抽取音频/crop(裁剪)(ffmpeg4.2.2)

    一,ffmpeg的安装 请参见: https://www.cnblogs.com/architectforest/p/12807683.html 说明:刘宏缔的架构森林是一个专注架构的博客,地址:ht ...

随机推荐

  1. 【Raspberry Pi】读取DHT11温度湿度波折

    从网上找到了DHT11厂家说明书,尝试用python根据时序图写数据获取驱动,但发现python的高层特性导致在做底层代码时例如控制20us时延这类需求就没什么好的办法. 还是得回到C-wiringP ...

  2. 第7步:安装Grid

    7.1解压文件 注意,安装Grid时需要以grid用户身份执行,在那之前需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb1~]# ...

  3. Multiview

    新建3个ViewController的类 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button ...

  4. map实现

    /*PLSQL实现Map*/ --建立序列create sequence seq_map_param_id ;--建立参数表create table map_param(id number prima ...

  5. cocos2d安卓自动编译脚本去掉复制Resources资源和签名功能

    去掉这两个功能的原因: 1.因为有时候打包是分渠道的,不同的渠道资源也可能不一样,所以不能直接复制资源. 2.如果用cocostudio打release包,因为要输入签名地址,会导致在自动签名处停住不 ...

  6. python 清华镜像pip install

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple

  7. Cocos2d-x 学习之引擎介绍

    Cocos2d-X是一个开源的移动2D游戏框架,MIT许可证下发布的.这是一个C + +cocos2d-iPhone项目的版本.cocos2d-X发展的重点是围绕cocos2d跨平台.即其实现一次编码 ...

  8. Ubuntu 16.04 安装google浏览器

    因为安装的Linux是64位的Ubuntu 16.04系统,所以本人决定也安装64位的谷歌Chrome浏览器.在 Ubuntu 16.04 中,要想使用谷歌的 Chrome 浏览器,可以通过命令行的方 ...

  9. 读 下一代SOA 服务技术与面向服务简明指南

    面向服务的八个设计原则 标准化服务合同 在同一个服务仓库中的服务都符合同样的合同设计标准 服务松耦合 服务合同施加了消费者低耦合的要求,而它们自己也与周围的环境脱钩 服务抽象 服务合同只包含基本信息, ...

  10. Servlet——总结

    当我们学完了JavaSe部分的知识之后,如果我们往Web方面发展和学习的话,我们将会接触到一个非常重要的web技术——Servlet.在说明Servlet的配置之前我们先来了通过下面的请求响应图解一下 ...