extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libavutil/time.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
}
int encodeH264(AVCodecContext* codecContent, AVPacket* packet, AVFrame* frame)
{
//编码
int ret = avcodec_send_frame(codecContent, frame);
if (ret < 0)
{
fprintf(stderr, "Error sending a frame for encoding\n");
return -1;
}
while (ret == 0)
{
ret = avcodec_receive_packet(codecContent, packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
}
else if (ret < 0) {
fprintf(stderr, "Error encoding video frame\n");
return -1;
}
if (ret == 0)
{
return ret;
//fwrite(packet->data, 1, packet->size, outFile);
}
}
return -1;
}
//
int main() {
//av_register_all();
const char* inputFileName = "001.mp4";
const char* audioutputFileName = "001_test.pcm";
const char* videoOutputFileName="001_test.yuv";
const char *outFileName="001_test.264";
#if 1
int ret = 0;
AVCodecContext* codecContent_enc264 = nullptr;
AVPacket* packet_enc264 = nullptr;
AVFrame* frame_enc264 = nullptr;
FILE* inFile = nullptr;
FILE* outFile = nullptr;
//查找指定编码器
const AVCodec* codec_enc264 = avcodec_find_encoder(AV_CODEC_ID_H264);
if (codec_enc264 == nullptr)
{
printf("could not find h264 encoder!");
return -1;
}
//申请编码器上下文
codecContent_enc264 = avcodec_alloc_context3(codec_enc264);
if (codecContent_enc264 == nullptr)
{
printf("could not alloc h264 content!");
return -1;
}
//必设