当前位置:网站首页>Ffmpe a small demo to understand 80% of common APIs

Ffmpe a small demo to understand 80% of common APIs

2022-06-11 07:26:00 Nwafu_ zyccc

#include <stdio.h>
#include <libavformat/avformat.h>


int main(int argc, char **argv)
{
    
    // Turn on network flow . Here, if you only need to read local media files , No need to use network functions , You don't have to add this sentence 
//    avformat_network_init();

    const char *default_filename = "";

    char *in_filename = NULL;

    if(argv[1] == NULL)
    {
    
        in_filename = default_filename;
    }
    else
    {
    
        in_filename = argv[1];
    }
    printf("in_filename = %s\n", in_filename);

    //AVFormatContext It is a structure that describes the composition and basic information of a media file or media stream 
    AVFormatContext *ifmt_ctx = NULL;           //  Input file demux

    int videoindex = -1;        //  Video index 
    int audioindex = -1;        //  Audio index 


    //  Open file , Mainly the detection protocol type , If it is a network file, create a network link 
    int ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL);
    if (ret < 0)  // If opening the media file fails , Reason for printing failure 
    {
    
        char buf[1024] = {
     0 };
        av_strerror(ret, buf, sizeof(buf) - 1);
        printf("open %s failed:%s\n", in_filename, buf);
        goto failed;
    }

    ret = avformat_find_stream_info(ifmt_ctx, NULL);
    if (ret < 0)  // If opening the media file fails , Reason for printing failure 
    {
    
        char buf[1024] = {
     0 };
        av_strerror(ret, buf, sizeof(buf) - 1);
        printf("avformat_find_stream_info %s failed:%s\n", in_filename, buf);
        goto failed;
    }

    // Media file opened successfully 
    printf_s("\n==== av_dump_format in_filename:%s ===\n", in_filename);
    av_dump_format(ifmt_ctx, 0, in_filename, 0);
    printf_s("\n==== av_dump_format finish =======\n\n");
    // url:  call avformat_open_input The path of the read media file / name 
    printf("media name:%s\n", ifmt_ctx->url);
    // nb_streams: nb_streams Number of media streams 
    printf("stream number:%d\n", ifmt_ctx->nb_streams);
    // bit_rate:  Bit rate of media file , Unit is bps
    printf("media average ratio:%lldkbps\n",(int64_t)(ifmt_ctx->bit_rate/1024));
    //  Time 
    int total_seconds, hour, minute, second;
    // duration:  Media file duration , Unit subtlety 
    total_seconds = (ifmt_ctx->duration) / AV_TIME_BASE;  // 1000us = 1ms, 1000ms = 1 second 
    hour = total_seconds / 3600;
    minute = (total_seconds % 3600) / 60;
    second = (total_seconds % 60);
    // Through the above operation , You can get the total length of media files 
    printf("total duration: %02d:%02d:%02d\n", hour, minute, second);
    printf("\n");
    /*
     *  The old version reads the information of media files, video and audio through traversal 
     *  The new version of the FFmpeg Newly added function av_find_best_stream, The same effect can be achieved 
     */
    for (uint32_t i = 0; i < ifmt_ctx->nb_streams; i++)
    {
    
        AVStream *in_stream = ifmt_ctx->streams[i];//  Audio stream 、 Video streaming 、 Subtitle stream 
        // If it's an audio stream , Then print the audio information 
        if (AVMEDIA_TYPE_AUDIO == in_stream->codecpar->codec_type)
        {
    
            printf("----- Audio info:\n");
            // index:  Each stream component is in ffmpeg After demultiplexing analysis, there is a unique index As identification 
            printf("index:%d\n", in_stream->index);
            // sample_rate:  Sampling rate of audio codec , Unit is Hz
            printf("samplerate:%dHz\n", in_stream->codecpar->sample_rate);
            // codecpar->format:  Audio sampling format 
            if (AV_SAMPLE_FMT_FLTP == in_stream->codecpar->format)
            {
    
                printf("sampleformat:AV_SAMPLE_FMT_FLTP\n");
            }
            else if (AV_SAMPLE_FMT_S16P == in_stream->codecpar->format)
            {
    
                printf("sampleformat:AV_SAMPLE_FMT_S16P\n");
            }
            // channels:  Number of audio channels 
            printf("channel number:%d\n", in_stream->codecpar->channels);
            // codec_id:  Audio compression coding format 
            if (AV_CODEC_ID_AAC == in_stream->codecpar->codec_id)
            {
    
                printf("audio codec:AAC\n");
            }
            else if (AV_CODEC_ID_MP3 == in_stream->codecpar->codec_id)
            {
    
                printf("audio codec:MP3\n");
            }
            else
            {
    
                printf("audio codec_id:%d\n", in_stream->codecpar->codec_id);
            }
            //  Total audio duration , The unit is in seconds . Note that if you enlarge the unit to milliseconds or subtle , The total audio duration is not necessarily equal to the total video duration 
            if(in_stream->duration != AV_NOPTS_VALUE)
            {
    
                int duration_audio = (in_stream->duration) * av_q2d(in_stream->time_base);
                // Convert the total audio duration into minutes and seconds and print it to the console 
                printf("audio duration: %02d:%02d:%02d\n",
                       duration_audio / 3600, (duration_audio % 3600) / 60, (duration_audio % 60));
            }
            else
            {
    
                printf("audio duration unknown");
            }

            printf("\n");

            audioindex = i; //  Get the index of audio 
        }
        else if (AVMEDIA_TYPE_VIDEO == in_stream->codecpar->codec_type)  // If it's a video stream , Then print the information of the video 
        {
    
            printf("----- Video info:\n");
            printf("index:%d\n", in_stream->index);
            // avg_frame_rate:  Video frame rate , Unit is fps, Indicates how many frames appear per second 
            printf("fps:%lffps\n", av_q2d(in_stream->avg_frame_rate));
            if (AV_CODEC_ID_MPEG4 == in_stream->codecpar->codec_id) // Video compression coding format 
            {
    
                printf("video codec:MPEG4\n");
            }
            else if (AV_CODEC_ID_H264 == in_stream->codecpar->codec_id) // Video compression coding format 
            {
    
                printf("video codec:H264\n");
            }
            else
            {
    
                printf("video codec_id:%d\n", in_stream->codecpar->codec_id);
            }
            //  Video frame width and frame height 
            printf("width:%d height:%d\n", in_stream->codecpar->width,
                   in_stream->codecpar->height);
            // Total video duration , The unit is in seconds . Note that if you enlarge the unit to milliseconds or subtle , The total audio duration is not necessarily equal to the total video duration 
            if(in_stream->duration != AV_NOPTS_VALUE)
            {
    
                int duration_video = (in_stream->duration) * av_q2d(in_stream->time_base);
                printf("video duration: %02d:%02d:%02d\n",
                       duration_video / 3600,
                       (duration_video % 3600) / 60,
                       (duration_video % 60)); // Convert the total video duration into minutes and seconds and print it to the console 
            }
            else
            {
    
                printf("video duration unknown");
            }

            printf("\n");
            videoindex = i;
        }
    }

    AVPacket *pkt = av_packet_alloc();

    int pkt_count = 0;
    int print_max_count = 10;
    printf("\n-----av_read_frame start\n");
    while (1)
    {
    
        ret = av_read_frame(ifmt_ctx, pkt);
        if (ret < 0)
        {
    
            printf("av_read_frame end\n");
            break;
        }

        if(pkt_count++ < print_max_count)
        {
    
            if (pkt->stream_index == audioindex)
            {
    
                printf("audio pts: %lld\n", pkt->pts);
                printf("audio dts: %lld\n", pkt->dts);
                printf("audio size: %d\n", pkt->size);
                printf("audio pos: %lld\n", pkt->pos);
                printf("audio duration: %lf\n\n",
                       pkt->duration * av_q2d(ifmt_ctx->streams[audioindex]->time_base));
            }
            else if (pkt->stream_index == videoindex)
            {
    
                printf("video pts: %lld\n", pkt->pts);
                printf("video dts: %lld\n", pkt->dts);
                printf("video size: %d\n", pkt->size);
                printf("video pos: %lld\n", pkt->pos);
                printf("video duration: %lf\n\n",
                       pkt->duration * av_q2d(ifmt_ctx->streams[videoindex]->time_base));
            }
            else
            {
    
                printf("unknown stream_index:\n", pkt->stream_index);
            }
        }

        av_packet_unref(pkt);
    }

    if(pkt)
        av_packet_free(&pkt);
failed:
    if(ifmt_ctx)
        avformat_close_input(&ifmt_ctx);


    getchar(); // Add this sentence , Prevent the program from exiting immediately after printing the information 
    return 0;
}

原网站

版权声明
本文为[Nwafu_ zyccc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020521358496.html