问题
how can i programmatically convert ( extract the audio channel ) from mp4 video file format ?
i just can't find any thing in the web , for using c++ .
i what to dynamically link external engine that i can via c++ take mp4 file and convert it to mp3 file.
and not passing command line args to LAME or MPLAYER or FFMPEG ?
回答1:
You can try using ffmpeg to do it in c or c++. Here is the normal flow of steps.
Init ffmpeg using av_register_all();
Open input file using avformat_open_input( &informat, sourcefile, 0, 0)).
Find stream info using avformat_find_stream_info(informat, 0)).
Find the audio stream by iterating through streams and comparing codec_type to AVMEDIA_TYPE_AUDIO.
Once you have input audio stream you can find audio decoder and open the decoder. Use avcodec_find_decoder(in_aud_strm->codec->codec_id) and avcodec_open2(in_aud_codec_ctx, in_aud_codec, NULL).
Now for output file guess the outformat using av_guess_format(NULL, (const char*)outfile, NULL).
Allocate context for outformat.
Find output audio encoder using avcodec_find_encoder(outfmt->audio_codec).
Add new stream audio stream avformat_new_stream(outformat, out_aud_codec).
Fill output codec context with desired sample rate, sample fmt, channel etc.
Open output file using avio_open().
Write the output headers using avformat_write_header(outformat, NULL).
Now in while loop start reading packet, decode only audio packet encode them and write them in opened output file. You can use av_read_frame(informat, &pkt) , avcodec_decode_audio4(in_aud_codec_ctx, pframeT, &got_vid_pkt, &pkt), avcodec_encode_audio2() and av_write_frame().
Finally write trailer using av_write_trailer.
You can looking into demuxing.c and muxing.c provided in ffmpeg examples.
来源:https://stackoverflow.com/questions/16131805/extract-audio-to-mp3-from-mp4-using-c-not-executing-ffmpeg-with-args