项目作者: VeiZhang

项目描述 :
FFmpeg命令在Android中的使用
高级语言: Java
项目地址: git://github.com/VeiZhang/AndroidFFmpeg.git


FFmpeg for Android

Download

  • 完整引用

    1. // 完整引用,集成所有CPU架构的可执行文件
    2. implementation 'com.excellence:ffmpeg:_latestVersion'
  • 部分引用

    1. implementation 'com.excellence:ffmpeg-java:_latestVersion'
    2. // ffmpeg部分引用,使用想要的CPU架构的可执行文件
    3. implementation 'com.excellence:ffmpeg-arm64-v8a:_latestVersion'
    4. implementation 'com.excellence:ffmpeg-armv7a:_latestVersion'
    5. implementation 'com.excellence:ffmpeg-x86:_latestVersion'
    6. implementation 'com.excellence:ffmpeg-x86_64:_latestVersion'
    7. // ffprobe部分引用,使用想要的CPU架构的可执行文件
    8. implementation 'com.excellence:ffprobe-arm64-v8a:_latestVersion'
    9. implementation 'com.excellence:ffprobe-armv7a:_latestVersion'
    10. implementation 'com.excellence:ffprobe-x86:_latestVersion'
    11. implementation 'com.excellence:ffprobe-x86_64:_latestVersion'

基于AndroidExec项目,FFmpeg命令执行

FFmpeg&FFprobe使用

  1. // 初始化,默认:不限制并发线程数;指令超时10s终止
  2. FFmpeg.init(context);
  3. FFprobe.init(context);
  4. // 自定义初始化参数:超时1s终止
  5. FFmpeg.init(context, new CommanderOptions.Builder().setTimeOut(1000).build())
  6. FFprobe.init(context, new CommanderOptions.Builder().setTimeOut(1000).build())
  7. // 获取FFmpeg工具路径
  8. FFmpeg.checkFFmpeg()
  9. FFprobe.checkFFmpeg()
  10. // 创建执行命令
  11. 推荐方式:new CommandTask.Builder().command(FFmpeg.checkFFmpeg()).command(cmd).build().deploy(IListener);
  12. 丢弃方式:FFmpeg.addTask(cmd, new IListener() {
  13. @Override
  14. public void onPre(String command) {
  15. Log.i(TAG, "onPre: " + command);
  16. }
  17. @Override
  18. public void onProgress(String message) {
  19. Log.i(TAG, "onProgress: " + message);
  20. }
  21. @Override
  22. public void onError(Throwable t) {
  23. t.printStackTrace();
  24. }
  25. @Override
  26. public void onSuccess(String message) {
  27. Log.i(TAG, "onSuccess: " + message);
  28. }
  29. });
  30. // 终止命令
  31. CommandTask.discard()
  32. // 终止所有命令
  33. FFmpeg.destroy()
  34. FFprobe.destroy()

FFmpeg命令

FFMpeg官网

Windows工具下载,解压后,把FFmpeg加入到环境变量中,可在Windows上使用FFmpeg

命令选项

  • 选项参数

    基本语法格式:

    1. ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
  • 常用选项

    1. -version: 显示版本信息。
    2. -formats: 显示所有有效的格式。
    3. -decoders: 显示所有有效解码器。
    4. -encoders: 显示所有有效的编码器。
    5. -bsfs: 显示有效的数据流滤镜。
    6. -pix_fmts: 显示有效的像素格式。
  • 主要选项

    1. -f fmt(input|output): 指定输入或者输出文件格式,可依据扩展名自动指定。
    2. -i filename(input): 指定输入文件。
    3. -y (global): 默认自动覆盖输出文件。
    4. -n (global): 不覆盖输出文件,如果输出文件已存在则立即退出。
    5. -t duration(input|output): 限制输入/输出的时间。
    6. 如果用于输入选项,就是限定从输入中读取多少时间的数据;
    7. 如果用于输出选项,则表示写入多少时间数据后就停止。
    8. duration 可以是以秒为单位的数值活着 hh:mm:ss[.xxx] 格式的时间值。
    9. -to -t 是互斥的,-t 有更高的优先级。
    10. -to time_stop(output): 写入 time_stop 时间后就停止。
    11. time_stop 可以是以秒为单位的数值或者 hh:mm:ss[.xxx] 格式的时间值。
    12. -fs limit_size(output): 设置输出文件大小限制,单位是字节(bytes)。
    13. -ss time_off(input|output): 指定输入文件或输出文件的开始位置。
  • 视频选项

    1. -vframes number(output): 设置输出文件的帧数。
    2. -r rate(input|output): 设置帧率(Hz 值)。
    3. -s size(input|output): 设置帧的尺寸。数据格式是 WxH,即宽度值x高度值。
    4. -aspect aspect(output): 指定视频的长宽显示比例。格式为浮点数字符串或者 num:den 格式字符串。
    5. "4:3""16:9""1.333"等。
    6. -vcodec codec(output): 设置视频编码器。
  • 音频选项

    1. -aframes number(output): 设置输出文件的帧数。
    2. -ar rate(input|output): 设置音频的采样率,单位为 Hz
    3. -aq quality(output): 设置音频品质(编码指定为 VBR)。
    4. -ac channels(input|output): 设置音频通道数。
    5. -af filtergraph(output): 对音频使用 filtergraph 滤镜效果。

