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本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

linux打包解压命令方式

《linux打包解压命令方式》文章介绍了Linux系统中常用的打包和解压命令,包括tar和zip,使用tar命令可以创建和解压tar格式的归档文件,使用zip命令可以创建和解压zip格式的压缩文件,每... 目录Lijavascriptnux 打包和解压命令打包命令解压命令总结linux 打包和解压命令打

linux如何复制文件夹并重命名

《linux如何复制文件夹并重命名》在Linux系统中,复制文件夹并重命名可以通过使用“cp”和“mv”命令来实现,使用“cp-r”命令可以递归复制整个文件夹及其子文件夹和文件,而使用“mv”命令可以... 目录linux复制文件夹并重命名我们需要使用“cp”命令来复制文件夹我们还可以结合使用“mv”命令总

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多