FFmpeg multiple video merges with different frame rates in different formats

Source: Internet
Author: User

The same video encoding format is a good merger, only need to follow the process to open the file, then a frame of the read, and even generate video parameters do not need to become, the only need is to change the PTS just fine but this piece I would not normally copy the original video stream parameters, because that time stamp is not good control,

Video merging in different encoded formats requires decoding each frame of data first, decoding it into the most original YUV format, and then encoding it into the encoding format you want. So almost all the data is the same.

FFmpeg decoding process:
1. Register all container formats and Codec:av_register_all ()
2. Open file: Av_open_input_file ()
3. Extracting stream information from a file: Av_find_stream_info ()
4. Exhaustive all flow, look for the type of Codec_type_video
5. Find the corresponding decoder: Avcodec_find_decoder ()
6. Open codec: Avcodec_open ()
7. Allocating memory for decoding frames: Avcodec_alloc_frame ()
8. Continuously extracting frame data from the stream: Av_read_frame ()
9. Determine the type of frame, for video frame call: Avcodec_decode_video ()
10. After decoding, release decoder: Avcodec_close ()
11. Close the input file: Avformat_close_input_file ()
Basic concepts:
Codec, data frame, media stream and container are the four basic concepts of digital media processing system.
First, you need a unified terminology:
Container/File (Conainer/file): A multimedia file in a specific format.
Stream: Refers to a continuous piece of data on the timeline, such as a piece of sound data, a piece of video data or a piece of subtitle data, which can be compressed or uncompressed, and the compressed data needs to be associated with a particular codec.
Data frame/packet (Frame/packet): Typically, a media stream consists of a large number of data frames, and for compressed data, the frame corresponds to the minimum processing unit of the codec. Typically, data frames that belong to different media streams are interleaved into containers, see interleaving.
Codec: A codec enables the conversion of compressed data to and from the original data in frames.
In FFmpeg, these basic elements are abstracted using structures such as Avformatcontext, Avstream, Avcodeccontext, Avcodec, and Avpacket, and their relationships are as shown in the figure above:
Avcodeccontext:
This is a data structure that describes the context of a codec, contains the parameter information required by many codecs, and lists some of the more important domains:
typedef struct AVCODECCONTEXT {
/ **
* Some codecs are required/can be used like the Extradata Huffman table.
* Mjpeg:huffman Table
* RV10 Other Signs
* MPEG4: Global head (also can be in bitstream or here)
* Allocated memory should be ff_input_buffer_padding_size bytes large
*, than extradata_size avoids the bitstream if it is with read prolems.
* Extradata by Byte content must not depend on the schema or the byte order of the CPU.
*-Encoding: Set/assign/release by Libavcodec.
*-Decoding: Set/assign/release by user.
* /
uint8_t *extradata;
int extradata_size;
/ **
* This is the basic unit of time, in the condition (in seconds)
* Frame time stamp sent representatives attended the meeting. For fixed FPS content,
* Base should be 1/framerate and the increment of timestamp should be
* Same as 1.
*-Encoding: Must be set by the user.
*-decode: libavcodec settings.
* /
Avrational time_base;

Enum Codecid codec_id;
/ **
* FOURCC (LSB in front, so "ABCD"-> ("D" << +) ("C" << +) ("B" << 8) + "a").
* This is used to fix some coding errors.
* The splitter should set what is the codec used to identify the domain.
* If there are multiple fields such as taps, in a container, then select a
* Maximize the use of codec-related information.
* If the codec tag field in the container then the 32-bit large splitter should
* A 32-bit number that is remapped to a table or other structure. You can also choose a new
* extra_codec_tag+ size can be added, but must prove that this is an obvious advantage
First
*-Encoding: Set by the user, if not, the default based on codec_id will be used.
*-decode: Set by user, will be converted to uppercase in initialize LIBAVCODEC.
* /
unsigned int codec_tag;
......
/ **
* The size of the buffer in the decoder's frame reordering.
* For MPEG-2, this is a IPB1 or 0 low latency IP.
*-code: LIBAVCODEC settings.
*-decode: libavcodec settings.
* /
int has_b_frames;

/ **
* Number of bytes per packet, if constant and known or 0
* Audio codec for some WAV.
* /
int block_align;
/ **
* From the tap bits per sample/pixel (HuffYUV required).
*-code: LIBAVCODEC settings.
*-Decoding: set by user.
* /
int bits_per_coded_sample;
.....
} Avcodeccontext;

If you use libavcodec purely, this information needs to be initialized by the caller, and if you are using the entire FFmpeg library, this information is called Avformat_open_input and AVFORMAT_FIND_STREAM_ The process of info is initialized based on the header information of the file and the headers in the media stream. Several of these major domains are interpreted as follows:
Extradata/extradata_size: This buffer contains additional information that the decoder may use to populate the Av_read_frame. In general, first of all, a specific format of demuxer in the reading of the format header information will be filled extradata, and secondly, if demuxer did not do this thing, for example, there may be no related codec information in the head, The corresponding parser will continue to seek from the media stream that has been re-used. If no additional information is found, the buffer pointer is empty.
Time_base:
Width/height: Video width and height.
Sample_rate/channels: Audio sample rate and number of channels.
SAMPLE_FMT: The original sampling format of the audio.
Codec_name/codec_type/codec_id/codec_tag: Information about the codec.
Avstrea
The struct describes a media stream, defined as follows:
typedef struct AVSTREAM {
int index;
Avcodeccontext *codec;
/ **
* Stream the real-time frame rate base.
* This is the minimum frame rate that all timestamps can have
* Accurate representation (it is all the least common multiple
* Stream frame rate). Please note that this value is just a guess.
* For example, if the time base is 1/90000 and all frames
* About 3600 or 1800 timer ticks, and then R_frame_rate will be 50/1.
* /
Avrational r_frame_rate;
/ **
* This is the basic unit of time, in the condition (in seconds)
* Frame time stamp sent representatives attended the meeting. For fixed FPS content,
* The time base should be 1/framerate the increment of the timestamp should be 1.
* /
Avrational time_base;
......
/ **
* Decode the first frame of traffic,-base points at traffic.
* If you are absolutely 100% sure, set the value
* It really is the first frame point.
* This may be undefined (av_nopts_value).
*@ Note amateur head not weak subject with right start_time amateur
* Tap must not set this.
* /

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.