之前遇到一个需求是将视频一秒一秒解码成一帧一帧的图片,用户滑动选择时间节点(微信朋友圈发10秒视频的编辑界面)。开始我是用的MediaMetadataRetriever类来获取图片,但是对于分辨率比较大的视频(1920*1080)获取一个图片要0.7/0.8秒,太慢了。后来又用FFmpeg的命令来批量的解码视频成一帧一帧的图片,速度依然不快每张图片得耗费0.5秒左右的时间。最后还是用FFmpeg,不过不是用命令行,而是用NDK,发现c解码视频再转换为图片保存起来效率大大挺高,一秒钟能解码4、5张图片,c就是快啊,做一下总结。

附上源码:
http://download.csdn.net/download/qq_28284547/10031785
GitHub:https://github.com/kui92/FFmpegTools

    创建工程,注意要勾选上支持c++。

我从网上找到了编译好的FFmpeg文件,直接考到自己的工程里的。

配置gradle与CMakeLists
gradle

CMakeLists

cmake_minimum_required(VERSION 3.4.1)
include_directories(
${CMAKE_SOURCE_DIR}/src/main/cpp/include
)

add_library( # Sets the name of the library.
jxffmpegrun

         # Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).src/main/cpp/cmdutils.csrc/main/cpp/ffmpeg.csrc/main/cpp/ffmpeg_filter.csrc/main/cpp/ffmpeg_opt.csrc/main/cpp/jx_ffmpeg_cmd_run.c)

add_library(avcodec SHARED IMPORTED)

add_library(avfilter SHARED IMPORTED)

add_library(avformat SHARED IMPORTED)

add_library(avutil SHARED IMPORTED)

add_library(swresample SHARED IMPORTED)

add_library(swscale SHARED IMPORTED)

add_library(fdk-aac SHARED IMPORTED)

set_target_properties(
avcodec
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libavcodec.so
)

set_target_properties(
avfilter
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libavfilter.so
)

set_target_properties(
avformat
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libavformat.so
)

set_target_properties(
avutil
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libavutil.so
)

set_target_properties(
swresample
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libswresample.so
)

set_target_properties(
swscale
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libswscale.so
)

set_target_properties(
fdk-aac
PROPERTIES IMPORTED_LOCATION
CMAKESOURCEDIR/src/main/jniLibs/{CMAKE_SOURCE_DIR}/src/main/jniLibs/{ANDROID_ABI}/libfdk-aac.so
)

target_link_libraries( # Specifies the target library.
jxffmpegrun
log
android
fdk-aac
avcodec
avfilter
avformat
avutil
swresample
swscale
# Links the target library to the log library
# included in the NDK.
${log-lib} )

新建FfmpegTool类,编写native方法并生成头文件(头文件的生成方法可自行百度)

实现native方法

//实现native方法
#define  LOG_TAG    "videoplayer"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
/*** 将AVFrame(YUV420格式)保存为JPEG格式的图片** @param width YUV420的宽* @param height YUV42的高**/
int MyWriteJPEG(AVFrame *pFrame,char *path,  int width, int height, int iIndex) {// 输出文件路径char out_file[1000] = {0};LOGD("path:%s", path);sprintf(out_file, "%stemp%d.jpg", path, iIndex);LOGD("out_file:%s", out_file);// 分配AVFormatContext对象AVFormatContext *pFormatCtx = avformat_alloc_context();// 设置输出文件格式pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);// 创建并初始化一个和该url相关的AVIOContextif (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {LOGD("Couldn't open output file.");return -1;}// 构建一个新streamAVStream *pAVStream = avformat_new_stream(pFormatCtx, 0);if (pAVStream == NULL) {return -1;}// 设置该stream的信息AVCodecContext *pCodecCtx = pAVStream->codec;pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;pCodecCtx->width = width;pCodecCtx->height = height;pCodecCtx->time_base.num = 1;pCodecCtx->time_base.den = 25;// Begin Output some informationav_dump_format(pFormatCtx, 0, out_file, 1);// End Output some information// 查找解码器AVCodec *pCodec = avcodec_find_encoder(pCodecCtx->codec_id);if (!pCodec) {LOGD("Codec not found.");return -1;}// 设置pCodecCtx的解码器为pCodecif (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {LOGD("Could not open codec.");return -1;}//Write Headeravformat_write_header(pFormatCtx, NULL);int y_size = pCodecCtx->width * pCodecCtx->height;//Encode// 给AVPacket分配足够大的空间AVPacket pkt;av_new_packet(&pkt, y_size * 3);//int got_picture = 0;int ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);if (ret < 0) {LOGD("Encode Error.\n");return -1;}if (got_picture == 1) {//pkt.stream_index = pAVStream->index;ret = av_write_frame(pFormatCtx, &pkt);}av_free_packet(&pkt);//Write Trailerav_write_trailer(pFormatCtx);LOGD("Encode Successful.\n");if (pAVStream) {avcodec_close(pAVStream->codec);}avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);return 0;
}/***方法的实现入口*/
JNIEXPORT jint JNICALL Java_com_esay_ffmtool_FfmpegTool_decodToImage(JNIEnv * env, jclass mclass, jstring in, jstring dir, jint startTime, jint num){char * input=jstringTostring(env,in);char * parent=jstringTostring(env,dir);LOGD("input:%s",input);LOGD("parent:%s",parent);av_register_all();AVFormatContext *pFormatCtx = avformat_alloc_context();// Open video fileif (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0) {LOGD("Couldn't open file:%s\n", input);return -1; // Couldn't open file}// Retrieve stream informationif (avformat_find_stream_info(pFormatCtx, NULL) < 0) {LOGD("Couldn't find stream information.");return -1;}// Find the first video streamint videoStream = -1, i;for (i = 0; i < pFormatCtx->nb_streams; i++) {if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO&& videoStream < 0) {videoStream = i;}}if (videoStream == -1) {LOGD("Didn't find a video stream.");return -1; // Didn't find a video stream}// Get a pointer to the codec context for the video streamAVCodecContext *pCodecCtx = pFormatCtx->streams[videoStream]->codec;// Find the decoder for the video streamAVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {LOGD("Codec not found.");return -1; // Codec not found}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {LOGD("Could not open codec.");return -1; // Could not open codec}// Allocate video frameAVFrame *pFrame = av_frame_alloc();if (pFrame == NULL) {LOGD("Could not allocate video frame.");return -1;}int64_t count = startTime;int frameFinished;//(*pCodecCtx).AVPacket packet;LOGD("start_time:%d",pFormatCtx->start_time);LOGD("pFormatCtx den:%d", pFormatCtx->streams[videoStream]->sample_aspect_ratio.den);LOGD("pFormatCtx num:%d", pFormatCtx->streams[videoStream]->sample_aspect_ratio.num);av_seek_frame(pFormatCtx,-1,(int64_t)count*AV_TIME_BASE,AVSEEK_FLAG_FRAME);while (av_read_frame(pFormatCtx, &packet) >= 0){if (packet.stream_index == videoStream){// Decode video frameavcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);// 并不是decode一次就可解码出一帧if (frameFinished){if(count<(startTime+num)){MyWriteJPEG(pFrame,parent, pCodecCtx->width,pCodecCtx->height, count);++count;av_seek_frame(pFormatCtx,-1,(int64_t)count*AV_TIME_BASE,AVSEEK_FLAG_FRAME);} else{av_packet_unref(&packet);LOGD("break:");break;}}}av_packet_unref(&packet);}// Free the YUV frameav_free(pFrame);// Close the codecsavcodec_close(pCodecCtx);// Close the video fileavformat_close_input(&pFormatCtx);return  0;
}

链接so库,在类FfmpegTool开头加入代码:

 static {System.loadLibrary("avutil");System.loadLibrary("fdk-aac");System.loadLibrary("avcodec");System.loadLibrary("avformat");System.loadLibrary("swscale");System.loadLibrary("swresample");System.loadLibrary("avfilter");System.loadLibrary("jxffmpegrun");}
//测试运行,在MainActivity中添加按钮执行如下方法,注意FfmpegTool.decodToImage方法是阻塞的,有时候耗时较长要放在子线程。
public void click2(View view){String path= "图片要保存的路径";String video="视频路径";FfmpegTool.decodToImage(video,path,0,60);}

最后再次附上源码:
http://download.csdn.net/download/qq_28284547/10031785
GitHub:https://github.com/kui92/FFmpegTools

FFmpeg解码视频帧为jpg图片保存到本地相关推荐

  1. 【OpenCV】获得视频的帧数、FPS以及按帧数将图片保存到本地

    文章目录 视频操作 获得视频的帧数 获得视频的FPS 按帧数将图片保存到本地 视频操作 获得视频的帧数 import os import cv2video_cap = cv2.VideoCapture ...

  2. php将视频流逐帧转图片,ffmpeg sdk解码视频帧后保存成BMP或JPG的方法

    ffmpeg解码视频的例子可以看官方自带的encode_decode.c. 官方解码保存成ppm,这里接下来保存成BMP或JPG. 原理: 保存BMP是解码成功后,从YUV420转成RGB24,然后构 ...

  3. 视频帧数(图片)和音频提取及保存方法图片合成视频方法---ffmpeg

    视频帧数(图片)和音频提取及保存方法&图片合成视频方法-ffmpeg 环境:Ubuntu16.04.Python3.5.anaconda3 任务需要,要从视频里提取一定帧数的图片和音频,查了不 ...

  4. ffmpeg解码视频存为BMP文件

    ffmpeg解码视频存为BMP文件 分类: ffmpeg2011-07-28 12:13 8人阅读 评论(0) 收藏 举报 view plain #include <windows.h> ...

  5. ffmpeg解码视频文件并播放

    最近学习了一下如何使用ffmpeg解码音视频,网上的教程挺多但是也挺杂的,搞了好几天,明白了ffmpeg解码音视频的大体流程,这里记录一下ffmpeg解码视频并播放音视频的例子,但并没有做音频.视频播 ...

  6. ffmpeg 解码视频(h264、mpeg2)输出yuv420p文件

    ffmpeg 解码视频(h264.mpeg2)输出yuv420p文件 播放yuv可以参考:ffplay -pixel_format yuv420p -video_size 768x320 -frame ...

  7. 采用FFmpeg从视频中提取音频(声音)保存为mp3文件

    采用FFmpeg从视频中提取音频(声音)保存为mp3文件 作者:雨水,日期:2016年1月9日 CSDN博客:http://blog.csdn.net/gobitan 摘要:看到好的视频文件,如果想把 ...

  8. 从零实现简易播放器:4.ffmpeg 解码视频为yuv数据-使用avcodec_send_packet与avcodec_receive_frame

    ffmpeg 解码视频为yuv数据 作者:史正 邮箱:shizheng163@126.com 如有错误还请及时指正 如果有错误的描述给您带来不便还请见谅 如需交流请发送邮件,欢迎联系 csdn : h ...

  9. java中抓拍图像_JavaCV调用摄像头并抓拍图片保存到本地

    添加依赖 org.bytedeco javacv-platform 1.4.1 org.bytedeco.javacpp-presets opencv-platform 3.4.1-1.4.1 jun ...

最新文章

  1. iOS开发--面试总结(二)
  2. 【数据结构与算法】之深入解析“打家劫舍III”的求解思路与算法示例
  3. Java EE 6 VS Spring 3:Java EE杀死了Spring? 没门!
  4. office套件_【office】Android版微软办公套件Office独立版一体化
  5. 对象测试_心理测试:你会选择跟对象去吃什么夜宵?测你治愈失恋的方法是什么...
  6. silverlight下多线程处理
  7. Visio画图如何保存高质量图片供论文使用
  8. 百度编辑器(UEditor)工具栏扩展秀米的编辑器工具
  9. 希捷移动硬盘linux,[转载]强列建议不要买seagate希捷移动硬盘!!!
  10. 蛙蛙推荐:蛙蛙牌网页捕捉器
  11. 基于强化学习与深度强化学习的游戏AI训练
  12. 只要两步,用Python将地址标记在地图上!
  13. 经纬度坐标转换到平面坐标
  14. 响应式网页设计_响应式网页设计–如何使网站在手机和平​​板电脑上看起来不错
  15. 开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
  16. 关于java 编译器级别与项目版本不匹配
  17. 数据中台 第5章 数据汇聚联通:打破企业数据孤岛
  18. 使用tensorflow和densenet神经网路实现语谱图声纹识别,即说话人识别。
  19. 选对池塘钓大鱼-人生选择的哲理
  20. 红旗linux进不去系统,为什么红旗linux6 0不能进系统

热门文章

  1. 基于Anaconda 搭建 OpenCV for Python 环境(全平台通用)
  2. java怎么向二维数组赋值_如何给JAVA二维数组赋值
  3. 中小企业进行工业互联网改革的痛点和解决方案
  4. 情人节程序员用HTML网页表白【粒子动画】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
  5. 【NodeJs篇】npm和包
  6. 水利RTU遥测终端机厂家
  7. 再论互联网公司盖楼那些事-东邪阿里
  8. 在WPF中加载gif动态图片
  9. 电脑重装系统后鼠标动不了该怎么解决
  10. Electron常见问题 3-Error: sha512 checksum mismatch, expected