接下来是解析影片的帧

/***
project.c
***/
#include<stdio.h>
#include<libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include<libswscale/swscale.h> void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[];
int y; //open file
sprintf(szFilename,"frame%d.ppm",iFrame);
pFile = fopen(szFilename, "wb");
if (NULL == pFile)
return; //write header
fprintf(pFile, "P6\n%d %d\n255\n",width,height); //write pixel data
for (y = ; y < height; y++)
{
fwrite(pFrame->data[] + y * pFrame->linesize[],
, width * , pFile);
} //close file
fclose(pFile);
}
int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGB = NULL;
AVPacket packet;
int i,videoStream;
int frameFinished;
int numBytes;
uint8_t *buffer = NULL; AVDictionary *optionsDict = NULL;
struct SwsContext *sws_ctx = NULL; if (argc < )
{
printf("Please provide a movie file\n");
return -;
} //register all formats and codecs
av_register_all(); //open video file
if (avformat_open_input(&pFormatCtx,argv[], NULL, NULL) != )
{
return -; //couldn't open file
} //retrive stream information
if (avformat_find_stream_info(pFormatCtx, NULL) < )
{
return -; //couldn't find stream information
} //dump information about file onto standard error
av_dump_format(pFormatCtx, , argv[], ); //find the first video stream
videoStream = -;
for (i = ; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
} if (videoStream == -)
{
return -; //Don't find a video stream
} //Get a pointer to the codec context for the video stream
pCodecCtx = pFormatCtx->streams[videoStream]->codec; //Find the decoder for the video stream
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
fprintf(stderr,"Unsupported codec!\n");
return -; //Codec not found
} //open codec
if (avcodec_open2(pCodecCtx, pCodec, &optionsDict) < )
{
return -; //Could not open codec
} //Allcocate an AVFrame structure
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
if (pFrameRGB == NULL)
{
return -;
} //Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); sws_ctx = sws_getContext
(
pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_RGB24,
SWS_BILINEAR,
NULL, NULL, NULL
); //Assign appropriate parts of buffer to image planes in pFrameRGB
//Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVFPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer,
AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height); //Read frames and save first five frames to disk
i = ;
while (av_read_frame(pFormatCtx, &packet) >= )
{
//Is this a packet from video stream
if (packet.stream_index == videoStream)
{
//decode video frame
avcodec_decode_video2(pCodecCtx, pFrame,
&frameFinished, &packet); //Did wo get a video frame
if (frameFinished)
{
//Convert the image from its native format to RGB
sws_scale(sws_ctx,
(uint8_t const * const *)pFrame->data,
pFrame->linesize,
,
pCodecCtx->height,
pFrameRGB->data,
pFrameRGB->linesize
); //Save the frame to disk
SaveFrame(pFrameRGB,pCodecCtx->width,
pCodecCtx->height,i);
printf("decde %d frame\n",i);
i++;
}
} //Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
} //Free the RGB image
av_free(buffer);
av_free(pFrameRGB); //Free the YUV frame
av_free(pFrame); //Close the codec
avcodec_close(pCodecCtx); //Close the video file
avformat_close_input(&pFormatCtx);
return ;
}

makefile如下:

//makefile

DIR_INC = -I/usr/local/include
DIR_LIB = -L/usr/local/lib LIBS = -lavformat\
-lavcodec\
-lva-x11 \
-lva \
-lxcb-shm \
-lxcb-xfixes \
-lxcb-render \
-lxcb-shape \
-lxcb -lX11 \
-lasound \
-lz \
-lswresample \
-lswscale \
-lavutil \
-lm \
-pthread FLAGS = -Wall -ggdb project : project.c
gcc project.c ${FLAGS} ${DIR_INC} ${DIR_LIB} ${LIBS} -o project .PHONY:clean
clean:
rm project

运行结果:

完成后有很多ppm文件,可以将ppm转为jpg:

编写一个脚本转化,内容如下:

/***
1.sh
***/
#!/bin/bash ff=`ls *.ppm` for f in $ff
do
file=`echo ${f%.*}`
ffmpeg -i "$file".ppm "$file".jpg
done mkdir -p jpgs
mv *.jpg jpgs
rm *.ppm

运行脚本:

sh 1.sh

可以在当前文件夹下找到jpgs文件夹下找到所有转化的jpg图片。

