Java调用ffmpeg进行视频.H264抽帧,并保存为图片

236 篇文章 43 订阅
126 篇文章 7 订阅

Java调用ffmpeg进行视频.H264抽帧,并保存为图片

1. 需求

对视频 D:\data\01-test.H264进行抽帧并保存为图片,图片命名为1.jpg,2.jpg…,图片保存在D:\data\01-test\image\路径;

2. 解决

命令行如下

需要注意输出路径D:\data\01-test\image\ 必须已经存在,才可以执行成功;

ffmpeg.exe -y -i D:\data\01-test.H264 -ss 00:00:00 -qscale:v 2 -f image2 -vsync 2 D:\data\01-test\image\%d.jpg

-qscale:v 2~31 图像质量,2代表高质量

  • -y 覆盖输出文件,不需要确认
  • -i 指定源视频文件路径
  • -ss 指定起始时间 -i与-ss 调换位置 -ss在前面,则会使用关键帧信息来进行索引,则会非常快。
  • -f image2 强迫采用格式image2,-f fmt 强制输入或输出文件格式。输入文件的格式通常
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 C 语言中调用 FFmpeg 进行视频抽帧,可以使用 FFmpeg 的 C API。以下是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> #define INBUF_SIZE 4096 int main(int argc, char **argv) { AVFormatContext *fmt_ctx = NULL; AVCodecContext *dec_ctx = NULL; AVCodec *dec = NULL; AVPacket pkt; int ret, i; if (argc != 2) { fprintf(stderr, "Usage: %s <input_file>\n", argv[0]); exit(1); } av_register_all(); if (avformat_open_input(&fmt_ctx, argv[1], NULL, NULL) < 0) { fprintf(stderr, "Could not open source file %s\n", argv[1]); exit(1); } if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { fprintf(stderr, "Could not find stream information\n"); exit(1); } for (i = 0; i < fmt_ctx->nb_streams; i++) { AVStream *stream = fmt_ctx->streams[i]; dec_ctx = stream->codec; dec = avcodec_find_decoder(dec_ctx->codec_id); if (!dec) { fprintf(stderr, "Failed to find decoder for stream #%d\n", i); exit(1); } if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { fprintf(stderr, "Failed to open decoder for stream #%d\n", i); exit(1); } } av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; while (av_read_frame(fmt_ctx, &pkt) >= 0) { if (pkt.stream_index == i) { AVFrame *frame = av_frame_alloc(); int got_frame; if (!frame) { fprintf(stderr, "Could not allocate frame\n"); exit(1); } ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &pkt); if (ret < 0) { fprintf(stderr, "Error decoding video frame\n"); exit(1); } if (got_frame) { char buf[1024]; snprintf(buf, sizeof(buf), "frame-%d.pgm", dec_ctx->frame_number); FILE *f = fopen(buf, "wb"); if (!f) { fprintf(stderr, "Could not open output file\n"); exit(1); } AVFrame *rgb = av_frame_alloc(); if (!rgb) { fprintf(stderr, "Could not allocate RGB frame\n"); exit(1); } int w = dec_ctx->width; int h = dec_ctx->height; av_image_alloc(rgb->data, rgb->linesize, w, h, AV_PIX_FMT_RGB24, 1); struct SwsContext *sws_ctx = sws_getContext(w, h, dec_ctx->pix_fmt, w, h, AV_PIX_FMT_RGB24, 0, NULL, NULL, NULL); if (!sws_ctx) { fprintf(stderr, "Could not initialize conversion context\n"); exit(1); } sws_scale(sws_ctx, (const uint8_t * const *)frame->data, frame->linesize, 0, h, rgb->data, rgb->linesize); fprintf(f, "P6\n%d %d\n255\n", w, h); fwrite(rgb->data[0], 1, w * h * 3, f); fclose(f); av_freep(&rgb->data[0]); av_frame_free(&rgb); } av_frame_free(&frame); } av_packet_unref(&pkt); } avcodec_free_context(&dec_ctx); avformat_close_input(&fmt_ctx); return 0; } ``` 上面的代码会将输入文件的视频抽帧,并将每一帧转换为 PGM 图像格式并保存到磁盘上。你可以根据自己的需求修改代码来实现更复杂的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值