JavaCV之FFmpegFrameFilter视频转灰度

server/2024/12/14 15:10:40/

1、代码

java">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>


http://www.ppmy.cn/server/150121.html

相关文章

51c大模型~合集89

我自己的原文哦~ https://blog.51cto.com/whaosoft/12815167 #OpenAI很会营销 而号称超强AI营销的灵感岛实测成效如何&#xff1f; OpenAI 是懂营销的&#xff0c;连续 12 天发布&#xff0c;每天一个新花样&#xff0c;如今刚过一半&#xff0c;热度依旧不减。 毫无疑问&…

【Spring】日志类Logger的使用

在Spring框架中&#xff0c;日志记录是一个重要的组成部分&#xff0c;通常使用不同的日志框架来处理应用程序的日志。Spring 本身并直接提供一个名为Logger 的类&#xff0c;而是通过抽象的日志 API 让开发者能够选择和使用不同的日志实现&#xff08;如 Log4j、Logback、SLF4…

力扣1049.最后一块石头的重量(01背包)之理解篇

1049. 最后一块石头的重量 II class Solution { public:int lastStoneWeightII(vector<int>& stones) {int sumNum 0;for(int i 0;i < stones.size();i){sumNum stones[i];}int target sumNum / 2;vector<int>dp(target 1, 0);for(int i 0;i < st…

后端接受前端传递数组进行批量删除

问题描述&#xff1a;当我们需要做批量删除功能的时候&#xff0c;我们循环单次删除的接口也能进行批量删除&#xff0c;但要删除100条数据就要调用100次接口&#xff0c;或者执行100次sql&#xff0c;这样系统开销是比较大的&#xff0c;那么我们直接采用接收的数组格式数据sq…

springboot423玩具租赁系统boot(论文+源码)_kaic

摘 要 传统办法管理信息首先需要花费的时间比较多&#xff0c;其次数据出错率比较高&#xff0c;而且对错误的数据进行更改也比较困难&#xff0c;最后&#xff0c;检索数据费事费力。因此&#xff0c;在计算机上安装玩具租赁系统软件来发挥其高效地信息处理的作用&#xff0c…

OpenAI 正式赋予 ChatGPT 通过视频实时与用户互动的能力

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Alan Chhabra:MongoDB AI应用程序计划(MAAP) 为客户提供价值

MongoDB全球合作伙伴执行副总裁 Alan Chhabra 每当有人向我问询MongoDB&#xff0c;我都会说他们很可能在不觉之间已经与MongoDB有过交集。事实上&#xff0c;包括70%财富百强在内的许多世界领先企业公司都在使用MongoDB。我们在MongoDB所做的一切都是为了服务客户&#xff0c…

Python实现中国象棋

探索中国象棋 Python 代码实现&#xff1a;从规则逻辑到游戏呈现 中国象棋&#xff0c;这款源远流长的棋类游戏&#xff0c;承载着深厚的文化底蕴与策略智慧。如今&#xff0c;借助 Python 与 Pygame 库&#xff0c;我们能够在数字世界中复刻其魅力&#xff0c;深入探究代码背后…