当前位置: 首页 > news >正文

Spring Boot 3整合FFmpeg进行图片和MP3转换为视频

Spring Boot 3整合FFmpeg进行图片和MP3转换为视频的示例代码如下:

添加FFmpeg依赖到pom.xml:

<dependency><groupId>com.github.kokorin.jaffree</groupId><artifactId>jaffree</artifactId><version>0.1.2</version>
</dependency>

创建一个服务类来执行转换:

import com.github.kokorin.jaffree.ffmpeg.FFmpeg;
import com.github.kokorin.jaffree.ffmpeg.FFmpegResult;
import org.springframework.stereotype.Service;import java.nio.file.Paths;@Service
public class VideoConversionService {public void convertImageAndAudioToVideo(String imagePath, String audioPath, String outputPath) throws Exception {FFmpegResult result = FFmpeg.atPath().addInput(FFmpeg.input(imagePath)).addInput(FFmpeg.input(audioPath)).overrideOutputFiles(true).addOutput(FFmpeg.output(outputPath).videoCodec("copy").audioCodec("copy")).execute();if (result.getState() == FFmpegResult.State.SUCCESS) {System.out.println("转换成功");} else {System.out.println("转换失败");}}
}

在Spring Boot应用的主类或者任意一个配置类中,调用转换服务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class VideoConversionRunner implements CommandLineRunner {@Autowiredprivate VideoConversionService videoConversionService;@Overridepublic void run(String... args) throws Exception {String imagePath = "path/to/image.jpg";String audioPath = "path/to/audio.mp3";String outputPath = "path/to/output.mp4";videoConversionService.convertImageAndAudioToVideo(imagePath, audioPath, outputPath);}
}

确保你的图片和音频文件路径是正确的,并且FFmpeg可执行文件在你的系统PATH中或者通过FFmpeg.atPath()指定。

以上代码假设你已经有了一个运行中的Spring Boot 3应用,并且FFmpeg已经安装在你的系统上。如果FFmpeg没有安装,你需要先下载并安装FFmpeg,或者使用Docker容器等方式来运行它。


http://www.mrgr.cn/news/38417.html

相关文章:

  • 现代cpp多线程与并发初探
  • 进程的那些事--实现shell
  • 六级翻译 高分笔记
  • 遥感图像变换检测实践上手(TensorRT+UNet)
  • 电子秤PCBA方案应用解决方案设计
  • 系统分析师14:需求工程
  • 如何使用 PyInstaller 将 Python 项目打包成 .exe 文件
  • 南平自闭症寄宿制学校:让孩子自信绽放
  • 【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL66
  • leetcode老问题新发现(更新ing)
  • bytetrack 内存泄露问题
  • 产品管理 - 互联网产品(4): 交互设计
  • 低代码可视化-UniApp二维码可视化-代码生成器
  • Python编码系列—Python备忘录模式:掌握对象状态保存与恢复技术
  • 计241 作业2:C程序设计初步
  • Study-Oracle-10-ORALCE19C-RAC集群搭建(一)
  • Supervisor进程管理工具安装
  • MNIST手写数字数据集
  • 【深度学习】(10)--ResNet残差网络
  • 【C语言】__attribute__((packed))与#pragma pack