package com.qxgmat.help; import com.nuliji.tools.Tools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @Service public class VideoHelp { private static final Logger logger = LoggerFactory.getLogger(VideoHelp.class); @Value("${video.ffmpeg}") private String ffmpegPath; @Value("${upload.local_path}") private String localPath; @Value("${upload.web_url}") private String webUrl; @Value("${upload.hls_path}") private String hlsPath; @Value("${upload.hls_url}") private String hlsUrl; public String getHLS(String videoUrl){ return videoUrl.replace(webUrl, hlsUrl+"/"+"video.m3u8"); } public void generateHLS(String videoPath){ File file = new File(videoPath); String fileName = file.getName(); File dir = new File(hlsPath); if (!dir.exists()) { dir.mkdirs(); } File dest = new File( dir.getAbsolutePath() + File.separator+fileName+File.separator+"video.m3u8"); File dirs = new File(dest.getAbsolutePath()); if (!dirs.exists()){ dirs.mkdirs(); } List commands = new java.util.ArrayList(); commands.add(ffmpegPath); commands.add("-i"); commands.add(videoPath); commands.add("-c:v libx264 -c:a aac -strict -2 -f hls "+dest); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); final Process p = builder.start(); //从输入流中读取视频信息 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); logger.debug("videoPath: {}", videoPath); logger.debug("ffmpeg: {}", sb.toString()); } catch (Exception e) { e.printStackTrace(); } } public int getVideoTime(String videoPath) { List commands = new java.util.ArrayList(); commands.add(ffmpegPath); commands.add("-i"); commands.add(videoPath); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); final Process p = builder.start(); //从输入流中读取视频信息 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); logger.debug("videoPath: {}", videoPath); logger.debug("ffmpeg: {}", sb.toString()); //从视频信息中解析时长 String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s"; Pattern pattern = Pattern.compile(regexDuration); Matcher m = pattern.matcher(sb.toString()); if (m.find()) { int time = getTimelen(m.group(1)); return time; } } catch (Exception e) { e.printStackTrace(); } return 0; } private static int getTimelen(String timelen){ int min=0; String strs[] = timelen.split(":"); if (strs[0].compareTo("0") > 0) { min+=Integer.valueOf(strs[0])*60*60;//秒 } if(strs[1].compareTo("0")>0){ min+=Integer.valueOf(strs[1])*60; } if(strs[2].compareTo("0")>0){ min+=Math.round(Float.valueOf(strs[2])); } return min; } }