热门标签 | 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图片。


推荐阅读
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文深入探讨了HTTP请求和响应对象的使用,详细介绍了如何通过响应对象向客户端发送数据、处理中文乱码问题以及常见的HTTP状态码。此外,还涵盖了文件下载、请求重定向、请求转发等高级功能。 ... [详细]
  • 本文详细探讨了HTML表单中GET和POST请求的区别,包括它们的工作原理、数据传输方式、安全性及适用场景。同时,通过实例展示了如何在Servlet中处理这两种请求。 ... [详细]
  • Python处理Word文档的高效技巧
    本文详细介绍了如何使用Python处理Word文档,涵盖从基础操作到高级功能的各种技巧。我们将探讨如何生成文档、定义样式、提取表格数据以及处理超链接和图片等内容。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 优化ASM字节码操作:简化类转换与移除冗余指令
    本文探讨如何利用ASM框架进行字节码操作,以优化现有类的转换过程,简化复杂的转换逻辑,并移除不必要的加0操作。通过这些技术手段,可以显著提升代码性能和可维护性。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • PHP数组平均值计算方法详解
    本文详细介绍了如何在PHP中计算数组的平均值,涵盖基本概念、具体实现步骤及示例代码。通过本篇文章,您将掌握使用PHP函数array_sum()和count()来求解数组元素的平均值。 ... [详细]
  • 本文介绍了如何使用JavaScript的Fetch API与Express服务器进行交互,涵盖了GET、POST、PUT和DELETE请求的实现,并展示了如何处理JSON响应。 ... [详细]
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社区 版权所有