This example is modified by Ffempg's doc/example example transcode.c, which can be used to convert audio and video codes as needed.
The original example of the role is more similar to the Remux, and did not realize the function of transcoding, only to achieve the format conversion, such as TS to AVI. Can not realize the conversion of audio and video coding format, such as H264 to MPEG2.
There are several ways to implement FFmpeg transcoding:
One way is: Streaming multiplexing-> video + audio streaming-> decoding->YUV/PCM-> video audio coding-> regenerated audio and video stream-> multiplexing-> stream
Another way to rely on Avfilter is to explain how to use it in a few other articles. Although Avfilter may be difficult to learn, but in practical programming applications, relying on avfilter to do transcoding efficiency is higher than the first way, and decoding the CPU and time consumption is much less. Therefore, it is advisable to study this part, after all, I have always felt that FFmpeg's strength is decoding and transcoding.
This example is video MPEG2 turn h264, audio Mpegaudio to g711.
[CPP] View plain copy print? /* * based on ffmpeg transcode.c * modified by tongli * #include <stdio.h> #include "snprintf.h" extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/ avformat.h> #include <libavfilter/avfiltergraph.h> #include < libavfilter/avcodec.h> #include <libavfilter/buffersink.h> #include <libavfilter/buffersrc.h> #include <libavutil/opt.h> #include < libavutil/pixdesc.h> } static avformatcontext *ifmt_ctx; Static AVFormatContext *ofmt_ctx; typedef struct filteringcontext { AVFilterContext *buffersink_ctx; AVFilterContext *buffersrc_ctx; avfiltergraph *filter _graph; } filteringcontext; static filteringcontext *filter_ctx; static int open_input_file (const char *filename) { int ret; unsigned int i; ifmt_ctx = NULL; if ((Ret = avformat_open_input (&ifmt_ctx, filename, null, null)) < 0) { av_log (Null, av_log_error, "cannot open input file\n"); return ret; } if ( (Ret =&nbsP;avformat_find_stream_info (ifmt_ctx, null) < 0) { av_log (null, av_log_error, "cannot find stream information\n") ); return ret; } for (i = 0; i < ifmt_ ctx->nb_streams; i++) { AVStream *stream; AVCodecContext *codec_ctx; stream = ifmt_ctx->streams[i]; codec_ctx = stream->codec;