package com.ssj.framework.weixin.util;

import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.idworker.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Date;
import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2018年5月3日 上午9:58:27 
 * 类说明 
 */
public class ExecLinuxCMD {
	 private static Logger logger = LoggerFactory.getLogger(ExecLinuxCMD.class);
	 public static Object exec(String cmd) {  
         try {  
             String[] cmdA = { "/bin/sh", "-c", cmd };  
             Process process = Runtime.getRuntime().exec(cmdA);  
             LineNumberReader br = new LineNumberReader(new InputStreamReader(  
                     process.getInputStream()));  
             StringBuffer sb = new StringBuffer();  
             String line;  
          while ((line = br.readLine()) != null) {  
                 //System.out.println(line);  
                 logger.info(line);
                 sb.append(line).append("\n");  
             }  
             return sb.toString();  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         return null;  
     } 
	 
	 public static String mergeToMp3(List<String> inputPath){
		 String file_path = (String) SystemResourceLocator.getValue("file_save_path");
			String fPath = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
			File  f = new File(file_path+fPath);
			if (!f.exists()) {
				f.mkdirs();
			}
			String outPath=fPath+"/"+ IdWorker.generateId() + ".mp3";
			StringBuffer sb =new StringBuffer();
			sb.append("/usr/local/ffmpeg/ffmpeg ");
			for (String string : inputPath) {
				sb.append(" -i ");  
				sb.append(file_path+string); 
			}
			sb.append(" -filter_complex "); 
			sb.append("\'"); 
			for (int i=0;i<inputPath.size();i++) {
				String numindex="["+i+":0]";
				sb.append(numindex); 
			}
			sb.append(" concat=n="+inputPath.size()+":v=0:a=1 [a]");
			sb.append("\' "); 
			sb.append("-map ");
			sb.append("[a] ");
			sb.append(file_path+outPath);
			logger.info(sb.toString());
			exec(sb.toString());
			return outPath;
	 }
}
