Java通过调用ffmpeg实现音频视频做转码、切割、抽帧处理

最近由于工作需要,需要对音频视频做转码、切割、抽帧处理,特写此博客,以作备忘。

大致步骤如下:

1、作为音视频处理工具、ffmpeg很强大
2、首先要在linux或者windows系统上安装ffmpeg(百度一下安装步骤)
3、按照我写的工具类,设置好安装路径即可使用

下面分享部分代码,大致如下

一、音频文件转wav格式

   /**
     * 音频文件转wav格式
     * @param ffmpegPath
     * @param sourcePath
     * @param targetPath
     * @return
     * @throws Exception
     */
    public static File changeFileToWav(String ffmpegPath ,String sourcePath, String targetPath) throws Exception {
        String webroot = ffmpegPath;
        Runtime run = null;
        try {
            run = Runtime.getRuntime();
            long start = System.currentTimeMillis();
            System.out.println(new File(webroot).getAbsolutePath());
            //执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
            //wav转pcm
            //Process p=run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
            //mp3转pcm
            // Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -y -i " + sourcePath + " -acodec pcm_s16le -f s16le -ac 1 -ar 16000 " + targetPath);
            //16bit 单声道 48khz
            Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -y -i " + sourcePath + " -ar 48000 -ac 1 -acodec pcm_s16le " + targetPath);
            InputStream stderr = p.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;

            while ((line = br.readLine()) != null)
                System.out.println(line);

            int exitVal = p.waitFor();
            System.out.println("Process exitValue: " + exitVal);

            //释放进程
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
            p.waitFor();
            long end = System.currentTimeMillis();
            System.out.println(sourcePath + " convert success, costs:" + (end - start) + "ms");
            File targetFile = new File(targetPath);
            return targetFile;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //run调用lame解码器最后释放内存
            run.freeMemory();
        }
        return null;
    }

二、音频切割

    /**
     * 音频切割
     * @param ffmpegPath
     * @param sourcePath
     * @param targetPath
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static File wavToCutting(String ffmpegPath ,String sourcePath, String targetPath,String startTime,String endTime) throws Exception {
        String webroot = ffmpegPath;
        Runtime run = null;
        try {
            run = Runtime.getRuntime();
            long start = System.currentTimeMillis();
            System.out.println(new File(webroot).getAbsolutePath());
            //执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
            //wav转pcm
            //Process p=run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
            //mp3转pcm
            // Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -y -i " + sourcePath + " -acodec pcm_s16le -f s16le -ac 1 -ar 16000 " + targetPath);
            //16bit 单声道 48khz
            System.err.print(new File(webroot).getAbsolutePath() + "/ffmpeg -i " + sourcePath + " -ss "+startTime+" -to "+endTime +" -y "+  targetPath);
            Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -i " + sourcePath + " -ss "+startTime+" -to "+endTime +" -y "+  targetPath);
            InputStream stderr = p.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;

            while ((line = br.readLine()) != null)
                System.out.println(line);

            int exitVal = p.waitFor();
            System.out.println("Process exitValue: " + exitVal);

            //释放进程
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
            p.waitFor();
            long end = System.currentTimeMillis();
            System.out.println(sourcePath + " convert success, costs:" + (end - start) + "ms");
            File targetFile = new File(targetPath);
            return targetFile;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //run调用lame解码器最后释放内存
            run.freeMemory();
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/105826791