java使用ffmpeg拆分和拼接音频

ffmpeg是一个开源的跨平台音视频处理工具,它可以对音视频进行格式转换、压缩、采集、裁剪、剪辑等操作。ffmpeg能够处理的音视频格式很多,包括常见的mp4、avi、mov等格式,也包括一些不那么常见的格式。

ffmpeg最初是由法国程序员Fabrice Bellard编写的,现在已经成为了流行的音视频处理工具。ffmpeg支持命令行操作,也可以通过API集成到其他应用程序中使用。因为经过多年的发展,ffmpeg的性能和稳定性都非常高,被广泛应用于音视频处理领域,例如视频编辑软件、转码工具、流媒体服务器等。

 按照指定时间对音频进行拆分

 public static void split(String firstFragmentPath,
                             String targetPath, String startTime, String continuousTime) {

        try {
            String command="ffmpeg -i "+firstFragmentPath+" -ss "+startTime+" -t "+continuousTime+" "+targetPath+" -y";
            Runtime.getRuntime().exec(new String[]{"sh", "-c", command});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

音频拼接

public static Boolean concat(String concatFilePath,String destFile){
        try {
            final String command = String.format("ffmpeg -f concat -i %s -c copy %s -y", concatFilePath, destFile);
            Runtime.getRuntime().exec(new String[]{"sh", "-c", command});
            return Boolean.TRUE;
        } catch (IOException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

第一个参数需要传递指定格式的文件(concat.txt)

file 'tmp0.wav'
file 'tmp1.wav'
file 'tmp2.wav'
file 'tmp3.wav'
file 'tmp4.wav'
file 'tmp5.wav'
file 'tmp6.wav'
file 'tmp7.wav'
file 'tmp8.wav'

你可能感兴趣的:(java,语音处理,java,ffmpeg,音视频,开发语言)