|
在上传完成后执行mp4的元数据转换,ffmpeg出现CMD进程卡死情况,请问是怎么回事?
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.UUID;
- import java.nio.channels.FileChannel;
- import java.io.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.justep.biz.client.Action;
- import com.justep.biz.client.ActionEngine;
- import com.justep.biz.client.ActionResult;
- import com.justep.biz.client.ActionUtils;
- import com.justep.ui.util.NetUtils;
- //@SuppressWarnings("serial")
- public class FileUploadAction extends com.justep.ui.impl.JProcessorImpl
- {
- //private static final long serialVersionUID = 1L;
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request,response);
- }
-
- @SuppressWarnings("resource")
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- //读取配置路径
- String readPath = "D:/KMS1.1/TEST";
- String savePath = "D:/KMS1.1/KMS_Library";
- String folad = "uploads";
- readPath = readPath +File.separator + folad;
- response.setContentType("text/html;charset=utf-8");
- String action = request.getParameter("action");
- try
- {
- if(action.equals("mergeChunks"))
- {
- //合并文件
- //需要合并的文件的目录标记
- String fileMd5 = request.getParameter("fileMd5");
- String head_id = request.getParameter("head_id");
- String target_id = request.getParameter("target_id");
- String quarter = request.getParameter("quarter");
- String ext = request.getParameter("ext");
- String doc_name = request.getParameter("doc_name");
- String doc_size = request.getParameter("doc_size");
- String userid = request.getParameter("userid");
- String username = request.getParameter("username");
-
- //读取目录里的所有文件
- readPath = readPath + File.separator + fileMd5;
- System.out.println(readPath);
- File f = new File(readPath);
- File[] fileArray = f.listFiles(new FileFilter(){
- //排除目录只要文件
- public boolean accept(File pathname) {
- // TODO Auto-generated method stub
- if(pathname.isDirectory()){
- return false;
- }
- return true;
- }
- });
- //转成集合,便于排序
- List<File> fileList = new ArrayList<File>(Arrays.asList(fileArray));
- Collections.sort(fileList,new Comparator<File>() {
- public int compare(File o1, File o2) {
- // TODO Auto-generated method stub
- if(Integer.parseInt(o1.getName()) < Integer.parseInt(o2.getName())){
- return -1;
- }
- return 1;
- }
- });
-
- //UUID.randomUUID().toString()-->随机名
- UUID uuid = UUID.randomUUID();
- String doc_id = uuid.toString().toUpperCase().replaceAll("-", "");
- //创建文件夹
- savePath = savePath + File.separator +head_id +File.separator + quarter;
- File folder = new File(savePath);
-
- if(!folder.exists()){
- folder.mkdirs();
- }
- //文件路径
- final String filePath = savePath + File.separator + doc_id+"."+ext;
- //factory.setRepository(file);
- System.out.println(filePath);
- File outputFile = new File(filePath);
- //创建文件
- outputFile.createNewFile();
- //输出流
- FileChannel outChnnel = new FileOutputStream(outputFile).getChannel();
- //合并
- FileChannel inChannel;
- for(File file : fileList){
- inChannel = new FileInputStream(file).getChannel();
- inChannel.transferTo(0, inChannel.size(), outChnnel);
- inChannel.close();
- //删除分片
- file.delete();
- }
- outChnnel.close();
-
- //清除上传的缓存文件夹
- File tempFile = new File(readPath);
- if(tempFile.isDirectory() && tempFile.exists()){
- tempFile.delete();
- }
-
-
- processMP4(filePath);
-
- //插入数据库的明细记录
- String bSessionID = NetUtils.getBSessionID(request);
- String language = NetUtils.getLanguage(request);
- String PROCESS = "/KMS/Library/process/Library/libraryProcess";
- String ACTIVITY = "mainActivity";
- String ACTION = "uploadFileAction";
-
- Action action_self = new Action();
- action_self.setProcess(PROCESS);
- action_self.setActivity(ACTIVITY);
- action_self.setName(ACTION);
- action_self.setParameter("doc_id", doc_id);
- action_self.setParameter("doc_name", doc_name);
- action_self.setParameter("doc_size", doc_size);
- action_self.setParameter("target_id", target_id);
- action_self.setParameter("userid", userid);
-
- action_self.setParameter("username", username);
- action_self.setParameter("quarter", quarter);
- // 调用动作
- ActionResult result = ActionEngine.invokeAction(action_self, ActionUtils.JSON_CONTENT_TYPE, bSessionID, language, null);
-
- response.getWriter().write("{"ifExist":1}");
- //init_MP4(filePath);
-
- }
- else if(action.equals("checkChunk"))
- {
- //检查当前分块是否上传成功
- String fileMd5 = request.getParameter("fileMd5");
- String chunk = request.getParameter("chunk");
- String chunkSize = request.getParameter("chunkSize");
- File checkFile = new File(savePath+"/"+fileMd5+"/"+chunk);
- //检查文件是否存在,且大小是否一致
- if(checkFile.exists() && checkFile.length()==Integer.parseInt(chunkSize))
- {
- //上传过
- response.getWriter().write("{"ifExist":1}");
- }
- else
- {
- //没有上传过
- response.getWriter().write("{"ifExist":0}");
- }
- }
- }
- catch(Exception e)
- {
- response.getWriter().write("{"ifExist":-1}");
- e.printStackTrace();
- }
- }
-
- private void init_MP4(String oldfilepath) {
- List<String> commend = new ArrayList<String>();
- commend.add("D:\\ffmpeg\\bin\\qt-faststart");
- commend.add(oldfilepath);
- commend.add(oldfilepath);
- System.out.println(commend.toString());
- ProcessBuilder builder = new ProcessBuilder(commend);
- builder.command(commend);
- try {
- builder.start();
- } catch (IOException e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
-
- }
-
- private static void processMP4(String oldfilepath) {
- List<String> commend = new ArrayList<String>();
- commend.add("D:\\ffmpeg\\bin\\ffmpeg");
- commend.add("-i");
- commend.add(oldfilepath);
- commend.add("-y");
- commend.add("-metadata");
- commend.add("title="h"");
- commend.add("-metadata");
- commend.add("comment="h"");
- commend.add("-c:v");
- commend.add("libx264");
- commend.add("-preset");
- commend.add("superfast");
- commend.add("-x264opts");
- commend.add("keyint=25");
- commend.add("-r");
- commend.add("15");
- commend.add("-b:v");
- commend.add("500k");
- commend.add("-c:a");
- commend.add("aac");
- commend.add("-strict");
- commend.add("experimental");
- commend.add("-ar");
- commend.add("44100");
- commend.add("-ac");
- commend.add("2");
- commend.add("-b:a");
- commend.add("64k");
- // commend.add("-vf");
- // commend.add(""movie=aa.png [watermark]; [in][watermark] overlay=10:10 [out]"");
- commend.add("-f");
- commend.add("mp4");
- commend.add("D:\\99999.mp4");
-
- System.out.println(commend.toString());
- try
- {
- ProcessBuilder builder = new ProcessBuilder(commend);
- builder.command(commend);
- builder.start();
- System.out.println("1232134");
- } catch (IOException e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- }
- }
复制代码
|
|