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

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.lryc.cn/news/493022.html

相关文章:

  • Go错误与日志处理—推荐实践
  • Android 13 Aosp Settings Android Studio版本
  • Jedis存储一个以byte[]的形式的对象到Redis
  • updatexml报错注入原理分析
  • 蓝桥杯c++算法秒杀【6】之动态规划【上】(数字三角形、砝码称重(背包问题)、括号序列、组合数问题:::非常典型的必刷例题!!!)
  • 【Qt】重写QComboBox下拉展示多列数据
  • 【mac】终端左边太长处理,自定义显示名称(terminal路径显示特别长)
  • 基于Springboot的流浪宠物管理系统
  • web博客系统的自动化测试
  • 【论文阅读】Multi-level Semantic Feature Augmentation for One-shot Learning
  • 网络知识面试
  • 图片预览 图片上传到服务器
  • 前端:base64的作用
  • Django在fitler过滤不等于的条件
  • Spring Boot英语知识分享网站:技术与实践
  • 京准电钟:NTP网络校时服务器从入门到精准
  • C++趣味编程玩转物联网:用树莓派Pico控制四位数码管
  • DRM(数字权限管理技术)防截屏录屏----视频转hls流加密、web解密播放
  • 实验三 z变换及离散时间LTI系统的z域分析
  • Python中的DrissionPage详解
  • python除了熟悉的pandas,openpyxl库也很方便的支持编辑Excel表
  • go语言怎么实现bash cmd里的mv功能?
  • Vue前端面试进阶(五)
  • 面试手撕题积累
  • notepad++文件github下载
  • .NET新知识点笔记
  • 数据结构:链表进阶
  • Python 爬虫入门教程:从零构建你的第一个网络爬虫
  • Java面试题、八股文——JVM篇最终篇
  • Spring Boot整合Redis Stack构建本地向量数据库相似性查询