使用ffmpeg根据时间戳自动截取音频
流程
1.将时间戳,源文件名称和地址,输出文件名称地址写入excel.
2.读取excel文件并循环执行ffmpeg指令
excel表格内容
E:\aht\PIQBR2021Q3.mp3 | 0:34:13 | 0:33:23 | E:\aht\ddd\lowlight.mp3 |
E:\aht\PIQBR2021Q3.mp3 | 1:29:08 | 1:28:12 | E:\aht\ddd\look_forward_to.mp3 |
E:\aht\STQBR2023Q3.mp3 | 0:18:14 | 0:13:06 | E:\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();}}