ffmpeg programming to view video file information

The effect is as follows

insert image description here

Execution effect print log

zh@zh-lpc:~/project/ffmpeg$ ./frmi  test.mp4
open test.mp4 success.

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    title           : BigBuckBunny_115k.mov
    encoder         : Lavf58.76.100
  Duration: 00:05:52.96, bitrate: N/A
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x160, 87 kb/s, 23.88 fps, 24 tbr, 90k tbn (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 12000 Hz, 2 channels, 32 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
zh@zh-lpc:~/project/ffmpeg$

Simple Analysis

Before the actual operation, let's analyze the steps we need to read a file in C language normally:

  • 1. Open the file;
  • 2. Read file information;
  • 3. Close the file.

Generally it is: open --> read --> close in three steps. Then at least these three steps are required in ffmpeg.

Small experiment of raw reading file in Linux

1. Create a file

zh@zh-lpc:~/project/unixapi$ echo  "aaaaa三生三世十里桃花123456" > info.txt

2. Create a C language programming file

#include <stdio.h>
#include <fcntl.h>

#define BUFFSIZE 1024


int main()
{
    
    

    int ret = 0;
    FILE *file;
    char buf[BUFFSIZE];
    char *fileName = "./info.txt";

    //open file
    file = fopen(fileName,"r");

    //read file
    fread(buf, BUFFSIZE+1, 1, file);

    printf("%s\n", buf);

    //close file
    fclose(file);
    
    return 0;
}

3. Compile

zh@zh-lpc:~/project/unixapi$ make unix_file
cc     unix_file.c   -o unix_file
zh@zh-lpc:~/project/unixapi$

4. Execute

zh@zh-lpc:~/project/unixapi$ ./unix_file
aaaaa三生三世十里桃花123456

zh@zh-lpc:~/project/unixapi$

ffmpeg read video file information - code

ffmpeg_read_media_info.c:

/**
 * use ffmpeg codeing read flow file info
 **/
#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avformat.h>

int main(int argc, char *argv[])
{
    
    
    int ret = 0;
    const char* fileName = "";
    AVFormatContext *ac =  NULL;
    //set log level
    av_log_set_level(AV_LOG_INFO);

    //diff params = 2 is params != 2
    if(argc != 2)
    {
    
    
        av_log(NULL,AV_LOG_WARNING,"params not enough. \n\n");
        return -1;
    }

    //file name 
    fileName = argv[1];

    //open video file
    ret = avformat_open_input(&ac,fileName,NULL,NULL);

    //diff open success or error
    if(ret < 0)
    {
    
    
        av_log(NULL,AV_LOG_ERROR,"open %s error. \n\n",fileName);
        return -1;
    }else{
    
    
        av_log(NULL,AV_LOG_INFO,"open %s success. \n\n",fileName);
    }

    av_dump_format(ac,0,fileName,0);

    //close file flow
    avformat_close_input(&ac);

    return 0;    
}

Compile:

gcc -g -o frmi ffmpeg_read_media_info.c  -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavformat -lavutil

implement

zh@zh-lpc:~/project/ffmpeg$ ls -l test.mp4
-rwx------ 1 zh zh 5431627 910 23:05 test.mp4
zh@zh-lpc:~/project/ffmpeg$
zh@zh-lpc:~/project/ffmpeg$ ls -l frmi
-rwxrwxr-x 1 zh zh 65152 916 22:00 frmi
zh@zh-lpc:~/project/ffmpeg$
zh@zh-lpc:~/project/ffmpeg$ ls -l ffmpeg_read_media_info.c
-rwx------ 1 zh zh 955 916 22:02 ffmpeg_read_media_info.c
zh@zh-lpc:~/project/ffmpeg$
zh@zh-lpc:~/project/ffmpeg$
zh@zh-lpc:~/project/ffmpeg$ ./frmi test.mp4
open test.mp4 success.

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    title           : BigBuckBunny_115k.mov
    encoder         : Lavf58.76.100
  Duration: 00:05:52.96, bitrate: N/A
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x160, 87 kb/s, 23.88 fps, 24 tbr, 90k tbn (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 12000 Hz, 2 channels, 32 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
zh@zh-lpc:~/project/ffmpeg$

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/120338723