FFmpeg(5) -- 相关工具类

这篇博客主要介绍了在FFmpeg中使用的一些自定义工具类,包括关键的对象和方法,虽然具体内容未详述,但方法名和对象名能提供清晰的理解。
摘要由CSDN通过智能技术生成

关于工具类中用到的一些自定义对象和方法就不贴出来了,相信大家看方法名或者对象名就能明白。

public class FFmpegUtils {
	private static String ffmpegEXE = "ffmpeg";
	
	static {
		String expression = ConfigUtils.getType("ffmpeg.expression");
		ffmpegEXE = StringUtils.isBlank(expression) ? "ffmpeg" : expression;
	}

	public static boolean screenCut(FFmpegParameter record, String outputPath) throws Exception {
		Process process = null;
		try {
			List<String> commands = new java.util.ArrayList<String>();
			commands.add(ffmpegEXE);
			commands.add("-y");
			commands.add("-ss");
			commands.add(record.getCutTime());
			commands.add("-i");
			commands.add(record.getFilePath());
			commands.add("-f");
			commands.add("image2");
			commands.add(outputPath);
			process = new ProcessBuilder(commands).redirectErrorStream(true).start();
			new PrintStreamUtils(process.getErrorStream()).start();
			new PrintStreamUtils(process.getInputStream()).start();
			process.waitFor();
			return validateInvokingFFmpeg(outputPath);
		} finally {
			if (null != process) {
				process.destroy();
			}
		}
	}
	
	public static boolean processMedia(FFmpegParameter record, String targetPath) throws Exception {
		Process process = null;
		try {
			if (checkVideo(record.getTargetType(), record.getTargetEncoding(), record.getBitrate(), record.getFramerate(), record.getFilePath())) {
				// 视频本身符合要求
				FileUtils.copyFile(new File(record.getFilePath()), new File(targetPath));
				return true;
			}
			List<String> commands = new ArrayList<String>();
			commands.add(ffmpegEXE);
			commands.add("-y");
			commands.add("-i");
			commands.add(record.getFilePath());
			if (StringUtils.isNotBlank(record.getBitrate())) {
				commands.add("-b");
				commands.add(record.getBitrate() + "k");
			}
			if (StringUtils.isNotBlank(record.getTargetEncoding())) {
				commands.add("-c:v");
				commands.add(record.getTargetEncoding());
			}
			if (true == record.isAudio()) {
				commands.add("-c:a");
				commands.add("aac");
			} else {
				commands.add("-an");
			}
			if (EncodingConstant.GIF.equals(record.getSourceType())) {
				String[] resolution = correctResolution(record.getFilePath());
				commands.add("-s");
				commands.add(resolution[0] + "x" + resolution[1]);
			}
			if (StringUtils.isNotBlank(record.getFramerate())) {
				commands.add("-r");
				commands.add(record.getFramerate());
			}
			commands.add(targetPath);
			process = new ProcessBuilder(commands).redirectErrorStream(true).start();
			new PrintStreamUtils(process.getErrorStream()).start();
			new PrintStreamUtils(process.getInputStream()).start();
			process.waitFor();
			return validateInvokingFFmpeg(targetPath);
		} finally {
			if (null != process) {
				process.destroy();
			}
		}
	}

	public static String getDuration(String filePath) throws Exception {
		Long time = null;
		String info = getMediaInfo(filePath);
		 //从视频信息中解析时长
        String regexDuration = "Duration: (.*?), start";
        Pattern pattern = Pattern.compile(regexDuration);
        Matcher m = pattern.matcher(info);
        if (m.find()) {
        	time = getTimelen(m.group(1));
        }
        return String.valueOf(time * 1000);
	}
	
	public static String getEncoding(String filePath) throws Exception {
		String info = getMediaInfo(filePath);
		String regexVideo = "Video: (.*?), (.*?), (.*?), (.*?), (.*?), (.*?)[,\\s]";
		Pattern pattern = Pattern.compile(regexVideo);
		Matcher m = pattern.matcher(info);
		String encoding = null;
		if (m.find()) {
			//编码格式:m.group(1),视频格式:m.group(2),分辨率:m.group(3),(码率:m.group(4),fps:m.group(5)||码率:m.group(5),fps:m.group(6))
			encoding = m.group(1).toLowerCase().replace(" ", "");
		}
		return encoding;
	}

	public static String getResolution(String filePath) throws Exception {
		String resolution = null;
		String info = getMediaInfo(filePath);
		String regexVideo = "Video: (.*?), (.*?), (.*?), (.*?), (.*?), (.*?)[,\\s]";
		Pattern pattern = Pattern.compile(regexVideo);
		Matcher m = pattern.matcher(info);
        if (m.find()) {
        	//编码格式:m.group(1),视频格式:m.group(2),分辨率:m.group(3),(码率:m.group(4),fps:m.group(5)||码率:m.group(5),fps:m.group(6))
        	if (m.group(3).toLowerCase().contains("x")) {
        		resolution = m.group(3);
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值