JavaCV视频文件音视频分离

1、

java 复制代码
package com.example.demo.ffpemg;

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FFmpegLogCallback;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FFmpegFrameGrabber;

public class AudioVideoSeparation {
    public static void main(String[] args) {
        String inputVideoPath = "f:/2222.MP4"; // 输入视频文件路径
        String outputAudioPath = "f:/output.aac"; // 输出音频文件路径
        String outputVideoPath = "f:/output.mp4"; // 输出视频文件路径
        FFmpegLogCallback.set();

        try {
            // 初始化视频抓取器
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputVideoPath);
            grabber.start();

            // 初始化音频录制器
            FFmpegFrameRecorder audioRecorder = new FFmpegFrameRecorder(outputAudioPath, grabber.getAudioChannels());
            audioRecorder.setFormat("aac");
            audioRecorder.setSampleRate(grabber.getSampleRate());
            audioRecorder.setAudioChannels(grabber.getAudioChannels());
            audioRecorder.start();

            // 初始化视频录制器
            FFmpegFrameRecorder videoRecorder = new FFmpegFrameRecorder(outputVideoPath, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
            videoRecorder.setFormat("mp4");
            videoRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            videoRecorder.setFrameRate(grabber.getFrameRate());
            videoRecorder.start();

            // 循环抓取视频帧并分离音频和视频
            Frame frame;
            while ((frame = grabber.grabFrame()) != null) {
                if (frame.image != null) { // 视频帧
                    videoRecorder.record(frame);
                }
                if (frame.samples != null) { // 音频采样
                    audioRecorder.record(frame);
                }
            }

            // 释放资源
            audioRecorder.stop();
            audioRecorder.release();
            videoRecorder.stop();
            videoRecorder.release();
            grabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、pom文件

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>demo1</description>
    <properties>
        <java.version>11</java.version>
        <javacpp.platform.macosx-x86_64>macosx-x86_64</javacpp.platform.macosx-x86_64>
        <javacpp.platform.linux-x86_64>linux-x86_64</javacpp.platform.linux-x86_64>
        <javacpp.platform.windows-x86_64>windows-x86_64</javacpp.platform.windows-x86_64>
        <javacv.version>1.5.11</javacv.version>
        <javacv.ffmpeg.version>7.1-1.5.11</javacv.ffmpeg.version>
        <javacv.opencv.version>4.10.0-1.5.11</javacv.opencv.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- mqtt -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <!--        <dependency>-->
        <!--            <groupId>com.baomidou</groupId>-->
        <!--            <artifactId>mybatis-plus-boot-starter</artifactId>-->
        <!--            <version>3.5.1</version>-->
        <!--        </dependency>-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>


        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.6</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>2.2.1</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.6.0</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dyvmsapi20170525</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.75.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.16.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.20.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.20.0</version>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>




        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.0.3</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.14.0</version>
        </dependency>


        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>api</artifactId>
            <version>0.29.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/ai.djl.mxnet/mxnet-engine -->
        <dependency>
            <groupId>ai.djl.mxnet</groupId>
            <artifactId>mxnet-engine</artifactId>
            <version>0.29.0</version>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/ai.djl.mxnet/mxnet-model-zoo -->
        <dependency>
            <groupId>ai.djl.mxnet</groupId>
            <artifactId>mxnet-model-zoo</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>basicdataset</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>model-zoo</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.timeseries</groupId>
            <artifactId>timeseries</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.huggingface</groupId>
            <artifactId>tokenizers</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>ai.djl.audio</groupId>
            <artifactId>audio</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.5.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>${javacv.version}</version>
        </dependency>
        <!--javacv 精简依赖  只依赖windows64位ffmpeg-->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>${javacv.version}</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg</artifactId>
            <version>${javacv.ffmpeg.version}</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/opencv-platform -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>opencv</artifactId>
            <version>4.10.0-1.5.11</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/tesseract -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>tesseract</artifactId>
            <version>5.5.0-1.5.11</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/openblas -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>openblas</artifactId>
            <version>0.3.28-1.5.11</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.bytedeco/leptonica -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>leptonica</artifactId>
            <version>1.85.0-1.5.11</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.bytedeco/flycapture -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>flycapture</artifactId>
            <version>2.13.3.31-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.bytedeco/libdc1394 -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>libdc1394</artifactId>
            <version>2.2.6-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/libfreenect -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>libfreenect</artifactId>
            <version>0.5.7-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.bytedeco/libfreenect2 -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>libfreenect2</artifactId>
            <version>0.2.0-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/librealsense -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>librealsense</artifactId>
            <version>1.12.4-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/librealsense2 -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>librealsense2</artifactId>
            <version>2.53.1-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/videoinput -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>videoinput</artifactId>
            <version>0.200-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bytedeco/artoolkitplus -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>artoolkitplus</artifactId>
            <version>2.3.1-1.5.9</version>
            <classifier>${javacpp.platform.windows-x86_64}</classifier>
        </dependency>



        <!--javacv 精简依赖  只依赖windows64位ffmpeg-->




    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
相关推荐
姓学名生3 小时前
李沐vscode配置+github管理+FFmpeg视频搬运+百度API添加翻译字幕
vscode·python·深度学习·ffmpeg·github·视频
学习嵌入式的小羊~4 小时前
RV1126+FFMPEG推流项目(11)编码音视频数据 + FFMPEG时间戳处理
ffmpeg·音视频
刘大猫.7 小时前
vue3使用音频audio标签
音视频·audio·preload·加载音频文件·vue3使用audio·vue3使用音频·audio标签
优联前端20 小时前
Web 音视频(二)在浏览器中解析视频
前端·javascript·音视频·优联前端·webav
我真不会起名字啊21 小时前
“深入浅出”系列之音视频开发:(3)音视频开发的学习路线和必备知识
音视频
是店小二呀21 小时前
【2024年CSDN平台总结:新生与成长之路】
数据库·人工智能·程序人生·aigc·音视频
无限大.1 天前
优化使用 Flask 构建视频转 GIF 工具
python·flask·音视频
音视频牛哥1 天前
RTMP|RTSP播放器只解码视频关键帧功能探讨
音视频·实时音视频·大牛直播sdk·rtsp播放器·rtmp播放器·rtsp player·rtmp player
普通网友1 天前
Android MediaPlayer音频播放器详解
android·音视频
少油少盐不要辣2 天前
js截取video视频某一帧为图片
javascript·音视频