FFmpeg读取文件列表

1. $ sudo vim dir.c

include <stdio.h>
#include <inttypes.h>
#include <libavutil/log.h>
#include <libavutil/error.h>
#include <libavformat/avio.h>

//文件类型
static const char *type_string(int type)
{
    switch (type) {
    case AVIO_ENTRY_DIRECTORY:
        return "<DIR>";
    case AVIO_ENTRY_FILE:
        return "<FILE>";
    case AVIO_ENTRY_BLOCK_DEVICE:
        return "<BLOCK DEVICE>";
    case AVIO_ENTRY_CHARACTER_DEVICE:
        return "<CHARACTER DEVICE>";
    case AVIO_ENTRY_NAMED_PIPE:
        return "<PIPE>";
    case AVIO_ENTRY_SYMBOLIC_LINK:
        return "<LINK>";
    case AVIO_ENTRY_SOCKET:
        return "<SOCKET>";
    case AVIO_ENTRY_SERVER:
        return "<SERVER>";
    case AVIO_ENTRY_SHARE:
        return "<SHARE>";
    case AVIO_ENTRY_WORKGROUP:
        return "<WORKGROUP>";
}

int main()
{
    AVIODirEntry *entry = NULL;
    AVIODirContext *ctx = NULL;
    int ret;

    av_log_set_level(AV_LOG_DEBUG);

    if ((ret = avio_open_dir(&ctx, "./", NULL)) < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
        goto fail;
    }

    while(1){
        if ((ret = avio_read_dir(ctx, &entry)) < 0) {
            av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
            goto fail;
        }
        if (!entry)  //当前目录中读不到列表
            break;

        av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %16"PRId64" %16"PRId64" %16"PRId64"\n",
               type_string(entry->type),
               entry->size,
               entry->name,
               entry->modification_timestamp,
               entry->access_timestamp,
               entry->status_change_timestamp);
    avio_free_directory_entry(&entry);  //读完当前列表就close
    }
fail:
    avio_close_dir(&ctx); //当前路径关掉,entry相当于当前路径中的每个列表内容
    return 0;
}

1)avio_open/read/close_dir要包含avio.h,av_err2str在error.h中,
2)PRId64需要#include <inttypes.h>,这是一种跨平台的书写方式,主要是为了同时支持32位和64位操作系统。PRId64表示64位整数,在32位系统中表示long long int,在64位系统中表示long int。相当于:

printf("%" "ld" "\n", value); //64bit OS
printf("%" "lld" "\n", value); //32bit OS

2. gcc编译,生成可执行dir文件

gcc -g -o dir dir.c  `pkg-config -cflags --libs  libavformat libavutil`

3. 执行dir

$ ./dir
<FILE>       744397468          aspect_ratio_SD188.ts 1725330324000000 1725330521000000 1725330324000000
<FILE>           19536                            dir 1725454816000000 1725454828000000 1725454816000000
<FILE>             554                  ffmpeg_file.c 1725421127000000 1725421132000000 1725421127000000
<DIR>             4096                         ffmpeg 1725244680000000 1725358851000000 1725244680000000
<FILE>               0                         10s.ts 1725330119000000 1725330136000000 1725330119000000
<FILE>      3993753600                        out.yuv 1725251667000000 1725251967000000 1725251667000000
<FILE>            1818                          dir.c 1725454814000000 1725454816000000 1725454814000000
<FILE>          496740                        out.pcm 1725252158000000 1725252234000000 1725252158000000
<FILE>           17264                     ffmpeg_log 1725419216000000 1725419216000000 1725419216000000
<FILE>         3932160            event_eachts_192.ts 1725250668000000 1725250726000000 1725250668000000
<DIR>             4096                        minghao 1725421799000000 1725421725000000 1725421799000000
<FILE>               0                          10.ts 1725330584000000 1725330521000000 1725330584000000
<FILE>        10635194                       game.flv 1725359634000000 1725359652000000 1725359634000000
<FILE>             168                   ffmpeg_log.c 1725416437000000 1725416446000000 1725416437000000
<FILE>         3237360                         out.ts 1725329375000000 1725329428000000 1725329375000000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ffmpeg读取MP4文件并保存为jpg的主要函数部分如下所示: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; int videoStream = -1; int frameFinished = 0; int numBytes; uint8_t *buffer = NULL; struct SwsContext *sws_ctx = NULL; int i; // 打开输入文件 if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { fprintf(stderr, "Could not open input file '%s'\n", argv[1]); return -1; } // 获取流信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { fprintf(stderr, "Could not find stream information\n"); return -1; } // 查找视频流 for (i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { fprintf(stderr, "Could not find video stream\n"); return -1; } // 获取视频解码器 pCodec = avcodec_find_decoder(pFormatCtx->streams[videoStream]->codecpar->codec_id); if (pCodec == NULL) { fprintf(stderr, "Unsupported codec\n"); return -1; } // 初始化解码器上下文 pCodecCtx = avcodec_alloc_context3(pCodec); if (avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar) < 0) { fprintf(stderr, "Failed to copy codec parameters to decoder context\n"); return -1; } // 打开解码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { fprintf(stderr, "Failed to open codec\n"); return -1; } // 分配帧内存 pFrame = av_frame_alloc(); if (pFrame == NULL) { fprintf(stderr, "Failed to allocate frame\n"); return -1; } // 分配缓冲区 numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); // 初始化SwsContext sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); // 读取帧并保存为jpg while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { avcodec_send_packet(pCodecCtx, &packet); avcodec_receive_frame(pCodecCtx, pFrame); // 转换像素格式 sws_scale(sws_ctx, (const uint8_t * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrame->data, pFrame->linesize); // 保存为jpg文件 char filename[256]; sprintf(filename, "frame%d.jpg", i++); FILE *file = fopen(filename, "wb"); fwrite(buffer, 1, numBytes, file); fclose(file); } av_packet_unref(&packet); } // 释放资源 av_free(buffer); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; } ``` 请注意,上述代码只是一个示例,可能需要根据实际情况进行适当的修改和调整。此外,还需要包含相应的头文件和链接相应的库文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值