用javacv提取视频中的音频数据

用javacv将.avi格式的视频中的音频提取并保存成.mp3格式

pom中添加依赖

  <dependencies>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv-platform</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacpp</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco.javacpp-presets</groupId>
      <artifactId>opencv-platform</artifactId>
      <version>4.0.1-1.4.4</version>
    </dependency>
    <dependency>
      <groupId>org.bytedeco.javacpp-presets</groupId>
      <artifactId>ffmpeg-platform</artifactId>
      <version>4.1-1.4.4</version>
    </dependency>
  </dependencies>

代码:

package main;/**
 * @program: New_javacv
 * @description:
 * @author: Mr.Wang
 * @create: 2021-04-08 21:56
 **/

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;
import org.bytedeco.javacv.FrameRecorder.Exception;

    /**
     * 音频参数转换(包含采样率、编码,位数,通道数)
     *
     * @author eguid
     *
     */
    public class AudioConvert {
        /**
         * 通用音频格式参数转换
         *
         * @param inputFile
         *            -导入音频文件
         * @param outputFile
         *            -导出音频文件
         * @param audioCodec
         *            -音频编码
         * @param sampleRate
         *            -音频采样率
         * @param audioBitrate
         *            -音频比特率
         */
        public static void convert(String inputFile, String outputFile, int audioCodec, int sampleRate, int audioBitrate,
                                   int audioChannels) {
            Frame audioSamples = null;
            // 音频录制(输出地址,音频通道)
            FFmpegFrameRecorder recorder = null;
            //抓取器
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);

            // 开启抓取器
            if (start(grabber)) {
                recorder = new FFmpegFrameRecorder(outputFile, audioChannels);
                recorder.setAudioOption("crf", "0");
                recorder.setAudioCodec(audioCodec);
                recorder.setAudioBitrate(audioBitrate);
                recorder.setAudioChannels(audioChannels);
                recorder.setSampleRate(sampleRate);
                recorder.setAudioQuality(0);
                recorder.setAudioOption("aq", "10");
                // 开启录制器
                if (start(recorder)) {
                    try {
                        // 抓取音频
                        while ((audioSamples = grabber.grabSamples()) != null) {
                            recorder.setTimestamp(grabber.getTimestamp());
                            recorder.record(audioSamples);
                        }

                    } catch (org.bytedeco.javacv.FrameGrabber.Exception e1) {
                        System.err.println("抓取失败");
                    } catch (Exception e) {
                        System.err.println("录制失败");
                    }
                    stop(grabber);
                    stop(recorder);
                }
            }

        }

        public static boolean start(FrameGrabber grabber) {
            try {
                grabber.start();
                return true;
            } catch (org.bytedeco.javacv.FrameGrabber.Exception e2) {
                try {
                    System.err.println("首次打开抓取器失败,准备重启抓取器...");
                    grabber.restart();
                    return true;
                } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                    try {
                        System.err.println("重启抓取器失败,正在关闭抓取器...");
                        grabber.stop();
                    } catch (org.bytedeco.javacv.FrameGrabber.Exception e1) {
                        System.err.println("停止抓取器失败!");
                    }
                }

            }
            return false;
        }

        public static boolean start(FrameRecorder recorder) {
            try {
                recorder.start();
                return true;
            } catch (Exception e2) {
                try {
                    System.err.println("首次打开录制器失败!准备重启录制器...");
                    recorder.stop();
                    recorder.start();
                    return true;
                } catch (Exception e) {
                    try {
                        System.err.println("重启录制器失败!正在停止录制器...");
                        recorder.stop();
                    } catch (Exception e1) {
                        System.err.println("关闭录制器失败!");
                    }
                }
            }
            return false;
        }

        public static boolean stop(FrameGrabber grabber) {
            try {
                grabber.flush();
                grabber.stop();
                return true;
            } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                return false;
            } finally {
                try {
                    grabber.stop();
                } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                    System.err.println("关闭抓取器失败");
                }
            }
        }

        public static boolean stop(FrameRecorder recorder) {
            try {
                recorder.stop();
                recorder.release();
                return true;
            } catch (Exception e) {
                return false;
            } finally {
                try {
                    recorder.stop();
                } catch (Exception e) {

                }
            }
        }
        // 测试
        public static void main(String[] args) {
            //pcm参数转换

            convert("F:\\javacv_project\\color_cast.avi", "F:\\javacv_project\\color_cast1.mp3", avcodec.AV_CODEC_ID_MP3, 8000, 40,1);
        }
    }




你可能感兴趣的:(javacv,音视频分离)