1、代码
package com.example.demo.ffpemg;import lombok.SneakyThrows;
import org.bytedeco.javacv.*;public class FFmpegFrameFilterVideoExample {@SneakyThrowspublic 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 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>