ffmpeg结合SDL编写播放器(三)的更多相关文章

  1. ffmpeg结合SDL编写播放器(二)

    我们将对帧数据做一些处理,比如将每一帧的 图像转为jpg或者bmp或者ppm等格式保存下来. 举例:在ffmpeg-2.8.8文件夹下编写test.c程序 /* test.c */ #include& ...

  2. ffmpeg结合SDL编写播放器

    创建播放窗口 SDL_Surface *screen = NULL; screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->heig ...

  3. ffmpeg结合SDL编写播放器(一)

    ffmpeg 工具是一个高效快速的命令行工具,进行视音频不同格式之间的转换. ffmpeg命令行 ffmpeg可以读取任意数量的输入“文件”(可以是常规文件,管道,网络流,抓取设备等)读取,由 -i ...

  4. H.264:FFMpeg 实现简单的播放器

    H.264:FFMpeg 实现简单的播放器   FFMPEG工程浩大,可以参考的书籍又不是很多,因此很多刚学习FFMPEG的人常常感觉到无从下手.我刚接触FFMPEG的时候也感觉不知从何学起. 因此我 ...

  5. H264-YUV通过RTP接收视频流ffmpeg解码SDL实时播放

    写在前面的话 写一个简单的播放器,通过RTP接收视频流,进行实时播放.最初,使用ffplay或者vlc接收按照SDP协议文件可以播放视频,但是视频中断后重启,不能正确的解包,时常会出现如下的错误信息. ...

  6. JavaCV 学习(二):使用 JavaCV + FFmpeg 制作拉流播放器

    一.前言 在 Android 音视频开发学习思路 中,我们不断的学习和了解音视频相关的知识,随着知识点不断的学习,我们现在应该做的事情,就是将知识点不断的串联起来.这样才能得到更深层次的领悟.通过整理 ...

  7. FFmpeg入门,简单播放器

    一个偶然的机缘,好像要做直播相关的项目 为了筹备,前期做一些只是储备,于是开始学习ffmpeg 这是学习的第一课 做一个简单的播放器,播放视频画面帧 思路是,将视频文件解码,得到帧,然后使用定时器,1 ...

  8. ffmpeg学习(三)——ffmpeg+SDL2 实现简单播放器

    本篇实现基于ffmpeg动态库用测试程序播放本地文件和RTSP视频流. 参考文章:http://blog.csdn.net/leixiaohua1020/article/details/8652605 ...

  9. 基于FFmpeg和Qt的播放器 QtAV库

    http://blog.csdn.net/ibingow/article/details/8144795

随机推荐

  1. C# vb .net图像合成-合成矩形

    在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...

  2. Linux下搭建keepalive+nginx

    一. 安装nginx(略) 二. 安装keepalive 下载http://www.keepalived.org/download.html 安装依赖包 yum install –y popt* gc ...

  3. S5PV210 固件烧写 u-boot烧写

    首先阅读CW210_CD自带光盘中CW210 开发板使用手册.pdf 使用usb 拨码开关置成usb启动.xx可以是ON或OFF.开发板上面也有丝印提示 usb线接好,串口线接好 使用DNW下载 自带 ...

  4. Vue Nginx反向代理配置 解决生产环境跨域

    Vue本地代理举例: module.exports = { publicPath: './', devServer: { proxy: { '/api': { target: 'https://mov ...

  5. Python学习日记(三十七) Mysql数据库篇 五

    pymsql的使用 初识pymysql模块 先在数据库中创建一个用户信息表,里面包含用户的ID.用户名.密码 create table userinfo( uid int not null auto_ ...

  6. Linux相关目录

    Linux 启动流程 Linux--基本目录 Linux--selinux Linux--网卡配置 Linux--系统运行级别 Linux--重要文件

  7. django memcached/redis缓存 =====缓存session

    全站使用 例如 博客等缓存,通过中间件实现全站缓存. 加缓存中间件,那么多中间件加在什么位置? 请求时:缓存加在中间件里的最后一个,比如一次经过1.2.3.4中间件,加在4 返回事:缓存加在中间件里的 ...

  8. springboot配置tomcat大全

    server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all poss ...

  9. first集合follow集的求法

    FIRST集的定义 : 设G=(VT,VN,P,S)是上下文无关文法 FIRST(a)={a|a=>*ab,a∈VT, a,b∈V*} 若a=>*ε则规定ε∈FIRST (a) FIRST ...

  10. Appium连接夜神模拟器,模拟手势点击(tap)

    # -*- coding:utf-8 -*- from appium import webdriver from time import sleep desired_caps ={ 'platform ...