当前位置:   article > 正文

用QT调用FFMPEG的接口_qt 调用ffmpeg

qt 调用ffmpeg

1、调用FFmpeg接口

打开Qt,新建工程;

在工程的pro文件中,添加如下字段;

  1. TEMPLATE = app
  2. CONFIG += console
  3. CONFIG -= app_bundle
  4. CONFIG -= qt
  5. SOURCES += \
  6. main.c
  7. INCLUDEPATH += /home/wxw/ffmpeg_build/include
  8. LIBS += /home/wxw/ffmpeg_build/lib/libavcodec.a \
  9. /home/wxw/ffmpeg_build/lib/libavdevice.a \
  10. /home/wxw/ffmpeg_build/lib/libavfilter.a \
  11. /home/wxw/ffmpeg_build/lib/libavformat.a \
  12. /home/wxw/ffmpeg_build/lib/libavutil.a \
  13. /home/wxw/ffmpeg_build/lib/libswresample.a \
  14. /home/wxw/ffmpeg_build/lib/libswscale.a

主要目的是:添加FFMEEG的库路径和头文件路径,实际地址根据不同的客户端环境,会有差异。(动态库或静态库,都可选)

在main文件中,直接调用ffmpeg的api,编译运行即可。如下:

  1. #include <stdio.h>
  2. // 包含ffmpeg头⽂件
  3. #include "libavutil/avutil.h"
  4. int main()
  5. {
  6. printf("Hello FFMPEG, version is %s\n", av_version_info());
  7. return 0;
  8. }

2、安装SDL库

sdl 下载地址:https://www.libsdl.org/download-2.0.php

下载SDL源码库,SDL2-2.0.10.tar.gz 2.

解压,进入目录中,然后依次执⾏命令

./autogen.sh

#实际路径依环境不同而定,指定编译安装的库目录和bin文件目录

./configure --prefix=/home/wxw/ffmpeg_build --bindir=/home/wxw/bin

make

sudo make install

3、Qt调用SDL库显示一个空窗口

打开Qt,新建工程;

在工程的pro文件中,添加如下字段;

  1. TEMPLATE = app
  2. CONFIG += console
  3. CONFIG -= app_bundle
  4. CONFIG -= qt
  5. SOURCES += \
  6. main.c
  7. INCLUDEPATH += /home/wxw/ffmpeg_build/include
  8. LIBS += /home/wxw/ffmpeg_build/lib/libSDL2.so

主要目的是:添加SDL的库路径和头文件路径,实际地址根据不同的客户端环境,会有差异。

