- av_register_all();
- avctx = avformat_alloc_context();
- avctx->oformat = av_guess_format(NULL, output_filename, NULL);
- strcpy(avctx->filename, output_filename);
- if (avio_open2(&avctx->pb, output_filename, AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL) < 0) {
- fprintf(stderr, "%s: avio_open2() failed\n", output_filename);
- exit(1);
- }
- AVCodec *codec_video = avcodec_find_encoder(AV_CODEC_ID_H264);
- avstream_video = avformat_new_stream(avctx, codec_video);
- if (avstream_video == nullptr) {
- fprintf(stderr, "%s: avformat_new_stream() failed\n", output_filename);
- exit(1);
- }
- avstream_video->time_base = AVRational{1, frame_rate};
- avstream_video->codec->width = width;
- avstream_video->codec->height = height;
- avstream_video->codec->time_base = AVRational{1, frame_rate};
- avstream_video->codec->ticks_per_frame = 1; // or 2?
-
- AVCodec *codec_audio = avcodec_find_encoder(AV_CODEC_ID_MP3);
- avstream_audio = avformat_new_stream(avctx, codec_audio);
- if (avstream_audio == nullptr) {
- fprintf(stderr, "%s: avformat_new_stream() failed\n", output_filename);
- exit(1);
- }
- avstream_audio->time_base = AVRational{1, frame_rate};
- avstream_audio->codec->bit_rate = 256000;
- avstream_audio->codec->sample_rate = 48000;
- avstream_audio->codec->sample_fmt = AV_SAMPLE_FMT_FLTP;
- avstream_audio->codec->channels = 2;
- avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
- avstream_audio->codec->time_base = AVRational{1, frame_rate};
-
- /* open it */
- if (avcodec_open2(avstream_audio->codec, codec_audio, NULL) < 0) {
+ AVCodec *codec_audio = avcodec_find_encoder(AUDIO_OUTPUT_CODEC);
+ context_audio = avcodec_alloc_context3(codec_audio);
+ context_audio->bit_rate = AUDIO_OUTPUT_BIT_RATE;
+ context_audio->sample_rate = OUTPUT_FREQUENCY;
+ context_audio->sample_fmt = AUDIO_OUTPUT_SAMPLE_FMT;
+ context_audio->channels = 2;
+ context_audio->channel_layout = AV_CH_LAYOUT_STEREO;
+ context_audio->time_base = AVRational{1, TIMEBASE};
+ if (avcodec_open2(context_audio, codec_audio, NULL) < 0) {