一 ffmpeg安装:
参考链接 https://blog.csdn.net/danfengw/article/details/62887997
注意:ffmpeg没有x264编码(带有x264解码)需要自己安装,上面链接提供安装的指令;
都安装完成后需要添加环境变量
sudo vim ~/.bashrc
在末尾添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/wping/ffmpeg_build/lib/
export PKG_CONFIG_PATH=/home/wping/ffmpeg_build/lib/pkgconfig/
二 正文
所有文件内容如上
-
CMakeLists.txt文件
这里有个坑刚开始怎么也编译不了最后用pkg-config编译
在(ffmpeg_build/lib)中有个pkgconfig文件
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
project(demo)
set(CMAKE_C_COMPILER "g++")
add_definitions(`pkg-config --cflags --libs x264 libswscale libavformat libavutil libavcodec`)
include(FindPkgConfig)
pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVFORMAT REQUIRED libavformat)
pkg_check_modules(AVSCALE REQUIRED libswscale)
pkg_check_modules(AVDEV REQUIRED libavdevice)
pkg_check_modules(AVUTIL REQUIRED libavutil)
pkg_check_modules(AVX REQUIRED x264)
pkg_check_modules(AVPOST REQUIRED libpostproc)
set(INC_DIR /home/wping/ffmpeg_build/include/) //在这里注意路径问题
set(LINK_DIR /home/wping/ffmpeg_build/lib/pkgconfig)
include_directories(${INC_DIR} ./include)
link_directories(${AVX_LIBRARY_DIRS} ${AVSCALE_LIBRARY_DIRS} ${AVFORMAT_LIBRARY_DIRS} ${AVUTIL_LIBRARY_DIRS} ${AVCODEC_LIBRARY_DIRS})
aux_source_directory(src SOURCE_FILES)
set(SOURCE ${SOURCE_FILES})
add_executable(demo ${SOURCE})
target_link_libraries(demo ${AVX_LDFLAGS} ${AVSCALE_LDFLAGS} ${AVFORMAT_LDFLAGS} ${AVUTIL_LDFLAGS} ${AVCODEC_LDFLAGS} )
- main.cpp
#include<stdio.h>
#include"rtspSource.h"
int main(){
char fileName[]= "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";//自己可以选择源流
rtspSource(fileName);
}
3.rtspSource.cpp
void rtspSource(char* fileName){
FILE *fpSaveH264;
if((fpSaveH264= fopen("saveH264.h264", "ab"))== NULL) return;
FILE *fpSaveYUV;
if(( fpSaveYUV= fopen("saveYUV.yuv", "ab")) == NULL) return;
ffmpegCommonSet *decodePro= new ffmpegCommonSet();
ffmpegCommonSet *encodePro= new ffmpegCommonSet();
h264decodeInit(decodePro, fileName);
h264encodeInit(encodePro, decodePro->pCodecCtx, decodePro->pCodecCtx->width, decodePro->pCodecCtx->height);
for(; ;){
av_init_packet(&decodePro->packet);
if(av_read_frame(decodePro->pFormatCtx, &decodePro->packet)>= 0){