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

使用ffmpeg根据时间戳自动截取音频

流程

        1.将时间戳,源文件名称和地址,输出文件名称地址写入excel.

        2.读取excel文件并循环执行ffmpeg指令

excel表格内容

E:\aht\PIQBR2021Q3.mp30:34:130:33:23E:\aht\ddd\lowlight.mp3
E:\aht\PIQBR2021Q3.mp31:29:081:28:12E:\aht\ddd\look_forward_to.mp3
E:\aht\STQBR2023Q3.mp30:18:140:13:06E:\aht\ddd\customer.mp3

  代码

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.*;
import org.apache.poi.ss.usermodel.*;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;public class MP3Trimmer {public static void main(String[] args) throws IOException {getExcelData();
//        MP3Trimmer("E:\\aht\\EnglishPath\\ttt.mp3","0:00:00","0:00:07","E:\\aht\\EnglishPath\\0102.mp3");}public static void MP3Trimmer(String file, String startTime, String endTime, String outFile) {try {// 执行一个命令String command = "ffmpeg -i \""+file+"\" -ss "+startTime+" -to "+endTime+" -c copy \""+outFile+"\"";  // 你可以更改为任何 cmd 命令Process process = Runtime.getRuntime().exec(command);// 获取命令执行的输出InputStream inputStream = process.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}// 等待命令执行完成int exitCode = process.waitFor();System.out.println("命令执行完成,退出码:" + exitCode);} catch (IOException | InterruptedException e) {e.printStackTrace();}}public static void getExcelData() throws IOException {// 加载 Excel 文件FileInputStream file = new FileInputStream(new File("E:\\aht\\EnglishPath\\yyyy.xlsx"));// 创建工作簿对象Workbook workbook = new XSSFWorkbook(file);// 获取第一个工作表Sheet sheet = workbook.getSheetAt(0);// 读取前20行和前3列for (int rowNum = 0; rowNum < 300; rowNum++) {Row row = sheet.getRow(rowNum);String infile="";String startTime="";String endTime="";String outFile="";if (row != null) { // 检查当前行是否为 nullfor (int colNum = 0; colNum < 4; colNum++) {Cell cell = row.getCell(colNum);if (cell != null) {// 根据单元格类型读取值switch (cell.getCellType()) {case 1:if (colNum == 0) {infile=cell.getStringCellValue();}else if (colNum == 1) {startTime=cell.getStringCellValue();}else if (colNum == 2) {endTime=cell.getStringCellValue();}else if (colNum == 3) {outFile=cell.getStringCellValue();}System.out.print(cell.getStringCellValue() + "\t");break;case 0:if (DateUtil.isCellDateFormatted(cell)) {// 如果单元格是日期或时间,格式化输出SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");if (colNum == 0) {infile=sdf.format(cell.getDateCellValue());}else if (colNum == 1) {startTime=sdf.format(cell.getDateCellValue());}else if (colNum == 2) {endTime=sdf.format(cell.getDateCellValue());}else if (colNum == 3) {outFile=sdf.format(cell.getDateCellValue());}System.out.print(sdf.format(cell.getDateCellValue()) + "\t");} else {if (colNum == 0) {infile=cell.getStringCellValue();}else if (colNum == 1) {startTime=cell.getStringCellValue();}else if (colNum == 2) {endTime=cell.getStringCellValue();}else if (colNum == 3) {outFile=cell.getStringCellValue();}System.out.print(cell.getNumericCellValue() + "\t");}break;case 2:if (colNum == 0) {infile=cell.getStringCellValue();}else if (colNum == 1) {startTime=cell.getStringCellValue();}else if (colNum == 2) {endTime=cell.getStringCellValue();}else if (colNum == 3) {outFile=cell.getStringCellValue();}System.out.print(cell.getBooleanCellValue() + "\t");break;case 3:if (colNum == 0) {infile=cell.getStringCellValue();}else if (colNum == 1) {startTime=cell.getStringCellValue();}else if (colNum == 2) {endTime=cell.getStringCellValue();}else if (colNum == 3) {outFile=cell.getStringCellValue();}System.out.print(cell.getCellFormula() + "\t");break;case 4:System.out.print("BLANK" + "\t");break;case 5:System.out.print("ERROR" + "\t");break;}} else {System.out.print("EMPTY" + "\t");}}}MP3Trimmer(infile,startTime,endTime,outFile);System.out.println(); // 换行}// 关闭文件流file.close();workbook.close();}}

http://www.lqws.cn/news/501805.html

相关文章:

  • 集成 Odoo、n8n 与 Dify,实现智能业务流程自动化
  • 《AI大模型应用技术开发工程师》学习总结
  • 工作流会使用到Webhook是什么
  • ArkTS与仓颉开发语言:鸿蒙编程的双子星
  • MySQL深分页性能瓶颈:问题分析与解决方案
  • Leetcode 3588. Find Maximum Area of a Triangle
  • 做上门私厨/上门做饭App小程序,到底是定制开发,还是选成品系统?
  • 开源跨平台的轻量 C# 编辑器
  • 软件设计模式_期末复习
  • 时序数据库IoTDB可实现的基本操作及命令汇总
  • Idea新UI
  • C++ 运行、编译和链接基础内容
  • gRPC在Windows DLL构建中的问题与现状
  • 云电脑,“死”于AI时代前夕 | 数智化观察
  • 麒麟V10操作系统离线安装Docker、Docker compose和1Panel
  • docker部署nginx
  • .NET 生态中主流的前后端生产级框架
  • 鸿蒙应用开发中的状态管理:深入解析AppStorage与LocalStorage
  • Hadoop RPC 分层设计的哲学:高内聚、低耦合的最佳实践
  • STM32[笔记]--4.嵌入式硬件基础
  • 华为云Flexus+DeepSeek征文 | 华为云MaaS平台上的智能客服Agent开发:多渠道融合应用案例
  • 多模态+类人认知:Embodied AI迈向AGI的三大瓶颈与突破路径
  • Spring Ai Alibaba Graph实现五大工作流模式
  • FPGA基础 -- Verilog 验证平台之 **cocotb 验证 `阶乘计算模块(factorial)` 的例子**
  • 【AI大模型】Spring AI 基于Redis实现对话持久存储详解
  • 报错:macOS 安装 sentencepiece
  • Sui 随全球加速采用,正式启用雅典 SuiHub 创新中心
  • 【动手学深度学习】4.7. 前向传播、反向传播和计算图
  • 【AI时代速通QT】第三节:Linux环境中安装QT并做测试调试
  • Unity反射机制