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>
相关推荐
电子科技圈1 小时前
XMOS将在CES 2025上展出多款由边缘AI驱动的创新音效、音频、识别和处理解决方案
人工智能·科技·嵌入式硬件·mcu·物联网·音视频·iot
m0_748238422 小时前
【Vue3】前端使用 FFmpeg.wasm 完成用户视频录制,并对视频进行压缩处理
前端·ffmpeg·wasm
java156550579702 小时前
前端 mp4 视频改成 m3u8 流模式
前端·音视频
阿龍17872 小时前
个人ffmpeg笔记(一)
笔记·ffmpeg
yunmoon013 小时前
一个功能强大的视频翻译和本地化配音工具,支持影视级双语字幕/视频配音
音视频
Jack153027682793 小时前
D2761是为保护扬声器所设计的音频限幅器,其限幅值可通过外接电 阻来调节,适合在个人电脑、便携式音响等系统中作音频限幅用。
电脑·手机·笔记本电脑·音视频·音响
飞飞-躺着更舒服13 小时前
多媒体文件解复用(Demuxing)过程
计算机视觉·音视频
最 上 川13 小时前
学在西电录播课使用python下载,通过解析m3u8协议、多线程下载ts视频块以及ffmpeg合并
爬虫·python·ffmpeg·音视频
红米饭配南瓜汤16 小时前
Android显示系统(09)- SurfaceFlinger的使用
android·音视频·媒体