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 join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

linux系统中java的cacerts的优先级详解

《linux系统中java的cacerts的优先级详解》文章讲解了Java信任库(cacerts)的优先级与管理方式,指出JDK自带的cacerts默认优先级更高,系统级cacerts需手动同步或显式... 目录Java 默认使用哪个?如何检查当前使用的信任库?简要了解Java的信任库总结了解 Java 信

java程序远程debug原理与配置全过程

《java程序远程debug原理与配置全过程》文章介绍了Java远程调试的JPDA体系,包含JVMTI监控JVM、JDWP传输调试命令、JDI提供调试接口,通过-Xdebug、-Xrunjdwp参数配... 目录背景组成模块间联系IBM对三个模块的详细介绍编程使用总结背景日常工作中,每个程序员都会遇到bu

Linux命令rm如何删除名字以“-”开头的文件

《Linux命令rm如何删除名字以“-”开头的文件》Linux中,命令的解析机制非常灵活,它会根据命令的开头字符来判断是否需要执行命令选项,对于文件操作命令(如rm、ls等),系统默认会将命令开头的某... 目录先搞懂:为啥“-”开头的文件删不掉?两种超简单的删除方法(小白也能学会)方法1:用“--”分隔命