日志系統
- #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
#include <iostream>
extern "C" {
#include <libavutil/log.h>
}int main() {// Set the log level to AV_LOG_DEBUGav_log_set_level(AV_LOG_DEBUG);// Log a message at the INFO levelav_log(nullptr, AV_LOG_INFO, "This is an informational message.\n");// Log a message at the WARNING levelav_log(nullptr, AV_LOG_WARNING, "This is a warning message.\n");// Log a message at the ERROR levelav_log(nullptr, AV_LOG_ERROR, "This is an error message.\n");return 0;
}
clang++ log.cpp -o log $(pkg-config --cflags --libs libavutil)
文件的刪除與重命名
avpriv_io_delete()
avpriv_io_move()
操作目錄
avio_open_dir()
avio_read_dir()
avio_close_dir()/* -------重要結構體----- */
AVIODirContext // 操作目錄的上下文
AVIODirEntry // 目錄項,用于存放文件名,文件大小等信息
Demo
實現ls命令
#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;
}
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()
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 thisret = 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;
}
clang++ ffmpeg_meta.cpp -o mediainfo $(pkg-config --cflags --libs libavutil libavformat)