123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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<String> commands = new java.util.ArrayList<String>();
- 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<String> commands = new java.util.ArrayList<String>();
- 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;
- }
- }
|