/ * @param filePath 要处理的文件路径 * @return 分割后的文件路径 * @throws Exception 文件 */ List<String> cutVideo(String filePath) throws Exception { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(filePath + "文件不存在"); } if (!filePath.endsWith(".mp4")) { throw new Exception("文件格式错误"); } //从ffmpeg获得的时间长度00:00:00格式 String videoTimeString = getVideoTime(file); log.info("从ffmpeg获得的时间长度00:00:00格式:{}",videoTimeString); //将时长转换为秒数 int videoSecond = parseTimeToSecond(videoTimeString); log.info("将时长转换为秒数:{}",videoSecond); //视频文件的大小 long fileLength = getVideoFileLength(file); log.info("视频文件的大小:{}",fileLength); List<String> cutedVideoPaths = new ArrayList<String>(); if (fileLength <= blockSize) { log.info("如果视频文件大小不大于预设值,则直接返回原视频文件"); cutedVideoPaths.add(filePath); } else { log.info("超过预设大小,需要切割"); int partNum = (int) (fileLength / blockSize); log.info("文件大小除以分块大小的商:{}",partNum); long remainSize = fileLength % blockSize; log.info("余数:{}",remainSize); int cutNum; if (remainSize > 0) { cutNum = partNum + 1; } else { cutNum = partNum; } log.info("cutNum:{}",cutNum); int eachPartTime = videoSecond / cutNum; log.info("eachPartTime:{}",eachPartTime); String fileFolder = file.getParentFile().getAbsolutePath(); log.info("fileFolder:{}",fileFolder); String fileName[] = file.getName().split("\."); log.info("fileName[]:{}",fileName); for (int i = 0; i < cutNum; i++) { List<String> commands = Lists.newArrayList(); commands.add("ffmpeg"); commands.add("-ss"); commands.add(parseTimeToString(eachPartTime * i)); if (i != cutNum - 1) { commands.add("-t"); commands.add(parseTimeToString(eachPartTime)); } commands.add("-i"); commands.add(filePath); commands.add("-codec"); commands.add("copy"); commands.add(fileFolder + File.separator + fileName[0] + "_part" + i + "." + fileName[1]); cutedVideoPaths.add(fileFolder + File.separator + fileName[0] + "_part" + i + "." + fileName[1]); newRunCommand(commands); } } return cutedVideoPaths; }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/9749.html