c++ 使用ffmpeg实现h264硬编码代码
时间: 2023-08-16 16:10:16 浏览: 171
以下是使用FFmpeg实现H.264硬编码的C++代码示例:
```c++
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
}
#define INBUF_SIZE 4096
int main(int argc, char **argv) {
AVCodec *codec;
AVCodecContext *codec_context = NULL;
int ret, i, j, k, got_output;
FILE *f;
AVFrame *frame;
AVPacket pkt;
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
AVFormatContext *format_context = NULL;
AVStream *stream = NULL;
AVIOContext *io_context = NULL;
if (argc <= 2) {
std::cout << "Usage: " << argv[0] << " <input file> <output file>" << std::endl;
return 0;
}
av_register_all();
avcodec_register_all();
// Open input file
if (avformat_open_input(&format_context, argv[1], NULL, NULL) < 0) {
std::cerr << "Could not open input file" << std::endl;
return -1;
}
// Find stream info
if (avformat_find_stream_info(format_context, NULL) < 0) {
std::cerr << "Could not find stream info" << std::endl;
return -1;
}
// Find video stream
for (i = 0; i < format_context->nb_streams; i++) {
if (format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
stream = format_context->streams[i];
break;
}
}
if (stream == NULL) {
std::cerr << "Could not find video stream" << std::endl;
return -1;
}
// Find decoder
codec = avcodec_find_decoder(stream->codecpar->codec_id);
if (codec == NULL) {
std::cerr << "Could not find codec" << std::endl;
return -1;
}
// Allocate codec context
codec_context = avcodec_alloc_context3(codec);
if (codec_context == NULL) {
std::cerr << "Could not allocate codec context" << std::endl;
return -1;
}
// Fill codec context with stream parameters
if (avcodec_parameters_to_context(codec_context, stream->codecpar) < 0) {
std::cerr << "Could not fill codec context with stream parameters" << std::endl;
return -1;
}
// Open codec
if (avcodec_open2(codec_context, codec, NULL) < 0) {
std::cerr << "Could not open codec" << std::endl;
return -1;
}
// Open output file
if (avio_open(&io_context, argv[2], AVIO_FLAG_WRITE) < 0) {
std::cerr << "Could not open output file" << std::endl;
return -1;
}
// Allocate frame
frame = av_frame_alloc();
if (frame == NULL) {
std::cerr << "Could not allocate frame" << std::endl;
return -1;
}
// Initialize packet
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
// Read frames from input file
while (av_read_frame(format_context, &pkt) >= 0) {
// Decode frame
ret = avcodec_send_packet(codec_context, &pkt);
if (ret < 0) {
std::cerr << "Error decoding frame" << std::endl;
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codec_context, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "Error decoding frame" << std::endl;
break;
}
// Convert frame to YUV420P format
AVFrame *tmp_frame = av_frame_alloc();
if (tmp_frame == NULL) {
std::cerr << "Could not allocate temporary frame" << std::endl;
break;
}
tmp_frame->format = AV_PIX_FMT_YUV420P;
tmp_frame->width = codec_context->width;
tmp_frame->height = codec_context->height;
ret = av_frame_get_buffer(tmp_frame, 32);
if (ret < 0) {
std::cerr << "Could not allocate temporary frame data" << std::endl;
break;
}
ret = av_frame_copy(tmp_frame, frame);
if (ret < 0) {
std::cerr << "Error copying frame data" << std::endl;
break;
}
ret = av_frame_copy_props(tmp_frame, frame);
if (ret < 0) {
std::cerr << "Error copying frame props" << std::endl;
break;
}
// Encode frame
ret = avcodec_send_frame(codec_context, tmp_frame);
if (ret < 0) {
std::cerr << "Error encoding frame" << std::endl;
break;
}
while (ret >= 0) {
AVPacket outpkt;
av_init_packet(&outpkt);
outpkt.data = NULL;
outpkt.size = 0;
ret = avcodec_receive_packet(codec_context, &outpkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "Error encoding frame" << std::endl;
break;
}
// Write packet to output file
avio_write(io_context, outpkt.data, outpkt.size);
av_packet_unref(&outpkt);
}
av_frame_free(&tmp_frame);
}
av_packet_unref(&pkt);
}
// Flush encoder
ret = avcodec_send_frame(codec_context, NULL);
if (ret < 0) {
std::cerr << "Error flushing encoder" << std::endl;
return -1;
}
while (ret >= 0) {
AVPacket outpkt;
av_init_packet(&outpkt);
outpkt.data = NULL;
outpkt.size = 0;
ret = avcodec_receive_packet(codec_context, &outpkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "Error flushing encoder" << std::endl;
return -1;
}
// Write packet to output file
avio_write(io_context, outpkt.data, outpkt.size);
av_packet_unref(&outpkt);
}
// Close output file
avio_close(io_context);
// Free resources
avcodec_free_context(&codec_context);
av_frame_free(&frame);
avformat_close_input(&format_context);
return 0;
}
```
此代码使用FFmpeg库打开输入文件,并将其解码为AVFrame。然后,它将AVFrame转换为YUV420P格式,将其编码为H.264并将其写入输出文件。
阅读全文