常用命令

  • 获取视频信息

    1. ffprobe -v quiet -print_format json -show_format -show_streams inputfile
  • 视频截图

    1. ffmpeg -i input_file -y -f mjpeg -ss 1 -t 0.001 -s widthxheight output_file
    2. i: 源文件
    3. y: 覆盖输出文件
    4. f: 截图格式
    5. ss: 起始位置,单位秒
    6. t: 截图时间,单位秒
    7. s: 图片宽x
  • 每隔 1 秒截一张图

    1. ffmpeg -i input.mp4 -f image2 -vf fps=fps=1 out%d.jpg
  • 每隔 20 秒截一张图

    1. ffmpeg -i input.mp4 -f image2 -vf fps=fps=1/20 out%d.jpg
  • 将视频的前 30 帧转换成一个 Gif

    1. ffmpeg -i input.mp4 -vframes 30 -y -f gif output.gif
  • 从视频中生成 Gif

    1. ffmpeg -i input.mp4 -t 10 -pix_fmt rgb24 output.gif
  • 转换视频为图片(每帧一张图)

    1. ffmpeg -i input.mp4 out%d.jpg
  • 图片转换为视频

    1. ffmpeg -f image2 -i out%d.jpg -r 25 video.mp4
  • 提取视频的关键帧

    1. ffmpeg -i input.mp4 -vf select='eq(pict_type\,I)' -vsync 2 -s 160x90 -f image2 out-%02d.jpeg
  • 分解视频音频流

    1. // 分离视频流
    2. ffmpeg -i input_file -vcodec copy -an output_file_video
    3. // 分离音频流
    4. ffmpeg -i input_file -vcodec copy -vn output_file_audio
  • 视频转码

    1. // 转码为码流原始文件
    2. ffmpeg -i input.mp4 -vcodec h264 -an -f m4v test.264
  • 视频封装

    1. ffmpeg -i video_file -i audio_file -vcodec copy -acodec copy output_file
  • 视频录制

    1. // 录制视频流
    2. ffmpeg -i rtsp://hostname/stream -vcodec copy output.avi
    3. // 通过电脑摄像头录制
    4. ffmpeg -f avfoundation -framerate 30 -i "0" -f mpeg1video -b 500k -r 20 -vf scale=640:360 output.avi

FFprobe命令

  • 查看多媒体包信息

    1. ffprobe -show_packets input.mp4

版本更新

版本 描述
1.2.6 增加ffprobe脚本 2022-6-1
1.2.5 增加OpenSSL 2021-8-23
1.2.4 每次初始化更新FFmpeg 2021-6-29
1.2.3 增加CPU架构 2021-5-30
1.2.2 修复任务偶尔不执行 2019-8-15
1.2.1 设置armeabi为默认的CPU架构 2019-5-29
1.2.0 新增armeabi架构,分离ffmpeg可执行文件 2019-5-21
1.1.1 更新ffmpeg可执行文件,修改低版本机型中任务销毁时导致的阻塞 2019-4-29
1.1.0 使用Builder模式创建命令任务,修复崩溃异常 2018-9-3
1.0.0 集成FFmpeg命令行执行 2018-8-17

感谢