笔记:ffmpeg使用实例:yuv->h264,pcm->aac

本文提供了一个使用FFmpeg库进行音视频编码的示例程序,包括H.264视频编码和AAC音频编码,并展示了如何将编码后的数据写入文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:程序有待完善,代码仅供参考。


#include "stdafx.h"



extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "SDL2/SDL.h"
};


#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")


#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48KHZ 32bit audio -> 32bit / 8 * 48000 * 1 = 192000 Byte


int main(int argc, char* argv[])
{
AVFormatContext* ofmt_ctx_h264 = NULL;
const char* output_filepath_h264 = NULL;


AVFormatContext* ofmt_ctx_aac = NULL;
const char* output_filepath_aac = NULL;


AVStream* ostream_video = NULL;
AVStream* ostream_audio = NULL;


AVCodecContext* codec_ctx_2h264 = NULL;
AVCodec* codec_2h264 = NULL;


AVCodecContext* codec_ctx_2aac = NULL;
AVCodec* codec_2aac = NULL;


uint8_t* video_buffer = NULL;
int video_buffer_size = -1;


uint8_t* audio_buffer = NULL;
int audio_buffer_size = -1;


AVFrame  frame_video = { 0 };
AVFrame  frame_audio = { 0 };
AVPacket pkt_video = { 0 };
AVPacket pkt_audio = { 0 };


av_register_all();
avformat_network_init();


output_filepath_h264 = "output.h264";
output_filepath_aac = "output.aac";


if (avformat_alloc_output_context2(&ofmt_ctx_h264, NULL, "h264", output_filepath_h264) < 0)
{
return -1;
}


if (avio_open(&ofmt_ctx_h264->pb, output_filepath_h264, AVIO_FLAG_READ_WRITE) < 0)
{
return -1;
}


if (avformat_alloc_output_context2(&ofmt_ctx_aac, NULL, "adts", output_filepath_aac) < 0)
{
return -1;
}


if (avio_open(&ofmt_ctx_aac->pb, output_filepath_aac, AVIO_FLAG_READ_WRITE) < 0)
{
return - 1;
}


ostream_video = avformat_new_stream(ofmt_ctx_h264, NULL);
ostream_audio = avformat_new_stream(ofmt_ctx_aac, NULL);


ostream_audio->codec->codec_tag = 0;
if (ofmt_ctx_aac->oformat->flags & AVFMT_GLOBALHEADER)
ostream_audio->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;


ostream_video->codec->codec_tag = 0;
if (ofmt_ctx_h264->oformat->flags & AVFMT_GLOBALHEADER)
ostream_video->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;


codec_ctx_2h264 = ostream_video->codec;
codec_ctx_2h264->codec_id = ofmt_ctx_h264->oformat->video_codec;
codec_ctx_2h264->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx_2h264->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx_2h264->width = 1920;
codec_ctx_2h264->height = 1080;
codec_ctx_2h264->bit_rate = 400000;
codec_ctx_2h264->gop_size = 25;
codec_ctx_2h264->time_base.num = 1;
codec_ctx_2h264->time_base.den = 25;
codec_ctx_2h264->qmin = 10;
codec_ctx_2h264->qmax = 51;
codec_ctx_2h264->max_b_frames = 3;


AVDictionary* param = NULL;
av_dict_set(&param, "preset", "slow", 0);
av_dict_set(&param, "tune", "zerolatency", 0);
//av_dict_set(&param, "profile", "main", 0);


codec_2h264 = avcodec_find_encoder(codec_ctx_2h264->codec_id);
if (avcodec_open2(codec_ctx_2h264, codec_2h264, &param) < 0)
{
return -1;
}


codec_ctx_2aac = ostream_audio->codec;
codec_ctx_2aac->codec_id = ofmt_ctx_aac->oformat->audio_codec;
codec_ctx_2aac->codec_type = AVMEDIA_TYPE_AUDIO;
codec_ctx_2aac->sample_fmt = AV_SAMPLE_FMT_FLTP; // 音频编解码只能使用此格式?
codec_ctx_2aac->sample_rate = 44100;
codec_ctx_2aac->bit_rate = 64000;
codec_ctx_2aac->channel_layout = AV_CH_LAYOUT_MONO; // 
codec_ctx_2aac->channels = av_get_channel_layout_nb_channels(codec_ctx_2aac->channel_layout);


codec_2aac = avcodec_find_encoder(codec_ctx_2aac->codec_id);
if (avcodec_open2(codec_ctx_2aac, codec_2aac, NULL) < 0)
{
return -1;
}


if (avformat_write_header(ofmt_ctx_h264, NULL) < 0)
{
return -1;
}


if (avformat_write_header(ofmt_ctx_aac, NULL) < 0)
{
return -1;
}

// video
frame_video.width = codec_ctx_2h264->width;
frame_video.height = codec_ctx_2h264->height;
frame_video.format = codec_ctx_2h264->pix_fmt;


int video_y_size = frame_video.width * frame_video.height;


video_buffer_size = avpicture_get_size((AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_buffer = (uint8_t*)av_malloc(video_buffer_size);
avpicture_fill((AVPicture*)&frame_video, video_buffer, (AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);


// audio
frame_audio.nb_samples = codec_ctx_2aac->frame_size;
frame_audio.channels = codec_ctx_2aac->channels;
frame_audio.format = codec_ctx_2aac->sample_fmt;


audio_buffer_size = av_samples_get_buffer_size(NULL, frame_audio.channels, frame_audio.nb_samples, (AVSampleFormat)frame_audio.format, 1);
audio_buffer = (uint8_t*)av_malloc(audio_buffer_size);
avcodec_fill_audio_frame(&frame_audio, frame_audio.channels, (AVSampleFormat)frame_audio.format, (const uint8_t*)audio_buffer, audio_buffer_size, 1);


av_init_packet(&pkt_audio);
pkt_audio.data = NULL;
pkt_audio.size = 0;


av_init_packet(&pkt_video);
pkt_video.data = NULL;
pkt_video.size = 0;


FILE* input_pcm_after = fopen("input_audio_fltp_mono.pcm", "rb");
FILE* input_bgr = fopen("input_video.yuv", "rb");


while (1)
{
if (fread(video_buffer, video_y_size * 3 / 2, 1, input_bgr) > 0)
{
static int videocnt = 0;
printf("videocnt %d\n", videocnt++);


frame_video.data[0] = video_buffer; // Y  
frame_video.data[1] = video_buffer + video_y_size; // U   ?
frame_video.data[2] = video_buffer + video_y_size * 5 / 4; // V   ?


int got_picture = 0;
//frame_yuv.pts = videocnt;
if (avcodec_encode_video2(codec_ctx_2h264, &pkt_video, &frame_video, &got_picture) < 0)
{
return -1;
}
if (got_picture == 1)
{
pkt_video.stream_index = 0;
av_write_frame(ofmt_ctx_h264, &pkt_video);
av_free_packet(&pkt_video);
}
}


if (fread(audio_buffer, audio_buffer_size, 1, input_pcm_after) > 0)
{
static int audiocnt = 0;
printf("audiocnt %d\n", audiocnt++);


frame_audio.data[0] = audio_buffer;
frame_audio.pts = audiocnt;


int got_frame = 0; 
if (avcodec_encode_audio2(codec_ctx_2aac, &pkt_audio, &frame_audio, &got_frame) < 0)
{
return -1;
}
if (got_frame == 1)
{
pkt_audio.stream_index = 0;
av_write_frame(ofmt_ctx_aac, &pkt_audio);
av_free_packet(&pkt_audio);
}
}
}


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值