linux 平台最简FFMPEG 程序

2024-02-22 03:08

本文主要是介绍linux 平台最简FFMPEG 程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在学习FFMPEG,看了很多的例子都是在Windows下的,这里提供一个linux下的一个简单程序,由雷神的工程修改而来。

[objc]  view plain copy
print ?
  1. #include <stdio.h>  
  2. #define __STDC_CONSTANT_MACROS  
  3. #include "avformat.h"  
  4. #include "avcodec.h"  
  5. #include "swscale.h"  
  6.   
  7. int main(int argc, char* argv[])  
  8. {  
  9.     AVFormatContext *pFormatCtx;  
  10.     int             i, videoindex;  
  11.     AVCodecContext  *pCodecCtx;  
  12.     AVCodec         *pCodec;  
  13.     AVFrame *pFrame,*pFrameYUV;  
  14.     uint8_t *out_buffer;  
  15.     AVPacket *packet;  
  16.     int y_size;  
  17.     int ret, got_picture;  
  18.     struct SwsContext *img_convert_ctx;  
  19.     //输入文件路径  
  20.     char filepath[]="../testfile/school.flv";  
  21.   
  22.     int frame_cnt;  
  23.       
  24.     av_register_all();                       /* 注册复用器 编码器 */  
  25.     avformat_network_init();                 /* 打开网络流 */  
  26.     pFormatCtx = avformat_alloc_context();   /* 分配内存 */  
  27.   
  28.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){               
  29.         printf("Couldn't open input stream.\n");  
  30.         return -1;  
  31.     }  
  32.     if(avformat_find_stream_info(pFormatCtx,NULL)<0){    /* 读取一部分视音频数据并且获得一些相关的信息 */  
  33.         printf("Couldn't find stream information.\n");  
  34.         return -1;  
  35.     }  
  36.     videoindex=-1;  
  37.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  38.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){  
  39.             videoindex=i;  
  40.             break;  
  41.         }  
  42.     if(videoindex==-1){  
  43.         printf("Didn't find a video stream.\n");  
  44.         return -1;  
  45.     }  
  46.   
  47.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  48.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id); /* 查找FFmpeg的解码器 */  
  49.     if(pCodec==NULL){  
  50.         printf("Codec not found.\n");  
  51.         return -1;  
  52.     }  
  53.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){    /* 初始化一个视音频编解码器的AVCodecContext  */  
  54.         printf("Could not open codec.\n");  
  55.         return -1;  
  56.     }  
  57.   
  58.     /* printf information of the input video */  
  59.     printf("AVFormatContext AVInputFormat name = %s  \n",pFormatCtx->iformat->name);  
  60.     printf("Number of elements in AVFormatContext.streams = %d  \n",pFormatCtx->nb_streams);  
  61.     printf("Duration of the stream, in AV_TIME_BASE fractional = %d  \n",pFormatCtx->duration);  
  62.     printf("Total stream bitrate in   %d  bit/s \n",pFormatCtx->bit_rate);  
  63.   
  64.     printf("picture width   =  %d \n",pCodecCtx->width);  
  65.     printf("picture height  =  %d \n",pCodecCtx->height);  
  66.       
  67.   
  68.   
  69.     pFrame=av_frame_alloc();        /* 分配一个 AVFrame 的内存*/  
  70.     pFrameYUV=av_frame_alloc();  
  71.     out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); /*内存分配函数 */  
  72.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); /* 为已经分配的空间的结构体AVPicture挂上一段用于保存数据的空间 */  
  73.     packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  74.   
  75.     printf("--------------- File Information ----------------\n");  
  76.     av_dump_format(pFormatCtx,0,filepath,0);  
  77.     printf("-------------------------------------------------\n");  
  78.       
  79.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   
  80.         pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULLNULLNULL);   /* 初始化SwsContext的函数 */  
  81.   
  82.     frame_cnt=0;  
  83.   
  84.     //FILE* h264_fd = fopen("test.h264", wb+);  
  85.     while(av_read_frame(pFormatCtx, packet)>=0){  
  86.         if(packet->stream_index==videoindex){  
  87.               
  88.            // fwrite(packet->data,packet->size, 1 h264_fd);  
  89.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  /* 解码一帧视频数据 */  
  90.             if(ret < 0){  
  91.                 printf("Decode Error.\n");  
  92.                 return -1;  
  93.             }  
  94.             if(got_picture){  
  95.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,   
  96.                     pFrameYUV->data, pFrameYUV->linesize);                       /* 转换像素的函数 */  
  97.                 //printf("Decoded frame index: %d\n",frame_cnt);  
  98.                 frame_cnt++;  
  99.   
  100.             }  
  101.         }  
  102.         av_free_packet(packet);  
  103.     }  
  104.   
  105.     //fclose(h264_fd);  
  106.     sws_freeContext(img_convert_ctx);  
  107.   
  108.     av_frame_free(&pFrameYUV);  
  109.     av_frame_free(&pFrame);  
  110.     avcodec_close(pCodecCtx);  
  111.     avformat_close_input(&pFormatCtx);  
  112.   
  113.     return 0;  
  114. }  


    Makefile 文件如下,头文件和库文件的目录会因为安装目录的不同而不同。

[objc]  view plain copy
print ?
  1. OUT_APP      = test  
  2. INCLUDE_PATH = /usr/local/include/  
  3. INCLUDE = -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \  
  4.             -I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \  
  5.             -I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \  
  6.             -I$(INCLUDE_PATH)libswscale/  
  7.   
  8. FFMPEG_LIBS = -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale  
  9. SDL_LIBS    =   
  10. LIBS        = $(FFMPEG_LIBS)$(SDL_LIBS)  
  11.   
  12. COMPILE_OPTS = $(INCLUDE)  
  13. C            = c  
  14. OBJ          = o  
  15. C_COMPILER   = cc  
  16. C_FLAGS      = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)  
  17.   
  18. LINK         = cc -o   
  19. LINK_OPTS    = -lz -lm  -lpthread  
  20. LINK_OBJ     = echo_information.o   
  21.   
  22. .$(C).$(OBJ):  
  23.     $(C_COMPILER) -c $(C_FLAGS) $<  
  24.   
  25.   
  26. $(OUT_APP): $(LINK_OBJ)  
  27.     $(LINK)$@  $(LINK_OBJ)  $(LIBS) $(LINK_OPTS)  
  28.   
  29. clean:  
  30.         -rm -rf *.$(OBJ) $(OUT_APP) core *.core *~ include/*~  

这篇关于linux 平台最简FFMPEG 程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/733947

相关文章

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st