首发地址,会更错
本文来自官方例子doc/examples/encode_audio.c 和 doc/examples/encode_video.c。
FFmpeg编码流程

其中,AVFormatContext、AVPacket等重要的结构体请看:FFmpeg重要结构体(转自雷神) 。
官方例子【Audio】
/**
* @author 秦城季
* @email xhunmon@126.com
* @Blog https://qincji.gitee.io
* @date 2021/01/07
* description: 来自官方例子:doc/examples/encode_audio.c
* 主要思路:
* 1、初始化编码器信息
* 2、从文件(或者生成)读取裸流数据到每个AVFrame中
* 3、把每个AVFrame通过编码器生成编码后数据的AVPacket
* 4、使用AVFormatContext封装到文件中进行输出
*
* <br>
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
extern "C"{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/samplefmt.h>
}
/* check that a given sample format is supported by the encoder */
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
{
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
if (*p == sample_fmt)
return 1;
p++;
}
return 0;
}
/* just pick the highest supported samplerate */
static int select_sample_rate(const AVCodec *codec)
{
const int *p;
int best_samplerate = 0;
if (!codec->supported_samplerates)
return 44100;
p = codec->supported_samplerates;
while (*p) {
if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
best_samplerate = *p;
p++;
}
return best_samplerate;
}
/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec)
{
const uint64_t *p;
uint64_t best_ch_layout = 0;
int best_nb_channels = 0;
if (!codec->channel_layouts)
return AV_CH_LAYOUT_STEREO;
p = codec->channel_layouts;
while (*p) {
int nb_channels = av_get_channel_layout_nb_channels(*p);
if (nb_channels > best_nb_channels) {
best_ch_layout = *p;
best_nb_channels = nb_channels;
}
p++;
}
return best_ch_layout;
}
static int f_index = 0;
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
FILE *output,AVFormatContext *ofmt_ctx)
{
int ret;
/* send the frame for encoding */
ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
/* read all the available output packets (in general there may be any
* number of them */
while (ret >= 0) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
//(封装——数据写入)
//Convert PTS/DTS
pkt->pts = f_index *100;
pkt->dts = f_index *100;
pkt->pos = -1;
pkt->stream_index = 0;
printf("Write 1 Packet. size:%5d\tpts:%lld\n", pkt->size, pkt->pts);
//Write
if (av_interleaved_write_frame(ofmt_ctx, pkt) < 0) {
printf("Error muxing packet\n");
break;
}
f_index++;
av_packet_unref(pkt);
}
}
int main(int argc

本文介绍如何使用FFmpeg库进行音频和视频的编码处理。主要内容包括初始化编码器、处理原始音频和视频帧、生成编码后的数据包,并将其封装到输出文件中。通过两个具体的示例程序,分别展示了音频AAC和视频H.264的编码过程。
最低0.47元/天 解锁文章
705

被折叠的 条评论
为什么被折叠?



