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

FFmpeg音视频处理解决方案

核心组件:

ffmpeg:主要的命令行工具,用于转码、转换格式等

ffprobe:用于分析多媒体文件信息的工具

ffplay:简单的媒体播放器

主要功能:

✅ 格式转换(转码)

✅ 视频裁剪、合并

✅ 调整分辨率、比特率

✅ 提取音频/视频

✅ 截图/生成缩略图

✅ 添加水印、字幕

✅ 流媒体处理

一、安装FFmpeg

Windows:

  1. 访问 https://ffmpeg.org/download.html
  2. 下载Windows版本,解压到指定目录
  3. 将bin目录添加到系统PATH环境变量

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install ffmpeg

macOS:

brew install ffmpeg

二、项目配置

在application.properties或application.yml中配置FFmpeg路径:

# application.properties
ffmpeg.path=/usr/bin/ffmpeg  # Linux/Mac
# 或
ffmpeg.path=C:\\ffmpeg\\bin\\ffmpeg.exe  # Windows

三、依赖注入与异步处理

@Service
@Slf4j
public class VideoTranscodingService {@Value("${ffmpeg.path}")private String ffmpegPath; // 注入FFmpeg路径@Async // 异步执行,避免阻塞请求public void transcodeVideo(Long materialId, String inputPath) {// 转码逻辑}
}

核心转码方法

  • ProcessBuilder
  • Process
private void transcodeToResolution(String inputPath, String outputPath, String resolution) throws Exception {List<String> command = new ArrayList<>();command.add(ffmpegPath);command.add("-i");command.add(inputPath);    // 输入文件command.add("-s");command.add(resolution);   // 目标分辨率command.add("-c:v");command.add("libx264");    // 视频编码器command.add("-crf");command.add("23");         // 视频质量command.add("-c:a");command.add("aac");        // 音频编码器command.add("-b:a");command.add("128k");       // 音频比特率command.add(outputPath);   // 输出文件ProcessBuilder builder = new ProcessBuilder(command);Process process = builder.start();int exitCode = process.waitFor(); // 等待转码完成if (exitCode != 0) {throw new RuntimeException("FFmpeg转码失败,退出码: " + exitCode);}
}

四、控制器中上传文件视频

@RestController
@RequestMapping("/api/video")
public class VideoController {@Autowiredprivate VideoTranscodingService transcodingService;@PostMapping("/upload")public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file, @RequestParam Long materialId) {try {// 1. 保存上传的文件String uploadDir = "uploads/";String originalFilename = file.getOriginalFilename();String filePath = uploadDir + UUID.randomUUID() + "_" + originalFilename;File dest = new File(filePath);file.transferTo(dest);// 2. 异步启动转码transcodingService.transcodeVideo(materialId, filePath);return ResponseEntity.ok("视频上传成功,转码中...");} catch (Exception e) {return ResponseEntity.status(500).body("上传失败: " + e.getMessage());}}
}
@GetMapping("/status/{materialId}")
public ResponseEntity<Map<String, Object>> getTranscodingStatus(@PathVariable Long materialId) {CourseMaterial material = materialRepository.findById(materialId).orElse(null);if (material == null) {return ResponseEntity.notFound().build();}Map<String, Object> response = new HashMap<>();response.put("status", material.getTranscodingStatus());response.put("filePath", material.getFilePath());response.put("duration", material.getDuration());return ResponseEntity.ok(response);
}
http://www.dtcms.com/a/359052.html

相关文章:

  • 互联网大厂面试:大模型应用开发岗位核心技术点解析
  • CSS基础学习第二天
  • 算法之x数之和
  • nginx配置websock请求,wss
  • GooglePlay提审问题记录
  • 生成式BI工具(WrenAI)
  • 防抖与节流的区别及实现【JS核心】
  • 恶补DSP:3.F28335的ePWM模块
  • 语义分割目前还是研究热点吗?
  • 【CF】Day136——Codeforces Round 1046 (Div. 2) CD (动态规划 | 数学)
  • 血氧检测原理与算法
  • Linux系统直接查询文件或目录绝对路径的方式
  • TensorFlow 深度学习 | 使用底层 API 实现模型训练(附可视化与 MLP)
  • HyperPlonk 的硬件友好性
  • Linux kernel 多核启动
  • LINUX-网络编程-TCP-UDP
  • Python 入门 Swin Transformer-T:原理、作用与代码实践
  • AI + 行业渗透率报告:医疗诊断、工业质检领域已进入规模化落地阶段
  • 通过数据蒸馏打破语音情感识别的资源壁垒
  • 基于单片机音乐喷泉/音乐流水灯/音乐播放器设计
  • 移动零,leetCode热题100,C++实现
  • SpringCloud Alibaba Sentinel 流量治理、熔断限流(四)
  • 【源码】智慧工地系统:智能化施工现场的全新管理方案
  • 第十七章 ESP32S3 SW_PWM 实验
  • 深入解析Nginx常见模块2
  • web渗透之RCE漏洞
  • 针对 “TCP 会话维持与身份验证” 的攻击
  • (二)设计模式(Command)
  • SQL Server 临时表合并与数量汇总的实现方法
  • 大模型不听话?试试提示词微调