This section is the beginning of the FFMPEG Development Player Learning notes “Hello FFmPEG”


🔔 section 1 – Hello FFmpeg 📗 section 2 – Soft unclip video stream, render RGB24 📗 section 3 – understand YUV 📗 section 4 – hard decoding,OpenGL render YUV Disk section 5 – Metal render YUV disk section 6 – Decode audio, play with AudioQueue 📗 section 7 – audio and video synchronization 📗 Section 8 – Perfect playback control 📗 Section 9 – double speed play 📗 section 10 – increase video filtering effect section 11 – audio changes

Demo address of this section: github.com/czqasngit/f… The example code provides both Objective-C and Swift implementations


The target

  • Compile FFMPEG to generate the X86_64 static library
  • Build macOS+ FFMPEG development environment
  • Understand the FFMPEG initialization process
  • Use FFMPEG and print audio and video information


Compile ffmpeg

1. Download

Ffmpeg is available at ffmpeg.org/download.ht… Download the latest Source Code.

After downloading, unpack, and then enter the ffMPEG source root directory.

2. Set compilation parameters

The terminal goes to the FFMPEG source directory and enters the following configuration command to configure a basic build parameter

./configure  --prefix=./macos --enable-gpl --enable-nonfree --enable-libfdk-aac
Copy the code

–prefix specifies the output directory of the compiled product, in this case to the macOS directory under the current directory, which was created in advance. –enable-gpl allows the use of GPL code, and the generated libraries and binaries will be under the GPL –enable-nofree allows the use of non-free code, The resulting libraries and binaries will not be distributable — enable-libfdK-aac encodes or decodes acc audio streams using libfDK-acc

3. Build ffmpeg

Compile and output the output to the MacOS directory using the following command

make && make install
Copy the code

The resulting directory structure looks like this

4. Compile libfdk – acc

Since FDK-acc is enabled, liBFDK-acc needs to be compiled separately. Download address: www.linuxfromscratch.org/blfs/view/s… To configure compilation parameters, create the MacOS directory first

./configure --prefix=./macos --disable-static
Copy the code
make && make install
Copy the code

Build macOS+ FFMPEG development environment


1. New construction

Create a new macOS project and create the following directory structure resources: Stores audio and video files

vender: Stores ffMPEG static libraries and header files

FFmpegPlayer: Objective – C project

FFmpegPlayer-Swift: Swift engineering

Once the project is created, import the FFMPEG library and header files

2. Set the search directory for header files

At this point, the preparatory work before coding is done 👏👏👏.

Understand the FFMPEG initialization process

Before coding, it is necessary to understand the basic decoding process of FFMPEG. The following figure roughly describes the process of FFMPEG soft decoding

1. Open the file/data stream

/// FMT: specifies the format of the open audio and video file. If not, it will be automatically derived. // options: Set the options for AVFormatContext, whose default values are defined as libavformat/options_table.h // Int ret = avformat_open_INPUT (&formatContext, URL, NULL, NULL);Copy the code

2. Locate audio and video streams

FormatContet: AVFormatContext: AVFormatContext: AVFormatContext NULL ret = avformat_find_stream_info(formatContext, NULL);Copy the code

This function will read a small number of packets to find the exact audio and video stream information. The packets will be cached and will not be read again when decoded.

3. Traverse the traffic information

Search for audio and video stream information through the read stream information

for(int i = 0; i < formatContext->nb_streams; i ++) { AVStream *stream = formatContext->streams[i]; AVMediaType mediaType = stream->codecpar->codec_type; if(mediaType == AVMEDIA_TYPE_VIDEO) { _mediaVideo = [[FFMediaVideoContext alloc] initWithAVStream:stream formatContext:formatContext]; if(! _mediaVideo) goto fail; } else if(mediaType == AVMEDIA_TYPE_AUDIO) { _mediaAudio = [[FFMediaAudioContext alloc] initWithAVStream:stream formatContext:formatContext]; if(! _mediaAudio) goto fail; }}Copy the code

4. Initialize the audio and video decoder

The parameters of the decoder can be read from AVStream. This structure includes the width and height of the video, FPS, video length and other information. If it is audio, it includes the sampling rate, sound channel, audio packet, audio format, decoder and other information.

AVCodecParameters *codecParameters = stream->codecpar;
Copy the code

AVCodec defines the function of the decoder, first finding the decoder that decodes the corresponding stream information

AVCodec *codec = avcodec_find_decoder(codecParameters->codec_id);
Copy the code

Once the decoder is found, a decoder context needs to be instantiated

AVCodecContext *codecContext = avcodec_alloc_context3(codec);
Copy the code

AVCodec defines the functionality, and the AVCodecContext structure represents the current AVCodec context in which the program is running, focusing on properties common to all AVCoDecs (and whose values are determined at runtime) and fields associated with other structures. AVCodecContext can be thought of as a concrete entity representation of AVCodec, and subsequent decoding operations use AVCodecContext. AVCodecContext not only contains the functions defined by AVCodec, but also needs to be invoked with specific parameters, which are stored in AVCodecParameters. After initialization, you also need to populate the AVCodecContext with the flow information parameters.

avcodec_parameters_to_context(codecContext, codecParameters);
Copy the code

AVCodecContext is closed by default and needs to be opened before decoding can be used

avcodec_open2(codecContext, codec, NULL);
Copy the code

To this, the completion of the audio and video decoder initialization, this is the basic process of soft decoding.

Print audio and video information

1. Print video stream information

NSLog(@"=================== Video Information ===================");
NSLog(@"FPS: %f", av_q2d(stream->avg_frame_rate));
NSLog(@"Duration: %d Seconds", (int)(stream->duration * av_q2d(stream->time_base)));
NSLog(@"Size: (%d, %d)", self->codecContext->width, self->codecContext->height);
NSLog(@"Decodec: %s", self->codec->long_name);
NSLog(@"=========================================================");
Copy the code

Note here a structure that is often used in FFMPEG: AVRational. Its definition is simple as follows:

typedef struct AVRational{
    int num; ///< Numerator
    int den; ///< Denominator
} AVRational;
Copy the code

The denominator and numerator parameters define a value, using such a structure can be very flexible to express any decimal.

For example, the variable time_base in AVStream literally translates to time base.

It means to divide 1 second into 11988, with each minute representing 0.000083416750083416754 seconds, and the value of stream->duration means it has 2490,800 copies based on this time_base, and 2490,800 * 0.000083416750083416754 tells you the total number of seconds in this video.

The av_q2d function is num/den.

Finally, the following video information is printed:

2. Print audio messages

NSLog(@"=================== Audio Information ===================");
NSLog(@"Sample Rate: %d", codecContext->sample_rate);
NSLog(@"FMT: %d, %s", codecContext->sample_fmt, av_get_sample_fmt_name(codecContext->sample_fmt));
NSLog(@"Channels: %d", codecContext->channels);
NSLog(@"Channel Layout: %llu", codecContext->channel_layout);
NSLog(@"Decodec: %s", self->codec->long_name);
NSLog(@"=========================================================");
Copy the code

This information about the audio is important, and it is also needed to initialize the audio player.

Conclusion:

  • Knowing ffMPEG’s capabilities, it can not only use off-the-shelf tools on its own, but can also be integrated into an App to use its API
  • Compiled FFMPEG and recognized the compiled products
  • Set up macOS+ FFMPEG development environment
  • Use FFMPEG and print audio and video stream information

For more content, please pay attention to wechat public number << program ape moving bricks >>