版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_32768743/article/details/89047038
代码为
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
}
#endif
int main() {
int64_t duration = 0;
av_register_all();
AVFormatContext *pFormatCtx = avformat_alloc_context();
avformat_open_input(&pFormatCtx, "/home/pikachu/Music/录音/23.wav", NULL, NULL);
if (pFormatCtx) {
avformat_find_stream_info(pFormatCtx, NULL);
duration = pFormatCtx->duration / 1000;
}
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
std::cout << "duration: " << duration << std::endl;
return 0;
}
配置为
cmake_minimum_required(VERSION 3.13)
project(voice_demo)
set(CMAKE_CXX_STANDARD 17)
set(TARGET_NAME voice_demo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(3rd_lib REQUIRED
libavformat libavcodec libavutil
)
add_executable(${TARGET_NAME} main.cpp)
target_include_directories(${TARGET_NAME} PUBLIC ${3rd_lib_INCLUDE_DIRS} )
target_link_libraries(${TARGET_NAME} ${3rd_lib_LIBRARIES} )
最后的效果
这里注意比较坑的地方是
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
}
#endif
不然会报
对‘av_register_all()’未定义的引用