在main.c,编译运行会显示一个空窗口。代码如下:

  1. #include <stdio.h>
  2. #include "SDL2/SDL.h"
  3. int main()
  4. {
  5. //声明一个窗口句柄
  6. SDL_Window *window_ptr = NULL;
  7. printf("test SDL demo 1.\n");
  8. //创建窗口
  9. SDL_Init(SDL_INIT_VIDEO);
  10. window_ptr = SDL_CreateWindow("Basic Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
  11. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
  12. if(window_ptr == NULL)
  13. {
  14. printf("%s: Call 'SDL_CreateWindow' failure. \n", __FUNCTION__);
  15. goto ERR_EXIT;
  16. }
  17. SDL_Delay(3000);
  18. //销毁窗口,释放资源
  19. SDL_DestroyWindow(window_ptr);
  20. SDL_Quit();
  21. ERR_EXIT:
  22. return 0;
  23. }

 4、Qt调用SDL库播放yuv视频

打开Qt,新建工程;

在工程的pro文件中,添加如下字段;

  1. TEMPLATE = app
  2. CONFIG += console
  3. CONFIG -= app_bundle
  4. CONFIG -= qt
  5. SOURCES += \
  6. main.c
  7. INCLUDEPATH += /home/wxw/ffmpeg_build/include
  8. LIBS += /home/wxw/ffmpeg_build/lib/libSDL2.so

主要目的是:添加SDL的库路径和头文件路径,实际地址根据不同的客户端环境,会有差异。

在main.c,编译运行会显示一个YUV视频播放窗口。重点是:SDL库的初始化和操作流程、YUV数据读取和渲染、窗口调整操作、刷新逻辑等。

代码如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "SDL2/SDL.h"
  4. //自定义消息类型
  5. #define REFRESH_EVENT (SDL_USEREVENT + 1) // 请求画面刷新事件
  6. #define QUIT_EVENT (SDL_USEREVENT + 2) // 退出事件
  7. // YUV像素分辨率
  8. #define YUV_WIDTH 320
  9. #define YUV_HEIGHT 240
  10. //定义YUV格式
  11. #define YUV_FORMAT SDL_PIXELFORMAT_IYUV
  12. int s_thread_exit = 0; // 退出标志 = 1则退出
  13. int refresh_video_timer(void *data)
  14. {
  15. while (!s_thread_exit)
  16. {
  17. SDL_Event event;
  18. event.type = REFRESH_EVENT;
  19. SDL_PushEvent(&event);
  20. SDL_Delay(40);
  21. }
  22. s_thread_exit = 0;
  23. //push quit event
  24. SDL_Event event;
  25. event.type = QUIT_EVENT;
  26. SDL_PushEvent(&event);
  27. return 0;
  28. }
  29. int main(int argc, char* argv[])
  30. {
  31. //初始化 SDL
  32. if(SDL_Init(SDL_INIT_VIDEO))
  33. {
  34. fprintf( stderr, "Could not initialize SDL - %s\n", SDL_GetError());
  35. return -1;
  36. }
  37. // SDL
  38. SDL_Event event; // 事件
  39. SDL_Rect rect; // 矩形
  40. SDL_Window *window = NULL; // 窗口
  41. SDL_Renderer *renderer = NULL; // 渲染
  42. SDL_Texture *texture = NULL; // 纹理
  43. SDL_Thread *timer_thread = NULL; // 请求刷新线程
  44. uint32_t pixformat = YUV_FORMAT; // YUV420P,即是SDL_PIXELFORMAT_IYUV
  45. // 1. YUV的分辨率
  46. int video_width = YUV_WIDTH;
  47. int video_height = YUV_HEIGHT;
  48. // 2.显示窗口的分辨率
  49. int win_width = YUV_WIDTH;
  50. int win_height = YUV_WIDTH;
  51. // YUV文件句柄
  52. FILE *video_fd = NULL;
  53. const char *yuv_path = "/home/wxw/QtProject/build-test_ffmpeg-Desktop_Qt_5_12_10_GCC_64bit-Debug/yuv420p_320x240.yuv";
  54. size_t video_buff_len = 0;
  55. uint8_t *video_buf = NULL; //读取数据后先把放到buffer里面
  56. // 我们测试的文件是YUV420P格式
  57. uint32_t y_frame_len = video_width * video_height;
  58. uint32_t u_frame_len = video_width * video_height / 4;
  59. uint32_t v_frame_len = video_width * video_height / 4;
  60. uint32_t yuv_frame_len = y_frame_len + u_frame_len + v_frame_len;
  61. //创建窗口
  62. window = SDL_CreateWindow("Simplest YUV Player",
  63. SDL_WINDOWPOS_UNDEFINED,
  64. SDL_WINDOWPOS_UNDEFINED,
  65. video_width, video_height,
  66. SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
  67. if(!window)
  68. {
  69. fprintf(stderr, "SDL: could not create window, err:%s\n",SDL_GetError());
  70. goto _FAIL;
  71. }
  72. // 基于窗口创建渲染器
  73. renderer = SDL_CreateRenderer(window, -1, 0);
  74. // 基于渲染器创建纹理
  75. texture = SDL_CreateTexture(renderer,
  76. pixformat,
  77. SDL_TEXTUREACCESS_STREAMING,
  78. video_width,
  79. video_height);
  80. // 分配空间
  81. video_buf = (uint8_t*)malloc(yuv_frame_len);
  82. if(!video_buf)
  83. {
  84. fprintf(stderr, "Failed to alloce yuv frame space!\n");
  85. goto _FAIL;
  86. }
  87. // 打开YUV文件
  88. video_fd = fopen(yuv_path, "rb");
  89. if( !video_fd )
  90. {
  91. fprintf(stderr, "Failed to open yuv file\n");
  92. goto _FAIL;
  93. }
  94. // 创建请求刷新线程
  95. timer_thread = SDL_CreateThread(refresh_video_timer,
  96. NULL,
  97. NULL);
  98. while (1)
  99. {
  100. // 收取SDL系统里面的事件
  101. SDL_WaitEvent(&event);
  102. if(event.type == REFRESH_EVENT) // 画面刷新事件
  103. {
  104. video_buff_len = fread(video_buf, 1, yuv_frame_len, video_fd);
  105. if(video_buff_len <= 0)
  106. {
  107. fprintf(stderr, "Failed to read data from yuv file!\n");
  108. goto _FAIL;
  109. }
  110. // 设置纹理的数据 video_width = 320, plane
  111. SDL_UpdateTexture(texture, NULL, video_buf, video_width);
  112. // 显示区域,可以通过修改w和h进行缩放
  113. rect.x = 0;
  114. rect.y = 0;
  115. float w_ratio = win_width * 1.0 /video_width;
  116. float h_ratio = win_height * 1.0 /video_height;
  117. // 320x240 怎么保持原视频的宽高比例
  118. rect.w = video_width * w_ratio;
  119. rect.h = video_height * h_ratio;
  120. // rect.w = video_width * 0.5;
  121. // rect.h = video_height * 0.5;
  122. // 清除当前显示
  123. SDL_RenderClear(renderer);
  124. // 将纹理的数据拷贝给渲染器
  125. SDL_RenderCopy(renderer, texture, NULL, &rect);
  126. // 显示
  127. SDL_RenderPresent(renderer);
  128. }
  129. else if(event.type == SDL_WINDOWEVENT)
  130. {
  131. //If Resize
  132. SDL_GetWindowSize(window, &win_width, &win_height);
  133. printf("SDL_WINDOWEVENT win_width:%d, win_height:%d\n",win_width, win_height );
  134. }
  135. else if(event.type == SDL_QUIT) //退出事件
  136. {
  137. s_thread_exit = 1;
  138. }
  139. else if(event.type == QUIT_EVENT)
  140. {
  141. break;
  142. }
  143. }
  144. _FAIL:
  145. s_thread_exit = 1; // 保证线程能够退出
  146. // 释放资源
  147. if(timer_thread)
  148. SDL_WaitThread(timer_thread, NULL); // 等待线程退出
  149. if(video_buf)
  150. free(video_buf);
  151. if(video_fd)
  152. fclose(video_fd);
  153. if(texture)
  154. SDL_DestroyTexture(texture);
  155. if(renderer)
  156. SDL_DestroyRenderer(renderer);
  157. if(window)
  158. SDL_DestroyWindow(window);
  159. SDL_Quit();
  160. return 0;
  161. }

  5、Qt解析yuv视频获取AAC音频

打开Qt,新建工程;

在工程的pro文件中,添加如下字段;

  1. TEMPLATE = app
  2. CONFIG += console
  3. CONFIG -= app_bundle
  4. CONFIG -= qt
  5. SOURCES += \
  6. main.c
  7. INCLUDEPATH += /home/wxw/ffmpeg_build/include
  8. LIBS += \
  9. /home/wxw/ffmpeg_build/lib/libavformat.a \
  10. /home/wxw/ffmpeg_build/lib/libavcodec.a \
  11. /home/wxw/ffmpeg_build/lib/libavdevice.a \
  12. /home/wxw/ffmpeg_build/lib/libavfilter.a \
  13. /home/wxw/ffmpeg_build/lib/libavutil.a \
  14. /home/wxw/ffmpeg_build/lib/libswresample.a \
  15. /home/wxw/ffmpeg_build/lib/libswscale.a \
  16. /home/wxw/ffmpeg_build/lib/libmp3lame.a \
  17. /home/wxw/ffmpeg_build/lib/libfdk-aac.a \
  18. /home/wxw/ffmpeg_build/lib/libopus.a \
  19. /home/wxw/ffmpeg_build/lib/libvpx.a \
  20. /home/wxw/ffmpeg_build/lib/libx265.a \
  21. /home/wxw/ffmpeg_build/lib/libx264.a
  22. LIBS += -lpthread -lnuma -lz -lm -lva-drm -lva -lva-x11 -lvdpau -lX11 -lXext -ldl -lvorbisenc -lvorbis

主要目的是:添加FFMPEG的库路径和头文件路径,实际地址根据不同的客户端环境,会有差异。

在main.c,编译运行会读取输入文件并抽出音频数据,存放到一个新文件中。重点是:FFMPEG接口调用初始化,获取文件信息,读取文件内容并查找音频分量,将原始音频数据拼接上ADTS头存放到aac文件中等。

运行时,需要在指令中添加目标文件的地址,这里用了绝对路径,相对路径不知为何行不通,先不管了,这些不重要。如下图:

dd0bdc35e17948bfa22a23f10f9f8f3d.png

代码如下:

  1. #include <stdio.h>
  2. #include "libavutil/log.h"
  3. #include "libavformat/avio.h"
  4. #include "libavformat/avformat.h"
  5. #define ADTS_HEADER_LEN 7;
  6. const int sampling_frequencies[] = {
  7. 96000, // 0x0
  8. 88200, // 0x1
  9. 64000, // 0x2
  10. 48000, // 0x3
  11. 44100, // 0x4
  12. 32000, // 0x5
  13. 24000, // 0x6
  14. 22050, // 0x7
  15. 16000, // 0x8
  16. 12000, // 0x9
  17. 11025, // 0xa
  18. 8000 // 0xb
  19. // 0xc d e f是保留的
  20. };
  21. int adts_header(char * const p_adts_header, const int data_length,
  22. const int profile, const int samplerate,
  23. const int channels)
  24. {
  25. int sampling_frequency_index = 3; // 默认使用48000hz
  26. int adtsLen = data_length + 7;
  27. int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);
  28. int i = 0;
  29. for(i = 0; i < frequencies_size; i++)
  30. {
  31. if(sampling_frequencies[i] == samplerate)
  32. {
  33. sampling_frequency_index = i;
  34. break;
  35. }
  36. }
  37. if(i >= frequencies_size)
  38. {
  39. printf("unsupport samplerate:%d\n", samplerate);
  40. return -1;
  41. }
  42. p_adts_header[0] = 0xff; //syncword:0xfff 高8bits
  43. p_adts_header[1] = 0xf0; //syncword:0xfff 低4bits
  44. p_adts_header[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit
  45. p_adts_header[1] |= (0 << 1); //Layer:0 2bits
  46. p_adts_header[1] |= 1; //protection absent:1 1bit
  47. p_adts_header[2] = (profile)<<6; //profile:profile 2bits
  48. p_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index 4bits
  49. p_adts_header[2] |= (0 << 1); //private bit:0 1bit
  50. p_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels 高1bit
  51. p_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bits
  52. p_adts_header[3] |= (0 << 5); //original:0 1bit
  53. p_adts_header[3] |= (0 << 4); //home:0 1bit
  54. p_adts_header[3] |= (0 << 3); //copyright id bit0 1bit
  55. p_adts_header[3] |= (0 << 2); //copyright id start0 1bit
  56. p_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bits
  57. p_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bits
  58. p_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value3bits
  59. p_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bits
  60. p_adts_header[6] = 0xfc; //11111100//buffer fullness:0x7ff 低6bits
  61. // number_of_raw_data_blocks_in_frame:
  62. // 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧。
  63. return 0;
  64. }
  65. int main(int argc, char *argv[])
  66. {
  67. int ret = -1;
  68. char errors[1024];
  69. char *in_filename = NULL;
  70. char *aac_filename = NULL;
  71. FILE *aac_fd = NULL;
  72. int audio_index = -1;
  73. int len = 0;
  74. AVFormatContext *ifmt_ctx = NULL;
  75. AVPacket pkt;
  76. // 设置打印级别
  77. av_log_set_level(AV_LOG_DEBUG);
  78. if(argc < 3)
  79. {
  80. av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");
  81. return -1;
  82. }
  83. in_filename = argv[1]; // 输入文件
  84. aac_filename = argv[2]; // 输出文件
  85. if(in_filename == NULL || aac_filename == NULL)
  86. {
  87. av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");
  88. return -1;
  89. }
  90. aac_fd = fopen(aac_filename, "wb");
  91. if (!aac_fd)
  92. {
  93. av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", aac_filename);
  94. return -1;
  95. }
  96. // 打开输入文件
  97. if((ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL)) < 0)
  98. {
  99. av_strerror(ret, errors, 1024);
  100. av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
  101. in_filename,
  102. ret,
  103. errors);
  104. return -1;
  105. }
  106. // 获取解码器信息
  107. if((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0)
  108. {
  109. av_strerror(ret, errors, 1024);
  110. av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
  111. in_filename,
  112. ret,
  113. errors);
  114. return -1;
  115. }
  116. // dump媒体信息
  117. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  118. // 初始化packet
  119. av_init_packet(&pkt);
  120. // 查找audio对应的steam index
  121. audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
  122. if(audio_index < 0)
  123. {
  124. av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
  125. av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
  126. in_filename);
  127. return AVERROR(EINVAL);
  128. }
  129. // 打印AAC级别
  130. printf("audio profile:%d, FF_PROFILE_AAC_LOW:%d\n",
  131. ifmt_ctx->streams[audio_index]->codecpar->profile,
  132. FF_PROFILE_AAC_LOW);
  133. if(ifmt_ctx->streams[audio_index]->codecpar->codec_id != AV_CODEC_ID_AAC)
  134. {
  135. printf("the media file no contain AAC stream, it's codec_id is %d\n",
  136. ifmt_ctx->streams[audio_index]->codecpar->codec_id);
  137. goto failed;
  138. }
  139. // 读取媒体文件,并把aac数据帧写入到本地文件
  140. while(av_read_frame(ifmt_ctx, &pkt) >=0 )
  141. {
  142. if(pkt.stream_index == audio_index)
  143. {
  144. char adts_header_buf[7] = {0};
  145. adts_header(adts_header_buf, pkt.size,
  146. ifmt_ctx->streams[audio_index]->codecpar->profile,
  147. ifmt_ctx->streams[audio_index]->codecpar->sample_rate,
  148. ifmt_ctx->streams[audio_index]->codecpar->channels);
  149. fwrite(adts_header_buf, 1, 7, aac_fd); // 写adts header , ts流不适用,ts流分离出来的packet带了adts header
  150. len = fwrite( pkt.data, 1, pkt.size, aac_fd); // 写adts data
  151. if(len != pkt.size)
  152. {
  153. av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
  154. len,
  155. pkt.size);
  156. }
  157. }
  158. av_packet_unref(&pkt);
  159. }
  160. failed:
  161. // 关闭输入文件
  162. if(ifmt_ctx)
  163. {
  164. avformat_close_input(&ifmt_ctx);
  165. }
  166. if(aac_fd)
  167. {
  168. fclose(aac_fd);
  169. }
  170. return 0;
  171. }

另:关于ADTS格式定义,详见《AAC文件的ADTS格式解析》

靠谱推荐:Free Pos,微信JOCIHEZ

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/758448
推荐阅读
相关标签
  

闽ICP备14008679号