Convert an input audio file to AAC in an MP4 container using FFmpeg.
#include <stdio.h>
#define OUTPUT_BIT_RATE 48000
#define OUTPUT_CHANNELS 2
#define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
{
static char error_buffer[255];
av_strerror(error, error_buffer,
sizeof(error_buffer));
return error_buffer;
}
{
int error;
NULL)) < 0) {
fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
*input_format_context = NULL;
return error;
}
fprintf(stderr, "Could not open find stream info (error '%s')\n",
return error;
}
if ((*input_format_context)->nb_streams != 1) {
fprintf(stderr, "Expected one audio input stream, but found %d\n",
(*input_format_context)->nb_streams);
}
fprintf(stderr, "Could not find input codec\n");
}
if ((error =
avcodec_open2((*input_format_context)->streams[0]->codec,
input_codec, NULL)) < 0) {
fprintf(stderr, "Could not open input codec (error '%s')\n",
return error;
}
*input_codec_context = (*input_format_context)->streams[0]->
codec;
return 0;
}
{
int error;
if ((error =
avio_open(&output_io_context, filename,
fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
return error;
}
fprintf(stderr, "Could not allocate output format context\n");
}
(*output_format_context)->pb = output_io_context;
NULL))) {
fprintf(stderr, "Could not find output file format\n");
}
av_strlcpy((*output_format_context)->filename, filename,
sizeof((*output_format_context)->filename));
fprintf(stderr, "Could not find an AAC encoder.\n");
}
fprintf(stderr, "Could not create new stream\n");
}
*output_codec_context = stream->
codec;
(*output_codec_context)->sample_rate = input_codec_context->
sample_rate;
if ((error =
avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
fprintf(stderr, "Could not open output codec (error '%s')\n",
}
return 0;
*output_format_context = NULL;
}
{
}
{
fprintf(stderr, "Could not allocate input frame\n");
}
return 0;
}
{
int error;
0, NULL);
if (!*resample_context) {
fprintf(stderr, "Could not allocate resample context\n");
}
if ((error =
swr_init(*resample_context)) < 0) {
fprintf(stderr, "Could not open resample context\n");
return error;
}
return 0;
}
{
fprintf(stderr, "Could not allocate FIFO\n");
}
return 0;
}
{
int error;
fprintf(stderr, "Could not write output file header (error '%s')\n",
return error;
}
return 0;
}
int *data_present, int *finished)
{
int error;
if ((error =
av_read_frame(input_format_context, &input_packet)) < 0) {
*finished = 1;
else {
fprintf(stderr, "Could not read frame (error '%s')\n",
return error;
}
}
data_present, &input_packet)) < 0) {
fprintf(stderr, "Could not decode frame (error '%s')\n",
return error;
}
if (*finished && *data_present)
*finished = 0;
return 0;
}
{
int error;
if (!(*converted_input_samples = calloc(output_codec_context->
channels,
sizeof(**converted_input_samples)))) {
fprintf(stderr, "Could not allocate converted input sample pointers\n");
}
frame_size,
fprintf(stderr,
"Could not allocate converted input samples (error '%s')\n",
av_freep(&(*converted_input_samples)[0]);
free(*converted_input_samples);
return error;
}
return 0;
}
uint8_t **converted_data,
const int frame_size,
{
int error;
converted_data, frame_size,
input_data , frame_size)) < 0) {
fprintf(stderr, "Could not convert input samples (error '%s')\n",
return error;
}
return 0;
}
const int frame_size)
{
int error;
fprintf(stderr, "Could not reallocate FIFO\n");
return error;
}
frame_size) < frame_size) {
fprintf(stderr, "Could not write data to FIFO\n");
}
return 0;
}
int *finished)
{
uint8_t **converted_input_samples = NULL;
int data_present;
input_codec_context, &data_present, finished))
if (*finished && !data_present) {
ret = 0;
}
if (data_present) {
ret = 0;
}
ret = 0;
if (converted_input_samples) {
free(converted_input_samples);
}
}
int frame_size)
{
int error;
fprintf(stderr, "Could not allocate output frame\n");
}
(*frame)->format = output_codec_context->
sample_fmt;
(*frame)->sample_rate = output_codec_context->
sample_rate;
fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
return error;
}
return 0;
}
int *data_present)
{
int error;
frame, data_present)) < 0) {
fprintf(stderr, "Could not encode frame (error '%s')\n",
return error;
}
if (*data_present) {
if ((error =
av_write_frame(output_format_context, &output_packet)) < 0) {
fprintf(stderr, "Could not write frame (error '%s')\n",
return error;
}
}
return 0;
}
{
int data_written;
fprintf(stderr, "Could not read data from FIFO\n");
}
output_codec_context, &data_written)) {
}
return 0;
}
{
int error;
fprintf(stderr, "Could not write output file trailer (error '%s')\n",
return error;
}
return 0;
}
int main(
int argc,
char **argv)
{
AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
if (argc < 3) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(1);
}
&input_codec_context))
&output_format_context, &output_codec_context))
&resample_context))
while (1) {
const int output_frame_size = output_codec_context->frame_size;
int finished = 0;
input_codec_context,
output_codec_context,
resample_context, &finished))
if (finished)
break;
}
output_codec_context))
if (finished) {
int data_written;
do {
output_codec_context, &data_written))
} while (data_written);
break;
}
}
ret = 0;
if (fifo)
if (output_codec_context)
if (output_format_context) {
}
if (input_codec_context)
if (input_format_context)
}