1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
| #include <stdio.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavutil/opt.h> #include <libavutil/channel_layout.h> #include <libswresample/swresample.h>
int main(int argc, char* argv[]) { if (argc < 3) { printf("Usage: %s <input_video> <output_audio>\n", argv[0]); return -1; }
const char* input_file = argv[1]; const char* output_file = argv[2];
const char* ext = strrchr(output_file, '.'); if (!ext || (strcmp(ext, ".mp3") != 0)) { fprintf(stderr, "Output file must have .mp3 extension\n"); return -1; }
AVFormatContext* format_context = NULL; if (avformat_open_input(&format_context, input_file, NULL, NULL) != 0) { fprintf(stderr, "Could not open input file '%s'\n", input_file); return -1; }
if (avformat_find_stream_info(format_context, NULL) < 0) { fprintf(stderr, "Could not find stream information\n"); avformat_close_input(&format_context); return -1; }
printf("Input file duration: %lld seconds\n", format_context->duration / AV_TIME_BASE);
AVStream* audio_stream = NULL; int audio_stream_idx = -1; for (unsigned int i = 0; i < format_context->nb_streams; i++) { if (format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audio_stream = format_context->streams[i]; audio_stream_idx = i; printf("Found audio stream at index %d\n", i); break; } }
if (!audio_stream) { fprintf(stderr, "Could not find audio stream\n"); avformat_close_input(&format_context); return -1; }
printf("Audio Stream Info:\n"); printf("Codec ID: %d\n", audio_stream->codecpar->codec_id); printf("Sample Rate: %d\n", audio_stream->codecpar->sample_rate); printf("Channels: %d\n", audio_stream->codecpar->ch_layout.nb_channels);
AVFormatContext* output_format_context = NULL; avformat_alloc_output_context2(&output_format_context, NULL, "mp3", output_file); if (!output_format_context) { fprintf(stderr, "Could not create output context\n"); avformat_close_input(&format_context); return -1; }
AVStream* output_stream = avformat_new_stream(output_format_context, NULL); if (!output_stream) { fprintf(stderr, "Could not create output stream\n"); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
const AVCodec* input_codec = avcodec_find_decoder(audio_stream->codecpar->codec_id); if (!input_codec) { fprintf(stderr, "Could not find input codec\n"); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
AVCodecContext* input_codec_context = avcodec_alloc_context3(input_codec); if (!input_codec_context) { fprintf(stderr, "Could not allocate input codec context\n"); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
if (avcodec_parameters_to_context(input_codec_context, audio_stream->codecpar) < 0) { fprintf(stderr, "Could not copy codec params to input codec context\n"); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
if (avcodec_open2(input_codec_context, input_codec, NULL) < 0) { fprintf(stderr, "Could not open input codec\n"); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
const AVCodec* output_codec = avcodec_find_encoder(AV_CODEC_ID_MP3); if (!output_codec) { fprintf(stderr, "Could not find MP3 encoder\n"); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
AVCodecContext* output_codec_context = avcodec_alloc_context3(output_codec); if (!output_codec_context) { fprintf(stderr, "Could not allocate output codec context\n"); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
output_codec_context->bit_rate = 192000; output_codec_context->sample_rate = input_codec_context->sample_rate; output_codec_context->sample_fmt = output_codec->sample_fmts[0];
AVChannelLayout stereo = AV_CHANNEL_LAYOUT_STEREO; av_channel_layout_copy(&output_codec_context->ch_layout, &stereo);
if (avcodec_open2(output_codec_context, output_codec, NULL) < 0) { fprintf(stderr, "Could not open output codec\n"); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
if (avcodec_parameters_from_context(output_stream->codecpar, output_codec_context) < 0) { fprintf(stderr, "Could not copy codec params to output stream\n"); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
SwrContext* swr_ctx = swr_alloc(); if (!swr_ctx) { fprintf(stderr, "Could not allocate resampler context\n"); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
av_opt_set_chlayout(swr_ctx, "in_chlayout", &input_codec_context->ch_layout, 0); av_opt_set_chlayout(swr_ctx, "out_chlayout", &output_codec_context->ch_layout, 0); av_opt_set_int(swr_ctx, "in_sample_rate", input_codec_context->sample_rate, 0); av_opt_set_int(swr_ctx, "out_sample_rate", output_codec_context->sample_rate, 0); av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", input_codec_context->sample_fmt, 0); av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", output_codec_context->sample_fmt, 0);
if (swr_init(swr_ctx) < 0) { fprintf(stderr, "Could not initialize resampler\n"); swr_free(&swr_ctx); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
if (!(output_format_context->oformat->flags & AVFMT_NOFILE)) { if (avio_open(&output_format_context->pb, output_file, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open output file\n"); swr_free(&swr_ctx); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; } }
if (avformat_write_header(output_format_context, NULL) < 0) { fprintf(stderr, "Could not write header\n"); avio_closep(&output_format_context->pb); swr_free(&swr_ctx); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avformat_free_context(output_format_context); avformat_close_input(&format_context); return -1; }
AVPacket* input_packet = av_packet_alloc(); AVFrame* input_frame = av_frame_alloc(); AVFrame* output_frame = av_frame_alloc(); AVPacket* output_packet = av_packet_alloc();
int64_t total_samples = 0;
while (av_read_frame(format_context, input_packet) >= 0) { if (input_packet->stream_index == audio_stream_idx) { if (avcodec_send_packet(input_codec_context, input_packet) < 0) { fprintf(stderr, "Error sending packet for decoding\n"); continue; }
while (avcodec_receive_frame(input_codec_context, input_frame) >= 0) { output_frame->nb_samples = av_rescale_rnd( swr_get_delay(swr_ctx, input_frame->sample_rate) + input_frame->nb_samples, output_codec_context->sample_rate, input_frame->sample_rate, AV_ROUND_UP );
output_frame->format = output_codec_context->sample_fmt; output_frame->sample_rate = output_codec_context->sample_rate; av_channel_layout_copy(&output_frame->ch_layout, &output_codec_context->ch_layout);
if (av_frame_get_buffer(output_frame, 0) < 0) { fprintf(stderr, "Could not allocate output frame samples\n"); continue; }
if (swr_convert(swr_ctx, output_frame->data, output_frame->nb_samples, (const uint8_t**)input_frame->data, input_frame->nb_samples) < 0) { fprintf(stderr, "Error while converting\n"); continue; }
total_samples += output_frame->nb_samples; printf("Processing samples: %d\n", output_frame->nb_samples);
if (avcodec_send_frame(output_codec_context, output_frame) < 0) { fprintf(stderr, "Error sending frame for encoding\n"); continue; }
while (avcodec_receive_packet(output_codec_context, output_packet) >= 0) { printf("Writing packet of size: %d\n", output_packet->size); if (av_interleaved_write_frame(output_format_context, output_packet) < 0) { fprintf(stderr, "Error writing packet\n"); } av_packet_unref(output_packet); } av_frame_unref(output_frame); } av_frame_unref(input_frame); } av_packet_unref(input_packet); }
printf("Total processed samples: %lld\n", total_samples);
avcodec_send_frame(output_codec_context, NULL); while (avcodec_receive_packet(output_codec_context, output_packet) >= 0) { printf("Writing final packet of size: %d\n", output_packet->size); av_interleaved_write_frame(output_format_context, output_packet); av_packet_unref(output_packet); }
av_write_trailer(output_format_context);
av_packet_free(&input_packet); av_packet_free(&output_packet); av_frame_free(&input_frame); av_frame_free(&output_frame); swr_free(&swr_ctx); avcodec_free_context(&output_codec_context); avcodec_free_context(&input_codec_context); avio_closep(&output_format_context->pb); avformat_free_context(output_format_context); avformat_close_input(&format_context);
return 0; }
|