ubuntu下cmake引用ffmpeg+opencv+boost以及一个调用ffmpeg的例子

时间:2024-04-03 11:55:26

前言

 

先要复杂点的例子可以看:

https://blog.csdn.net/JasonDing1354/article/details/41212425

https://blog.csdn.net/leixiaohua1020/article/details/44084321

 

不过先说明,要看看这些文章的时间,如果用的是很早之前的ffmpeg的版本的话

那么有些api是改了的,估计没办法可以运行得通。

 

正文

好了,先看看qt的目录结构:

ubuntu下cmake引用ffmpeg+opencv+boost以及一个调用ffmpeg的例子

 

CMakeLists.txt的内容如下--当然了,我这里删除掉一些没用的东西,只保留需要的东西:

cmake_minimum_required(VERSION 3.5)
SET(SOURCE_FILES
main.cpp
)
project(00_LearnOpenCV LANGUAGES CXX)


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)



find_package(X11 REQUIRED)

# ---[ FFMPEG

set( FFMPEG_BUILD_PATH /home/too-white/ffmpeg-build/target)
set( FFMPEG_INCLUDE_PATH ${FFMPEG_BUILD_PATH}/include)
set( FFMPEG_LIB_PATH ${FFMPEG_BUILD_PATH}/lib)

##头文件
include_directories(SYSTEM ${FFMPEG_INCLUDE_PATH})

list(APPEND LINKER_LIBS
    ${FFMPEG_LIB_PATH}/libavcodec.so
    ${FFMPEG_LIB_PATH}/libavformat.so
    ${FFMPEG_LIB_PATH}/libavutil.so
    ${FFMPEG_LIB_PATH}/libswresample.so
    ${FFMPEG_LIB_PATH}/libswscale.so
    )


# ---[ OpenCV
find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio)
if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found
    find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
endif()
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${OpenCV_LIBS})
message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")

# ---[ Boost
## 引入boost类库
#导入需要使用的boost库,如regex filesystem thread
find_package(Boost REQUIRED COMPONENTS regex filesystem thread )
if (NOT Boost_FOUND)
    message("Boost类库找不到---还没有配置吗??")
endif ()
#导入头文件
include_directories(${Boost_INCLUDE_DIRS})
#Boost_LIBRARIES的库顺便添加到liker_libs里面去。
list(APPEND LINKER_LIBS ${Boost_LIBRARIES})

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)


add_executable(00_LearnOpenCV ${SOURCE_FILES})
target_include_directories(00_LearnOpenCV PRIVATE ${INCLUDE_DIR})
target_link_libraries(00_LearnOpenCV
    ${LINKER_LIBS}
    ${SDL2_LIBRARIES}
    pthread
    swresample
    m
    swscale
    z
    lzma
    rt
    )


测试用代码:

#include <stdio.h>
#include "ffmpegdecode.h"
#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
};
#endif
#endif



int is_h264_file(char *filename)
{
    AVFormatContext *ifmt_ctx = NULL;
    int ret = 0;


    if ((ret = avformat_open_input(&ifmt_ctx, filename, 0, 0)) < 0) {

            printf( "Could not open input file.");

            return 0;

        }
    //	printf("1ifmt_ctx->video_codec_id:%d\n",ifmt_ctx->video_codec_id);
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {

            printf( "Failed to retrieve input stream information");

            avformat_close_input(&ifmt_ctx);
            return 0;

        }

    //ifmt_ctx->streams[0]->codecpar->codec_id;

    printf("3ifmt_ctx->video_codec_id:%d\n",ifmt_ctx->streams[0]->codecpar->codec_id);


    if(ifmt_ctx->streams[0]->codecpar->codec_id==AV_CODEC_ID_H264)
    {
        printf("is h264 file\n");
        avformat_close_input(&ifmt_ctx);
        return 1;
    }

    else
    {
        printf("is not h264 file\n");
        avformat_close_input(&ifmt_ctx);
        return 0;
    }

}



int main(int argc, char* argv[])

{
    char * filepath="/home/too-white/temp/002.mp4";
    is_h264_file(filepath);
}

执行结果:

ubuntu下cmake引用ffmpeg+opencv+boost以及一个调用ffmpeg的例子

 

这个代码其实只是读取文件的编码判断是不是h264的格式,就用来测试一下。