调用FFmpeg API

环境:Mac OS

编译器:Apple clang version 13.0.0

linux也可以用下面脚本

使用下面脚本一键搭建FFmpeg 4.2.2环境,默认会下载到用户主目录下

#!/bin/bash

ff_version="ffmpeg-4.2.2"
ff="${ff_version}.tar.bz2"

main(){
    
    
cd ~/

if [ ! -d $ff_version ];then
   if [ ! -f "$ff" ];then wget https://ffmpeg.org/releases/$ff;fi
   tar -xjvf $ff
fi


cd $ff_version

if [ -f "Changelog" ];then
   rm Changelog *.md COPYING.* CREDITS MAINTAINERS RELEASE* VERSION
fi

echo "IyEvYmluL2Jhc2gKCmlmIFsgISAtZCBidWlsZCBdO3RoZW4KICAgIG1rZGlyIGJ1aWxkCmZpCi4vY29uZmlndXJlIFwKICAgIC0tcHJlZml4PSQocHdkKS9idWlsZCBcCiAgICAtLWVuYWJsZS1zaGFyZWQgXAogICAgLS1kaXNhYmxlLWFzbSBcCiAgICAtLWRpc2FibGUtZG9jIFwKICAgIC0tZGlzYWJsZS1uZXR3b3JrIFwKICAgIC0tZGlzYWJsZS1mZnByb2JlIFwKICAgIC0tZGlzYWJsZS1mZnBsYXkgXAogICAgLS1kaXNhYmxlLWF2ZGV2aWNlIFwKCm1ha2UgLWogMTYgJiYgbWFrZSBpbnN0YWxsCgpraW5kPSIqLnNvIgpjaGVja19vcygpewogICAgY2FzZSAiJCh1bmFtZSkiIGluCiAgICAiRGFyd2luIikga2luZD0iKi5keWxpYiIgOzsKICAgIGVzYWMKfQoKY2hlY2tfb3MKCm1rZGlyIGxpYgpjcCAkKGZpbmQgLiAtbmFtZSAiJHtraW5kfSIpIC4vbGliLwo=" | base64 -d > load.sh && chmod +x load.sh

cat load.sh
echo -e "\033[31mAre you ok?[Y/n]\033[0m"
read ok
if [ "$ok" == "n" ];then return;fi
./load.sh
}

main

调用源代码main.cpp

#include<iostream>

extern "C" {
    
    
#include "libavformat/avformat.h"
}


int main(){
    
    
   AVFormatContext *fmt_ctx = nullptr;
   std::string filename = "./demo.mp4";

   fmt_ctx = avformat_alloc_context();

   AVDictionary *format_opts = nullptr;
   av_dict_set(&format_opts, "probesize" , "32" , 0);

   avformat_open_input(&fmt_ctx,filename.c_str(),nullptr,&format_opts);

   avformat_find_stream_info(fmt_ctx,nullptr);
   printf("duration->%lld\n", fmt_ctx->duration);
   printf("nb_streams->%u\n", fmt_ctx->nb_streams);

   for (int i = 0; i < fmt_ctx->nb_streams; i++) {
    
    
      printf("stream codec type->%d\n", fmt_ctx->streams[i]->codecpar->codec_type);
   }

   printf("iformat name ->%s\n", fmt_ctx->iformat->name);
   printf("iformat long name ->%s\n", fmt_ctx->iformat->long_name);

   av_dict_free(&format_opts);

   return 0;
}

编译

clang++ main.cpp -I ~/ffmpeg-4.2.2/build/include/ -L ~/ffmpeg-4.2.2/build/lib/ -lavformat -o main

运行信息

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fda4400bc00] decoding for stream 0 failed
duration->636149000
nb_streams->2
stream codec type->0
stream codec type->1
iformat name ->mov,mp4,m4a,3gp,3g2,mj2
iformat long name ->QuickTime / MOV

猜你喜欢

转载自blog.csdn.net/csdn546229768/article/details/128728743