提出問題
由於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.o
${PROJECT_SOURCE_DIR}/fftools/ffmpeg_filter.o
${PROJECT_SOURCE_DIR}/fftools/ffmpeg_hw.o
${PROJECT_SOURCE_DIR}/fftools/cmdutils.o
)
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模塊來使用。