The simplest transcoding program based on FFmpeg __ffmpeg

Source: Internet
Author: User

This paper introduces a simple transcoding device based on FFmpeg. It converts a video format, including both the marshaling format and the encoding format, to another video format. In the process of video and audio codec processing, the transcoding device belongs to a more complex thing. Because it combines the decoding and coding of video. A video player, generally contains only the decoding function, a video coding tool, generally contains only the encoding function, and a video transcoding device, you need to decode the video first, and then encode the video, which is equivalent to the combination of decoder and encoder. The following illustration holds a video transcoding process. Input video encapsulation format is FLV, Video coding standard is H.264, Audio coding standard is AAC, output video package format is AVI, Video coding standard is MPEG2, Audio coding standard is MP3. As you can see from the process, firstly, the video stream and the audio compression stream are separated from the input video, then the video stream and the audio code stream are decoded, the uncompressed pixel data/audio sampling data are obtained, and then the uncompressed pixel data/audio sampling data is encoded again. Get the recoding video stream and audio code stream, and then encapsulate the video stream and audio code stream into a file.

The video transcoding device introduced in this paper uses the FFmpeg class library to realize the above process from the programming point of view. This example is adapted from the FFmpeg example, the platform is VC2010, the class library version is 2014.5.6. Flowchart (2014.9.29 update)

The following is a two-piece flowchart using FFmpeg transcoding video. The graph uses the light green to mark the video encoding and decoding function. From the code can be seen, using a lot of avfilter, it is recommended to learn the contents of the Avfilter before looking at the source code of the transcoding.

PS: In fact, the transcoding device is not necessarily dependent on avfilter. Therefore, it is intended to have time to further simplify the transcoding, so that the learning of the person without Avfilter can also understand the transcoding device.


Briefly describe the meaning of each function in the process:
Open_input_file (): Opens the input file and initializes the associated structure.
Open_output_file (): Opens the output file and initializes the associated structure.
Init_filters (): Initializes the Avfilter-related structure.
Av_read_frame (): Reads a avpacket from the input file.
Avcodec_decode_video2 (): Decoding a video Avpacket (storage H.264, such as compressed code stream data) for Avframe (storage YUV and other uncompressed pixel data).
Avcodec_decode_video4 (): Decodes an audio Avpacket (storage MP3, etc.) for Avframe (storing PCM sampled data).
Filter_encode_write_frame (): Encodes a avframe.
Flush_encoder (): After the input file is read, the output encoder remaining avpacket.

The functions in the above functions Open_input_file (), Open_output_file (), init_filters () are described in other articles, and are not repeated here:

Open_input_file () can refer to: 100 lines of code to achieve the simplest ffmpeg+sdl based video player (sdl1.x)

Open_output_file () may refer to: The simplest ffmpeg video encoder (YUV encoded as H.264)

Init_filters () can refer to: The simplest Avfilter example based on ffmpeg (watermark overlay)

Here is a description of the encoded function Filter_encode_write_frame (). The process of the filter_encode_write_frame () function, as shown in the following illustration, completes the video/audio encoding function.

PS: The coding process for video and audio is not the same as the encoding function Avcodec_encode_video2 () and Avcodec_encode_audio2 (), except that the other parts are almost identical.


Briefly describe the meaning of each function in Filter_encode_write_frame ():

Av_buffersrc_add_frame (): Add the decoded avframe to the filtergraph.

Av_buffersink_get_buffer_ref (): Take a avframe from the filtergraph.

Avcodec_encode_video2 (): Encode a video avframe for Avpacket.

Avcodec_encode_audio2 (): Encode an audio avframe for Avpacket.

Av_interleaved_write_frame (): Writes the encoded avpacket to the file.


Code

Put Code on

[CPP]  View plain copy/*   * simplest FFmpeg-based transcoding    *Simplest FFmpeg Transcoder   *   * Rei  Lei Xiaohua   *leixiaohua1020@126.com   * Communication University/digital TV Technology    *Communication University of China / DigitalTV Technology   *http://blog.csdn.net/leixiaohua1020   *   * This program realizes the conversion between video formats. is one of the simplest video transcoding programs.    *   */       #include   "stdafx.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/avutil.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)    {     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.