* Open an output file and the required encoder. * Also set some basic encoder parameters. * Some of these parameters are based on the input file's parameters. * @param filename File to be opened * @param input_codec_context Codec context of input file * @param[out] output_format_context Format context of output file * @param[out] output_codec_context Codec context o
| 144 | * @return Error code (0 if successful) |
| 145 | */ |
| 146 | static int open_output_file(const char *filename, |
| 147 | AVCodecContext *input_codec_context, |
| 148 | AVFormatContext **output_format_context, |
| 149 | AVCodecContext **output_codec_context) |
| 150 | { |
| 151 | AVCodecContext *avctx = NULL; |
| 152 | AVIOContext *output_io_context = NULL; |
| 153 | AVStream *stream = NULL; |
| 154 | const AVCodec *output_codec = NULL; |
| 155 | int error; |
| 156 | |
| 157 | /* Open the output file to write to it. */ |
| 158 | if ((error = avio_open(&output_io_context, filename, |
| 159 | AVIO_FLAG_WRITE)) < 0) { |
| 160 | fprintf(stderr, "Could not open output file '%s' (error '%s')\n", |
| 161 | filename, av_err2str(error)); |
| 162 | return error; |
| 163 | } |
| 164 | |
| 165 | /* Create a new format context for the output container format. */ |
| 166 | if (!(*output_format_context = avformat_alloc_context())) { |
| 167 | fprintf(stderr, "Could not allocate output format context\n"); |
| 168 | return AVERROR(ENOMEM); |
| 169 | } |
| 170 | |
| 171 | /* Associate the output file (pointer) with the container format context. */ |
| 172 | (*output_format_context)->pb = output_io_context; |
| 173 | |
| 174 | /* Guess the desired container format based on the file extension. */ |
| 175 | if (!((*output_format_context)->oformat = av_guess_format(NULL, filename, |
| 176 | NULL))) { |
| 177 | fprintf(stderr, "Could not find output file format\n"); |
| 178 | goto cleanup; |
| 179 | } |
| 180 | |
| 181 | if (!((*output_format_context)->url = av_strdup(filename))) { |
| 182 | fprintf(stderr, "Could not allocate url.\n"); |
| 183 | error = AVERROR(ENOMEM); |
| 184 | goto cleanup; |
| 185 | } |
| 186 | |
| 187 | /* Find the encoder to be used by its name. */ |
| 188 | if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) { |
| 189 | fprintf(stderr, "Could not find an AAC encoder.\n"); |
| 190 | goto cleanup; |
| 191 | } |
| 192 | |
| 193 | /* Create a new audio stream in the output file container. */ |
| 194 | if (!(stream = avformat_new_stream(*output_format_context, NULL))) { |
| 195 | fprintf(stderr, "Could not create new stream\n"); |
| 196 | error = AVERROR(ENOMEM); |
| 197 | goto cleanup; |
| 198 | } |
| 199 | |
| 200 | avctx = avcodec_alloc_context3(output_codec); |
| 201 | if (!avctx) { |
| 202 | fprintf(stderr, "Could not allocate an encoding context\n"); |
| 203 | error = AVERROR(ENOMEM); |
no test coverage detected