FFmpeg03:多媒体文件处理基础

日志系统

  • #include <libavutil/log.h>
  • av_log_set_level(AV_LOG_DEBUG)
  • av_log(NULL, AV_LOG_INFO, "...%s\n", op)

常用日志级别

AV_LOG_ERROR

AV_LOG_WARNING

AV_LOG_INFO

Demo

log.cpp

cpp 复制代码
#include <iostream>
extern "C" {
#include <libavutil/log.h>
}

int main() {
    // Set the log level to AV_LOG_DEBUG
    av_log_set_level(AV_LOG_DEBUG);

    // Log a message at the INFO level
    av_log(nullptr, AV_LOG_INFO, "This is an informational message.\n");

    // Log a message at the WARNING level
    av_log(nullptr, AV_LOG_WARNING, "This is a warning message.\n");

    // Log a message at the ERROR level
    av_log(nullptr, AV_LOG_ERROR, "This is an error message.\n");

    return 0;
}
shell 复制代码
clang++ log.cpp -o log $(pkg-config --cflags --libs libavutil)

文件的删除与重命名

cpp 复制代码
avpriv_io_delete()
avpriv_io_move()

操作目录

cpp 复制代码
avio_open_dir()
avio_read_dir()
avio_close_dir()

/* -------重要结构体----- */
AVIODirContext // 操作目录的上下文
AVIODirEntry // 目录项,用于存放文件名,文件大小等信息

Demo

实现ls命令

cpp 复制代码
#include <iostream>

extern "C" {
    #include <libavutil/log.h>
    #include <libavformat/avformat.h>
}

int main(void) {

    int ret;
    AVIODirContext *ctx = nullptr;
    AVIODirEntry *entry = nullptr;
    av_log_set_level(AV_LOG_INFO);

    ret = avio_open_dir(&ctx, "./", nullptr);
    if (ret < 0) {
        av_log(nullptr, AV_LOG_ERROR, "Could not open directory: %s\n", av_err2str(ret));
        return ret;
    }

    while (1) {
        ret = avio_read_dir(ctx, &entry);
        if (ret < 0) {
            av_log(nullptr, AV_LOG_ERROR, "Could not read directory: %s\n", av_err2str(ret));
            
            break;
        }
        if (!entry) {
            break; // No more entries
        }

        av_log(nullptr, AV_LOG_INFO, "%12"PRId64" %s\n", entry->size, entry->name);
        avio_free_directory_entry(&entry);
    }

    avio_close_dir(&ctx);
    return 0;
}
shell 复制代码
clang++ ffmpeg_ls.cpp -o ffmpeg_ls $(pkg-config --cflags --libs libavutil libavformat)

处理流数据的基本概念

  • 多媒体文件其实是个容器
  • 在容器里有很多流(Stream/Track)
  • 每种流是由不同的编码器编码的
  • 从流中读出的数据称为包
  • 在一个包中包含着一个或多个帧

几个重要的结构体

  • AVFormatContext
  • AVStream
  • AVPacket

操作流数据的基本步骤

解复用----> 获取流------>读数据包----->释放资源

打印音视频Meta信息

  • av_register_all (5.0以后就被取消了)
  • avformat_open_input() / avformat_close_input
  • av_dump_format()
cpp 复制代码
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/log.h>
}

int main() {
    int ret;
    AVFormatContext *fmt_ctx = nullptr;
    av_log_set_level(AV_LOG_INFO);
    
    // av_register_all(); ffmpeg 4.0 and later do not require this
    ret = avformat_open_input(&fmt_ctx, "./input.mp4", nullptr, nullptr);
    if (ret < 0) {
        av_log(nullptr, AV_LOG_ERROR, "Could not open input file: %s\n", av_err2str(ret));
        return ret;
    }

    av_dump_format(fmt_ctx, 0, "./input.mp4", 0);

    avformat_close_input(&fmt_ctx);

    return 0;
}
shell 复制代码
clang++ ffmpeg_meta.cpp -o mediainfo $(pkg-config --cflags --libs libavutil libavformat)
相关推荐
Everbrilliant891 小时前
FFmpeg解码视频数据ANativeWindow播放
ffmpeg·音视频·ffmpeg视频解码·anativewindow·threadsafequeue·解码线程·渲染线程
hjjdebug12 小时前
ffmpeg 问答系列->demux 部分
ffmpeg·基本概念·流参数
chjqxxxx12 小时前
php使用ffmpeg实现视频随机截图并转成图片
ffmpeg·php·音视频
陈陈陈建蕾3 天前
Mac使用FFmpeg进行屏幕录制,并使用VLC本地播放
ffmpeg·github
vivo互联网技术3 天前
Android动效探索:彻底弄清如何让你的视频更加酷炫
android·ffmpeg·跨平台·图形·mediaplayer·纹理·opengl es·坐标系
stereohomology4 天前
ffmpeg视频mp4到gif用大模型很方便
ffmpeg·音视频
f***45324 天前
从MySQL5.7平滑升级到MySQL8.0的最佳实践分享
ffmpeg
努力还债的学术吗喽6 天前
ffmpeg离线安装到服务器:解决conda/sudo/无法安装的通用方案
服务器·ffmpeg·conda
zymill6 天前
hysAnalyser --- UDP实时流分析使用指南
ffmpeg·ts流分析·mpegts·音视频分析·数字电视流录制·audio vivid·视频分析工具
Everbrilliant897 天前
FFmpeg解码音频数据AudioTrack/OpenSL播放
ffmpeg·音视频·audiotrack·opensl·ffmpeg音频解码播放·decodethread·opensl播放与解码同步