FFmpeg introductory learning 01--open video files

background

FFmpeg is the most commonly used open source software in audio and video development, and many commercial software are developed and customized based on FFmpeg.
Since recent projects at work involve audio and video-related development, this series of articles is used to organize and record some learning experiences in the development process using FFmpeg for future reference.

Basic principles of video playback


The above picture comes from the Internet and describes the basic principles of video playback. That is: to implement a video player, each process in the above figure needs to be completely implemented.
This article starts with opening a video file and records how to use FFmpeg to open a video file.

Open video file

Overview

Before decoding audio and video, you need to open the media file to obtain information about the audio/video stream contained in it. Opening audio and video files involves the following steps:

Create FFmpegPlayer class

Create a FFmpegPlayer class to manage related resources:

class FFmpegPlayer {
public:
    explicit FFmpegPlayer(const char* m_url);
    ~FFmpegPlayer();
public:
    bool        openFile();//打开文件
private:
    std::string             url;//文件路径
    AVFormatContext*        formatContext = nullptr;//封装格式上下文
};

Implement the openFile interface:

bool FFmpegPlayer::openFile() {
    // 创建 formatContext
    formatContext = avformat_alloc_context();
    if( !formatContext )
    {
        avformat_close_input(&formatContext);
        return false;
    }
    // 打开文件
    if (avformat_open_input(&formatContext, url.c_str(), nullptr, nullptr) != 0)
    {
        if( !formatContext ) {
            avformat_close_input(&formatContext);
        }
        return false;
    }

    // 查找输入流信息
    if (avformat_find_stream_info(formatContext, nullptr) < 0)
    {
        if( !formatContext ) {
            avformat_close_input(&formatContext);
        }
        return false;
    }
    // 查找音视频流索引
    int video_index = av_find_best_stream(formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
    int audio_index = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, video_index, nullptr, 0);

    //打印视频信息
    std::cout <<"视频流索引:" << video_index <<std::endl;
    std::cout <<"音频流索引:" << audio_index<<std::endl;
    std::cout <<"时长:"<< formatContext->duration / AV_TIME_BASE <<std::endl;
    std::cout <<"视频宽度:" << formatContext->streams[video_index]->codecpar->width << std::endl;
    std::cout <<"视频高度:" << formatContext->streams[video_index]->codecpar->height << std::endl;
    auto frame_rate =  av_guess_frame_rate(formatContext, formatContext->streams[video_index], nullptr);
    std::cout <<"帧率:" <<  (double )frame_rate.num / frame_rate.den << std::endl;
    
    return true;
}

Run the example

Pass in the video file path and open the file:

#include "FFmpegPlayer.h"
int main() {
    const char  * url = "C:\\Users\\111111\\Desktop\\ts\\0.ts";
    FFmpegPlayer * player = new FFmpegPlayer(url);
    if( player->openFile())
    {
        std::cout << "文件打开成功!"<<std::endl;
    }

    return 0;
}

The result of running the code is as follows:

Original textOriginal text 

★The business card at the end of the article allows you to receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmap, etc.

See below! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/132864545