Convert an input audio file to AAC in an MP4 container using FFmpeg.Formats other than MP4 are supported based on the output file extension.
#include <stdio.h>
#define OUTPUT_BIT_RATE 96000
#define OUTPUT_CHANNELS 2
{
fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
*input_format_context =
NULL;
}
fprintf(stderr, "Could not open find stream info (error '%s')\n",
}
if ((*input_format_context)->nb_streams != 1) {
fprintf(stderr, "Expected one audio input stream, but found %d\n",
(*input_format_context)->nb_streams);
}
if (!(input_codec =
avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
fprintf(stderr, "Could not find input codec\n");
}
if (!avctx) {
fprintf(stderr, "Could not allocate a decoding context\n");
}
}
fprintf(stderr, "Could not open input codec (error '%s')\n",
}
*input_codec_context = avctx;
return 0;
}
{
fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
}
fprintf(stderr, "Could not allocate output format context\n");
}
(*output_format_context)->pb = output_io_context;
fprintf(stderr, "Could not find output file format\n");
}
if (!((*output_format_context)->url =
av_strdup(filename))) {
fprintf(stderr, "Could not allocate url.\n");
}
fprintf(stderr, "Could not find an AAC encoder.\n");
}
fprintf(stderr, "Could not create new stream\n");
}
if (!avctx) {
fprintf(stderr, "Could not allocate an encoding context\n");
}
fprintf(stderr, "Could not open output codec (error '%s')\n",
}
fprintf(stderr, "Could not initialize stream parameters\n");
}
*output_codec_context = avctx;
return 0;
*output_format_context =
NULL;
}
{
}
{
fprintf(stderr, "Could not allocate input frame\n");
}
return 0;
}
{
if (!*resample_context) {
fprintf(stderr, "Could not allocate resample context\n");
}
fprintf(stderr, "Could not open resample context\n");
}
return 0;
}
{
fprintf(stderr, "Could not allocate FIFO\n");
}
return 0;
}
{
fprintf(stderr, "Could not write output file header (error '%s')\n",
}
return 0;
}
int *data_present, int *finished)
{
*finished = 1;
else {
fprintf(stderr, "Could not read frame (error '%s')\n",
}
}
fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
}
*finished = 1;
fprintf(stderr, "Could not decode frame (error '%s')\n",
} else {
*data_present = 1;
}
}
{
if (!(*converted_input_samples = calloc(output_codec_context->
channels,
sizeof(**converted_input_samples)))) {
fprintf(stderr, "Could not allocate converted input sample pointers\n");
}
fprintf(stderr,
"Could not allocate converted input samples (error '%s')\n",
av_freep(&(*converted_input_samples)[0]);
free(*converted_input_samples);
}
return 0;
}
{
fprintf(stderr, "Could not convert input samples (error '%s')\n",
}
return 0;
}
{
fprintf(stderr, "Could not reallocate FIFO\n");
}
fprintf(stderr, "Could not write data to FIFO\n");
}
return 0;
}
int *finished)
{
int data_present = 0;
input_codec_context, &data_present, finished))
if (*finished) {
}
if (data_present) {
}
if (converted_input_samples) {
free(converted_input_samples);
}
}
{
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 not allocate output frame samples (error '%s')\n",
}
return 0;
}
int *data_present)
{
}
fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
}
fprintf(stderr, "Could not encode frame (error '%s')\n",
} else {
*data_present = 1;
}
if (*data_present &&
fprintf(stderr, "Could not write frame (error '%s')\n",
}
}
{
int data_written;
fprintf(stderr, "Could not read data from FIFO\n");
}
output_codec_context, &data_written)) {
}
return 0;
}
{
fprintf(stderr, "Could not write output file trailer (error '%s')\n",
}
return 0;
}
int main(
int argc,
char **argv)
{
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 {
data_written = 0;
output_codec_context, &data_written))
} while (data_written);
break;
}
}
if (fifo)
if (output_codec_context)
if (output_format_context) {
}
if (input_codec_context)
if (input_format_context)
}