C++ffmpeg视频编码
时间: 2023-11-25 21:52:24 浏览: 112
ffmpeg编码的代码
以下是使用C++和FFmpeg进行视频编码的基本步骤:
1.导入FFmpeg库并初始化:
```c++
#include <iostream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(int argc, char* argv[]) {
av_register_all();
avcodec_register_all();
avformat_network_init();
return 0;
}
```
2.打开输入文件并获取输入格式:
```c++
AVFormatContext* inputFormatContext = NULL;
if (avformat_open_input(&inputFormatContext, inputFileName, NULL, NULL) != 0) {
std::cout << "Could not open input file " << inputFileName << std::endl;
return -1;
}
if (avformat_find_stream_info(inputFormatContext, NULL) < 0) {
std::cout << "Could not find stream information" << std::endl;
return -1;
}
```
3.查找视频流并获取编解码器:
```c++
AVCodec* codec = NULL;
int videoStreamIndex = -1;
for (int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
codec = avcodec_find_decoder(inputFormatContext->streams[i]->codecpar->codec_id);
if (codec == NULL) {
std::cout << "Unsupported codec!" << std::endl;
return -1;
}
break;
}
}
if (videoStreamIndex == -1) {
std::cout << "Could not find video stream!" << std::endl;
return -1;
}
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (codecContext == NULL) {
std::cout << "Could not allocate codec context!" << std::endl;
return -1;
}
if (avcodec_parameters_to_context(codecContext, inputFormatContext->streams[videoStreamIndex]->codecpar) < 0) {
std::cout << "Could not copy codec parameters to codec context!" << std::endl;
return -1;
}
if (avcodec_open2(codecContext, codec, NULL) < 0) {
std::cout << "Could not open codec!" << std::endl;
return -1;
}
```
4.创建输出文件并获取输出格式:
```c++
AVFormatContext* outputFormatContext = NULL;
if (avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, outputFileName) < 0) {
std::cout << "Could not allocate output format context!" << std::endl;
return -1;
}
AVOutputFormat* outputFormat = outputFormatContext->oformat;
if (outputFormat->video_codec == AV_CODEC_ID_NONE) {
std::cout << "Could not find suitable video codec!" << std::endl;
return -1;
}
AVStream* outputStream = avformat_new_stream(outputFormatContext, NULL);
if (outputStream == NULL) {
std::cout << "Could not allocate output stream!" << std::endl;
return -1;
}
AVCodecContext* outputCodecContext = avcodec_alloc_context3(NULL);
if (outputCodecContext == NULL) {
std::cout << "Could not allocate output codec context!" << std::endl;
return -1;
}
outputCodecContext->codec_id = outputFormat->video_codec;
outputCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
outputCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
outputCodecContext->width = codecContext->width;
outputCodecContext->height = codecContext->height;
outputCodecContext->time_base = codecContext->time_base;
outputCodecContext->bit_rate = 400000;
outputCodecContext->gop_size = 10;
outputCodecContext->max_b_frames = 1;
if (outputFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
outputCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (avcodec_open2(outputCodecContext, NULL, NULL) < 0) {
std::cout << "Could not open output codec!" << std::endl;
return -1;
}
if (avcodec_parameters_from_context(outputStream->codecpar, outputCodecContext) < 0) {
std::cout << "Could not copy codec parameters to output stream!" << std::endl;
return -1;
}
```
5.创建SwsContext并分配缓冲区:
```c++
SwsContext* swsContext = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, outputCodecContext->width, outputCodecContext->height, outputCodecContext->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
if (swsContext == NULL) {
std::cout << "Could not create SwsContext!" << std::endl;
return -1;
}
AVFrame* inputFrame = av_frame_alloc();
if (inputFrame == NULL) {
std::cout << "Could not allocate input frame!" << std::endl;
return -1;
}
AVFrame* outputFrame = av_frame_alloc();
if (outputFrame == NULL) {
std::cout << "Could not allocate output frame!" << std::endl;
return -1;
}
outputFrame->format = outputCodecContext->pix_fmt;
outputFrame->width = outputCodecContext->width;
outputFrame->height = outputCodecContext->height;
if (av_frame_get_buffer(outputFrame, 32) < 0) {
std::cout << "Could not allocate output frame buffer!" << std::endl;
return -1;
}
```
6.读取输入文件并编码输出文件:
```c++
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
while (av_read_frame(inputFormatContext, &packet) >= 0) {
if (packet.stream_index == videoStreamIndex) {
if (avcodec_send_packet(codecContext, &packet) < 0) {
std::cout << "Error sending a packet for decoding!" << std::endl;
break;
}
while (avcodec_receive_frame(codecContext, inputFrame) == 0) {
sws_scale(swsContext, inputFrame->data, inputFrame->linesize, 0, codecContext->height, outputFrame->data, outputFrame->linesize);
outputFrame->pts = av_rescale_q(inputFrame->pts, codecContext->time_base, outputCodecContext->time_base);
if (avcodec_send_frame(outputCodecContext, outputFrame) < 0) {
std::cout << "Error sending a frame for encoding!" << std::endl;
break;
}
while (avcodec_receive_packet(outputCodecContext, &packet) == 0) {
av_packet_rescale_ts(&packet, outputCodecContext->time_base, outputStream->time_base);
packet.stream_index = outputStream->index;
if (av_interleaved_write_frame(outputFormatContext, &packet) < 0) {
std::cout << "Error while writing output packet!" << std::endl;
break;
}
av_packet_unref(&packet);
}
}
}
av_packet_unref(&packet);
}
```
7.释放资源:
```c++
avformat_close_input(&inputFormatContext);
avformat_free_context(inputFormatContext);
avcodec_free_context(&codecContext);
avformat_free_context(outputFormatContext);
avcodec_free_context(&outputCodecContext);
av_frame_free(&inputFrame);
av_frame_free(&outputFrame);
sws_freeContext(swsContext);
```
阅读全文