ビデオファイル情報を表示する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$

簡単な分析

実際の操作の前に、通常C言語でファイルを読み取るために必要な手順を分析しましょう。

  • 1.ファイルを開きます。
  • 2.ファイル情報を読み取ります。
  • 3.ファイルを閉じます。

一般的には、次のとおりです。開く->読み取る->3つのステップで閉じる。次に、ffmpegでは少なくともこれらの3つのステップが必要です。

Linuxでの生の読み取りファイルの小さな実験

1.ファイルを作成します

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

2.C言語プログラミングファイルを作成します

#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.コンパイル

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

4.実行する

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

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

ffmpegビデオファイル情報を読む-コード

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;    
}

コンパイル:

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

埋め込む

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$

おすすめ

転載: blog.csdn.net/qq_17623363/article/details/120338723