背景
由于FFmpeg提供了几个命令行工具(最常用的三剑客:ffmpeg,ffplay,ffprobe)来提供给其他程序调用来实现快速开发,但这些进程只能满足一般的项目需求,有时侯,为了实现一些特色功能,又不想另写模块来实现,这时,一个好的实践是修改这些工具的源文件,比如ffmpeg.c。然后重新编译生成自已的工具进程来给其他模块来使用。下面就以最常用的ffmpeg来说明如何在Ubuntu18.04环境下使用Qt5.12.3的Qt Creator来对ffmpeg-4.1中的fftools/ffmpeg.c进行编译、调试、和生成app。
编译和安装ffmpeg-4.1
这个我之前的博客有写,网上也能找到很多。
创建CMakeLists.txt
为了方便,我直接在ffmpeg-4.1目录下创建”CMakeLists.txt”,内容如下: 这个是实际编译通过并正常进入调试模式的设置。网上很难找到。
cmake_minimum_required(VERSION 3.0.0)
project(qzmffmpeg VERSION 0.1) #compile .c pass.
set(CMAKE_C_STANDARD)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# src ========================================
set(SRCS
${PROJECT_SOURCE_DIR}/fftools/ffmpeg.c
${PROJECT_SOURCE_DIR}/fftools/ffmpeg_opt.c
${PROJECT_SOURCE_DIR}/fftools/ffmpeg_filter.c
${PROJECT_SOURCE_DIR}/fftools/ffmpeg_hw.c
${PROJECT_SOURCE_DIR}/fftools/cmdutils.c
)
add_executable(${PROJECT_NAME}
#${DIR_SRCS_CURRENT}
${SRCS}
)
target_link_libraries(${PROJECT_NAME} -lavcodec)
target_link_libraries(${PROJECT_NAME} -lavdevice)
target_link_libraries(${PROJECT_NAME} -lavfilter)
target_link_libraries(${PROJECT_NAME} -lavformat)
target_link_libraries(${PROJECT_NAME} -lavutil)
target_link_libraries(${PROJECT_NAME} -lpostproc)
target_link_libraries(${PROJECT_NAME} -lswresample)
target_link_libraries(${PROJECT_NAME} -lswscale)
target_link_libraries(${PROJECT_NAME} -lm)
target_link_libraries(${PROJECT_NAME} -lpthread)
使用Qt Creator建立一个PRJ来编译ffmpeg.c
把用Qt Creator加载CMakeLists.txt作为一个项目,先运行”Run CMake”,再运行“Build Project “qzmffmpeg”。如下图所示:
Build的Compile Output信息如下:
16:29:47: Running steps for project qzmffmpeg...
16:29:47: Starting: "/usr/local/bin/cmake" --build . --target clean
16:29:47: The process "/usr/local/bin/cmake" exited normally.
16:29:47: Starting: "/usr/local/bin/cmake" --build . --target all
[ 50%] Building C object CMakeFiles/qzmffmpeg.dir/fftools/ffmpeg.c.o
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c: In function ‘do_streamcopy’:
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:2080:5: warning: ‘av_copy_packet_side_data’ is deprecated [-Wdeprecated-declarations]
av_copy_packet_side_data(&opkt, pkt);
^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:319:0,
from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:
/home/xhj/qzm/ffmpeg-4.1/libavcodec/avcodec.h:4406:5: note: declared here
int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src);
^~~~~~~~~~~~~~~~~~~~~~~~
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c: In function ‘init_output_stream’:
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:3549:9: warning: ‘avcodec_copy_context’ is deprecated [-Wdeprecated-declarations]
ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
^~~
In file included from /home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:319:0,
from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:
/home/xhj/qzm/ffmpeg-4.1/libavcodec/avcodec.h:4178:5: note: declared here
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src);
^~~~~~~~~~~~~~~~~~~~
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:3549:9: warning: ‘codec’ is deprecated [-Wdeprecated-declarations]
ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
^~~
In file included from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:0:
/home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:878:21: note: declared here
AVCodecContext *codec;
^~~~~
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:3595:9: warning: ‘codec’ is deprecated [-Wdeprecated-declarations]
ost->st->codec->codec= ost->enc_ctx->codec;
^~~
In file included from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:0:
/home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:878:21: note: declared here
AVCodecContext *codec;
^~~~~
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c: In function ‘check_keyboard_interaction’:
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:3976:13: warning: ‘codec’ is deprecated [-Wdeprecated-declarations]
debug = input_streams[0]->st->codec->debug<<1;
^~~~~
In file included from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:0:
/home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:878:21: note: declared here
AVCodecContext *codec;
^~~~~
/home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:3999:13: warning: ‘codec’ is deprecated [-Wdeprecated-declarations]
input_streams[i]->st->codec->debug = debug;
^~~~~~~~~~~~~
In file included from /home/xhj/qzm/ffmpeg-4.1/fftools/ffmpeg.c:43:0:
/home/xhj/qzm/ffmpeg-4.1/libavformat/avformat.h:878:21: note: declared here
AVCodecContext *codec;
^~~~~
[100%] Linking C executable qzmffmpeg
[100%] Built target qzmffmpeg
16:29:48: The process "/usr/local/bin/cmake" exited normally.
16:29:48: Elapsed time: 00:01.
使用Qt Creator来调试和生成ffmpeg进程
经过上面的CMake和Build后,即可开始调试ffmpeg.c,如下图所示:

然后,我们就可以改写ffmpeg.c,加入我们的功能进去,并调试通过后,生成release的ffmpeg模块来使用。
转载
原文链接:https://blog.csdn.net/u012915636/article/details/90208644