Java中怎么利用ffmpeg将音频和视频合成视频,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、视频格式转换功能
ffmpeg视频转换功能。视频格式转换,比如可以将多种视频格式转换为flv格式,可不是视频信号转换 。
ffmpeg可以轻易地实现多种视频格式之间的相互转换(wma,rm,avi,mod等),例如可以将摄录下的视频avi等转成现在视频网站所采用的flv格式。
2、视频截图功能
对于选定的视频,截取指定时间的缩略图。视频抓图,获取静态图和动态图,不提倡抓gif文件;因为抓出的gif文件大而播放不流畅
3、给视频加水印功能
使用ffmpeg 视频添加水印(logo)。
好了,下面开始今天的正文。
借助第三方工具ffmpeg合成视频
需求:在小破站上下载了一些视频,但是放到电脑里面看,我擦,声音文件和视频文件是分开的。
正确安装ffmpeg并配置好环境变量。
Java代码测试
里面是下载的视频和音频
我就上代码递归了,只要用正确的ffmpeg的命令和Java调用ffmpeg.exe的程序,就可以合成啦。
package com.lovely.test;
import java.io.BufferedReader;
import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
*
* 视频中获取音频文件
*
*/
public class TestFfmpeg {
// FFmpeg全路径
private static final String FFMPEG_PATH = "D:\\softWare\\tools\\joyTool\\ffmpeg\\bin\\ffmpeg.exe";
public static void main(String[] args) {
String path = "E:\\StudyVedio\\ComputerScience\\US";
try {
getAll(path);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 具体合成视频函数
* @param videoInputPath
* 原视频的全路径
*
* @param audioInputPath
* 音频的全路径
*
* @param videoOutPath
* 视频与音频结合之后的视频的路径
*/
public static void convetor(String videoInputPath, String audioInputPath, String videoOutPath)
throws Exception {
Process process = null;
InputStream errorStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader br = null;
try {
// ffmpeg命令
String command = FFMPEG_PATH + " -i " + videoInputPath + " -i " + audioInputPath
+ " -c:v copy -c:a aac -strict experimental " +
" -map 0:v:0 -map 1:a:0 "
+ " -y " + videoOutPath;
process = Runtime.getRuntime().exec(command);
errorStream = process.getErrorStream();
inputStreamReader = new InputStreamReader(errorStream);
br = new BufferedReader(inputStreamReader);
// 用来收集错误信息的
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
}
// 递归函数
public static void getAll(String path) throws Exception {
String videoInputPath = "";
String audioInputPath = "";
String videoOutPath = "";
File file = new File(path);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
getAll(f.getPath());
if (f.isFile()) {
if (f.getName().endsWith(".m4s")) {
if (f.getName().endsWith("audio.m4s"))
audioInputPath = file.getPath() + "\\audio.m4s";
if (f.getName().endsWith("video.m4s"))
videoInputPath = file.getPath() + "\\video.m4s";
videoOutPath = file.getPath() + "\\all.mp4";
if (!videoInputPath.equals(""))
convetor(videoInputPath, audioInputPath, videoOutPath);
}
}
}
}
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。