JavaCV之FFmpegFrameFilter视频转灰度

1、代码

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


import lombok.SneakyThrows;
import org.bytedeco.javacv.*;

public class FFmpegFrameFilterVideoExample {
    @SneakyThrows
    public static void main(String[] args) {

        // 输入视频文件路径
        String inputVideoPath = "f:/2222.mp4";
        // 输出视频文件路径
        String outputVideoPath = "f:/2222output_video.mp4";

        // 抓取器,用于读取输入视频
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputVideoPath);
        grabber.start();

        // 创建帧过滤器,这里使用一个简单的视频滤镜(例如:将视频转为灰度)
//        传入滤镜参数(这里是format = gray,用于将视频转换为灰度)
//        、视频宽度和高度。同时设置像素格式为AVPixelFormat.GRAY8和帧率为抓取器获取的帧率,并启动帧过滤器。

        FFmpegFrameFilter frameFilter = new FFmpegFrameFilter("format=gray", grabber.getImageWidth(),
                grabber.getImageHeight());
        frameFilter.setFrameRate(grabber.getFrameRate());
        frameFilter.start();

        // 用于写入输出视频的帧记录器
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputVideoPath, grabber.getImageWidth(),
                grabber.getImageHeight());
        recorder.setVideoCodec(grabber.getVideoCodec());
        recorder.setFormat("mp4");
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setAudioChannels(grabber.getAudioChannels());
        recorder.setAudioCodec(grabber.getAudioCodec());
        recorder.start();

        Frame frame;
        while ((frame = grabber.grab()) != null) {
            //从抓取器中抓取帧,将其推送(push)到帧过滤器中,然后通过pull方法获取经过滤镜处理后的帧
            // 。如果设置了帧记录器(在代码中部分注释掉了),可以将处理后的帧记录到输出视频文件中。
            frameFilter.push(frame);
            Frame filteredFrame = frameFilter.pull();
            recorder.record(filteredFrame);
        }

        frameFilter.stop();
        grabber.stop();
        recorder.stop();
        frameFilter.release();
        grabber.release();
        recorder.release();

        System.out.println("视频滤镜处理完成。");

    }
}

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>
相关推荐
sky丶Mamba9 分钟前
Java虚拟机启动时默认携带参数(jdk8)
java·jvm
五味香22 分钟前
Java学习,字符串搜索
java·c语言·开发语言·python·学习·golang·kotlin
weixin_11223327 分钟前
基于Java图书借阅系统的设计与实现
java·开发语言
江上清风山间明月40 分钟前
flutter编译e: Daemon compilation failed: null java.lang.Exception错误解决
java·flutter·exception·daemon·compilation
大G哥41 分钟前
Flutter如何调用java接口如何导入java包
java·开发语言·flutter
helloworld工程师1 小时前
Dubbo的负载均衡及高性能RPC调用
java·大数据·人工智能
南宫生1 小时前
力扣-图论-9【算法学习day.59】
java·学习·算法·leetcode·图论
confident31 小时前
hibernate 配置 二级 缓存
java·缓存·hibernate
CopyProfessor1 小时前
Java版-图论-最短路-Floyd算法
java·算法·图论
-觅-1 小时前
‘Close Project‘ is not available while IDEA is updating indexes的解决
java·ide·intellij-idea