热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:ffmpeg结合SDL编写播放器

篇首语:本文由编程笔记#小编为大家整理,主要介绍了ffmpeg结合SDL编写播放器相关的知识,希望对你有一定的参考价值。接下来是解析影片的帧

篇首语:本文由编程笔记#小编为大家整理,主要介绍了ffmpeg结合SDL编写播放器相关的知识,希望对你有一定的参考价值。


接下来是解析影片的帧


/***
project.c
**
*/
#include

#include

#include

#include

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE
*pFile;
char szFilename[32];
int y;

//open file
sprintf(szFilename,"frame%d.ppm",iFrame);
pFile
= fopen(szFilename, "wb");
if (NULL == pFile)
return;

//write header
fprintf(pFile, "P6
%d %d
255
",width,height);
//write pixel data
for (y = 0; y )
{
fwrite(pFrame->data[0] + y * pFrame->linesize[0],
1, width * 3, 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 <2)
{
printf(
"Please provide a movie file
");
return -1;
}

//register all formats and codecs
av_register_all();

//open video file
if (avformat_open_input(&pFormatCtx,argv[1], NULL, NULL) != 0)
{
return -1; //couldn‘t open file
}

//retrive stream information
if (avformat_find_stream_info(pFormatCtx, NULL) <0)
{
return -1; //couldn‘t find stream information
}

//dump information about file onto standard error
av_dump_format(pFormatCtx, 0, argv[1], 0);

//find the first video stream
videoStream = -1;
for (i = 0; i nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream
= i;
break;
}
}
if (videoStream == -1)
{
return -1; //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!
");
return -1; //Codec not found
}
//open codec
if (avcodec_open2(pCodecCtx, pCodec, &optionsDict) <0)
{
return -1; //Could not open codec
}

//Allcocate an AVFrame structure
pFrame = av_frame_alloc();
pFrameRGB
= av_frame_alloc();
if (pFrameRGB == NULL)
{
return -1;
}

//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 = 0;
while (av_read_frame(pFormatCtx, &packet) >= 0)
{
//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,
0,
pCodecCtx
->height,
pFrameRGB
->data,
pFrameRGB
->linesize
);

//Save the frame to disk
SaveFrame(pFrameRGB,pCodecCtx->width,
pCodecCtx
->height,i);
printf(
"decde %d frame
",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 0;
}

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图片。


推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
  • 如何在php文件中添加图片?
    本文详细解答了如何在php文件中添加图片的问题,包括插入图片的代码、使用PHPword在载入模板中插入图片的方法,以及使用gd库生成不同类型的图像文件的示例。同时还介绍了如何生成一个正方形文件的步骤。希望对大家有所帮助。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文讨论了如何使用GStreamer来删除H264格式视频文件中的中间部分,而不需要进行重编码。作者提出了使用gst_element_seek(...)函数来实现这个目标的思路,并提到遇到了一个解决不了的BUG。文章还列举了8个解决方案,希望能够得到更好的思路。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
author-avatar
辛勤的核桃4dr_797
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有