当前位置: 首页 > article >正文

android集成FFmpeg步骤以及常用命令,踩坑经历

1、入坑第一步:首先集成的库必须正确。最好是有ndk的,FFmpeg有许多个版本,我才开始接触的时候随便选了一个,一般的 方法没有问题。但是涉及到需要使用libx264等条件进行编码时,老是报错,网上搜索资料也没有人说需要ndk的支持才行。这个问题困扰了好几天,怎么试不行,最后更换集成库才成功。

此为引入的库:com.github.yangfeng1994:FFmpeg-Android:v2.0.1

2、图片合成视频命令:

String[] ffmpegCommand = new String[]{
        "-y", // 覆盖输出文件
        "-framerate", 5, // 每5秒一帧,可根据需求调整
        "-i", dir+ "/img%04d.jpg", // 输入图片格式
        "-c:v", "libx264", // 视频编码器
        "-pix_fmt", "yuv420p", // 像素格式
        "-vf", "scale=" + downInfo.getSize(), // 视频分辨率
        "-threads", "1", // 线程数
        "-preset", "ultrafast", // 编码速度
        "-crf", "28", // 编码质量
        out.mp4// 输出视频路径
};

3、加入音频执行命令

String comman = " -i " + videopath+ " -i " + audioPath + " -c:v copy " + outPath;

4、多个视频拼接成一个视频

首先将多个视频文件路径写入file_list.txt

String fileListPath = dir + File.separator + "file_list.txt";
File file = new File(fileListPath);
String fileListContent = "file '" + input1 + "'\nfile '" + input2 + "'";
FileOutputStream outStream = new FileOutputStream(file); // 加上文件名
outStream.write(fileListContent.getBytes());
outStream.close();

执行命令:

String command = "-f concat -safe 0 -i " + fileListPath + " -c:a aac -c copy " + out.mp4;

5、给视频添加水印

String[] command = new String[]{
        "-i", srcFile, // 输入视频路径
        "-i", waterMark, // 水印图片路径
        "-filter_complex", "overlay=x=(W-w)/2:y=H-h*2", // 水印位置
        "-codec:a", "copy", // 音频编码
      //  "-codec:v", "copy", // 视频编码
        targetFile // 输出视频路径
};

视频制作就到此结束了。

以下是一些常用命令:

视频叠加成画中画
String reverseVideo = "-i %s -i %s -filter_complex overlay=%d:%d %s";reverseVideo = String.format(reverseVideo, inputFile1, inputFile2, x, y, targetFile);

 视频抽帧转成图片

String toImage = "-i %s -ss %s -t %s -r %s %s";
toImage = String.format(Locale.CHINESE, toImage, inputFile, startTime, duration, frameRate, targetFile);
toImage = toImage + "%3d.jpg";
视频降噪
String reverseVideo = "-i %s -nr 500 %s";
reverseVideo = String.format(reverseVideo, inputFile, targetFile);
视频反序倒播
String reverseVideo = "-i %s -filter_complex [0:v]reverse[v] -map [v] %s";//单纯视频反序
reverseVideo = String.format(reverseVideo, inputFile, targetFile);

多画面拼接视频

String multiVideo = "-i %s -i %s -filter_complex hstack %s";//hstack:水平拼接,默认

if (videoLayout == LAYOUT_VERTICAL) {//vstack:垂直拼接
    multiVideo = multiVideo.replace("hstack", "vstack");
}
multiVideo = String.format(multiVideo, input1, input2, targetFile);
音频编码
String combineVideo = "-f s16le -ar %d -ac %d -i %s %s";
combineVideo = String.format(combineVideo, sampleRate, channel, srcFile, targetFile);
视频转成Gif动图
String screenShotCmd = "-i %s -ss %d -t %d -s 320x240 -f gif %s";
screenShotCmd = String.format(screenShotCmd, srcFile, startTime, duration, targetFile);
视频截图
String screenShotCmd = "-i %s -f image2 -t 0.001 -s %s %s";
screenShotCmd = String.format(screenShotCmd, srcFile, size, targetFile);
视频剪切
String cutVideoCmd = "-ss %s -t %s -i %s -c:v libx264 -c:a aac -strict experimental -b:a 98k %s";
cutVideoCmd = String.format(cutVideoCmd, startTime, endTime, srcFile, targetFile);
视频转码
String transformVideoCmd = "-i %s -vcodec copy -acodec copy %s";
transformVideoCmd = String.format(transformVideoCmd, srcFile, targetFile);
抽取视频
String mixAudioCmd = "-i %s -vcodec copy -an %s";
mixAudioCmd = String.format(mixAudioCmd, srcFile, targetFile);
抽取音频
String mixAudioCmd = "-i %s -acodec copy -vn %s";
mixAudioCmd = String.format(mixAudioCmd, srcFile, targetFile);
音频混合
String mixAudioCmd = "-i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s";
mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile);
音频合并
String concatAudioCmd = "-i concat:%s|%s -acodec copy %s";
concatAudioCmd = String.format(concatAudioCmd, srcFile, appendFile, targetFile);
音频剪切
String cutAudioCmd = "-i %s -ss %d -t %d %s";
cutAudioCmd = String.format(cutAudioCmd, srcFile, startTime, duration, targetFile);
音频转码
String transformAudioCmd = "-i %s %s";
transformAudioCmd = String.format(transformAudioCmd, srcFile, targetFile);

注意踩坑:1、如果需要使用libx264等硬件编码,一定需要引入支持ndk的依赖库。

                  2、图片合成视频过程中,发现部分图片无法合成视频或者合成视频报错,这也是一处坑,当我的图片的分辨率是830x1080,在将图片动画合成视频时,始终报错,最后经过各种尝试得出一下结论。请检查图片的分辨率是否符合1:1,3:4,4:3,16:9,9:16等比例,如果不是常用的分辨率就会报错。

经过一个多月的研究,FFmpeg已经应用比较熟悉了,如果大家有什么问题或者发现我的方法没有用可以和我交流。大家一起学习一起进步

 


http://www.kler.cn/a/415842.html

相关文章:

  • 第 40 章 - Go语言 模式与反模式
  • 【目标跟踪】Anti-UAV数据集详细介绍
  • 每日十题八股-2024年11月29日
  • 华纳云:服务器网络延迟问题可能由哪些因素引起?
  • 力扣81:搜索旋转排序数组II
  • 软件开发和调试:定位错误位置
  • 服务器实现ssh证书登录
  • PaddleOCR:一款高性能的OCR工具介绍
  • Linux xattr 命令详解
  • Windows下的Milvus安装-保姆级安装教程
  • 【JAVA】Java高级:连接池的使用与性能优化——C3P0、HikariCP与DBCP比较
  • 深度学习——激活函数
  • 基于snowflake id 的 N 位唯一数字id 生成算法总结
  • 浅谈Java库之‌Guava
  • SQL进阶技巧:如何寻找同一批用户 | 断点分组应用【最新面试题】
  • 【机器学习chp8】统计学习理论
  • 【llamafactory】安装与环境配置
  • 使用 Python 删除视频的某一段并保留其他时间段
  • 技术模板纪要
  • 解决SpringBoot连接Websocket报:请求路径 404 No static resource websocket.