extract audio to mp3 from mp4 using c++ (not executing ffmpeg with args)

核能气质少年 提交于 2019-12-21 05:39:07

问题


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.

  1. Init ffmpeg using av_register_all();

  2. Open input file using avformat_open_input( &informat, sourcefile, 0, 0)).

  3. Find stream info using avformat_find_stream_info(informat, 0)).

  4. Find the audio stream by iterating through streams and comparing codec_type to AVMEDIA_TYPE_AUDIO.

  5. 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).

  6. Now for output file guess the outformat using av_guess_format(NULL, (const char*)outfile, NULL).

  7. Allocate context for outformat.

  8. Find output audio encoder using avcodec_find_encoder(outfmt->audio_codec).

  9. Add new stream audio stream avformat_new_stream(outformat, out_aud_codec).

  10. Fill output codec context with desired sample rate, sample fmt, channel etc.

  11. Open output file using avio_open().

  12. Write the output headers using avformat_write_header(outformat, NULL).

  13. 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().

  14. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!