基本思想:记录学习夏曹俊ffmpeg基本函数使用,window11+clion_mingw+ffmpeg库,基本函数使用和格式转换
一、学习大佬的如何使用av_frame_alloc()
cmakelists.txt
cmake_minimum_required(VERSION 3.16)
project(window11)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_SOURCE_DIR}/include)
set(OpenCV_DIR "D:\\Opencv455\\buildMinGW")#改为mingw-bulid的位置
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
add_library(libavformat STATIC IMPORTED)
set_target_properties(libavformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libavformat.dll.a)
add_library(libavdevice STATIC IMPORTED)
set_target_properties(libavdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libavdevice.dll.a)
add_library(libavcodec STATIC IMPORTED)
set_target_properties(libavcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libavcodec.dll.a)
add_library(libavfilter STATIC IMPORTED)
set_target_properties(libavfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libavfilter.dll.a)
add_library(libavutil STATIC IMPORTED)
set_target_properties(libavutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libavutil.dll.a)
add_library(libswresample STATIC IMPORTED)
set_target_properties(libswresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libswresample.dll.a)
add_library(libswscale STATIC IMPORTED)
set_target_properties(libswscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libswscale.dll.a)
add_executable(window11 main.cpp )
target_link_libraries(window11 ${OpenCV_LIBS}
libavformat
libavdevice
libavcodec
libavfilter
libavutil
libswresample
libswscale
)
main.cpp
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
using namespace std;
using namespace cv;
int main() {
std::cout<<"first ffmpeg"<<std::endl;
// std::cout<<avcodec_configuration()<<std::endl;
auto frame1=av_frame_alloc();
frame1->width=400;
frame1->height=300;
frame1->format=AV_PIX_FMT_ARGB;
int re=av_frame_get_buffer(frame1,0);
if(re!=0){
char buff[1024]={0};
av_strerror(re,buff,sizeof(buff));
std::cout<<buff<<std::endl;
}
std::cout<<"frame ->line="<<frame1->linesize[0]<<std::endl;
av_frame_free(&frame1);
return 0;
}
二、学习大佬如何使用 sws_getCachedContext
cmakelists.txt
cmake_minimum_required(VERSION 3.21)
project(untitled1)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled1 main.cpp)
target_link_libraries(untitled1
${OpenCV_LIBS}
-lavformat -lavcodec -lswscale -lavutil -lz
)
main.cpp
#include <iostream>标签:11,IMPORTED,FFMPEG,height,width,视频格式,rgba,include,yuv From: https://blog.51cto.com/u_12504263/6038753
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
#include <fstream>
using namespace std;
using namespace cv;
int main() {
std::cout << "first ffmpeg" << std::endl;
// std::cout<<avcodec_configuration()<<std::endl;
int width = 1920;
int height = 1080;
int rgba_width = 1920;
int rgba_height = 1080;
unsigned char *yuv[3] = {0};
yuv[0] = new unsigned char[width * height];//y
yuv[1] = new unsigned char[width * height / 4];//y
yuv[2] = new unsigned char[width * height / 4];//y
unsigned char *rgba = new unsigned char[rgba_width * rgba_height * 4];
int rgba_linesize = rgba_width * 4;
ifstream ifs;
ifs.open("/home/ubuntu/1920x1080_min.yuv", ios::binary);
if (!ifs) {
cerr << "open fail" << endl;
return -1;
}
int yuv_linesize[3] = {width , width /2, width / 2};
SwsContext *yuv2rgb = NULL;
while (1) {
ifs.read((char *) yuv[0], width * height);
ifs.read((char *) yuv[1], width * height / 4);
ifs.read((char *) yuv[2], width * height / 4);
if (ifs.gcount() == 0) break;
yuv2rgb = sws_getCachedContext(yuv2rgb,
width,
height,
AV_PIX_FMT_YUV420P,
rgba_width,
rgba_height,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
0, 0, 0
);
if (!yuv2rgb) {
cerr << " paramter error" << endl;
return -1;
}
unsigned char *data[1];
data[0] = rgba;
int lines[3] = {rgba_linesize,rgba_linesize,rgba_linesize};
int re = sws_scale(yuv2rgb, yuv, yuv_linesize, 0, height, data, lines);
std::cout << re << std::endl;
cv::Mat RGBAImg;
RGBAImg.create(height , width, CV_8UC4); //
RGBAImg.data = data[0];
cv::Mat rgbImg = cv::Mat(height, width, CV_8UC3);
cv::cvtColor(RGBAImg, rgbImg, COLOR_RGBA2BGR);
if (rgbImg.empty()) {
printf("rgbImg empty\n");
}
cv::imshow("mpp", rgbImg);
cv::waitKey(1);
}
delete yuv[0];
delete yuv[1];
delete yuv[2];
delete rgba;
return 0;
}