Example #1
0
 private void handleCombineVideos(
     HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
     throws IOException, NoSuchAlgorithmException, InvalidFileSizeException, ServiceException,
         InterruptedException {
   int projectId = getProjectIdFromSessionAttributes(session);
   String[] videos = DatabaseApi.getVideosOnTimeline(projectId);
   String format = request.getParameter("output-extension");
   log.info("Combining Videos, if necessary");
   if (videos.length == 0) {
     response.sendRedirect("editor.jsp");
   } else {
     String[] videoURLs = new String[videos.length];
     for (int i = 0; i < videos.length; i++) {
       videoURLs[i] = DatabaseApi.getVideoUrl(DatabaseApi.getVideoId(videos[i], projectId));
     }
     String combinedURL = "";
     if (videoURLs.length == 1) {
       combinedURL = S3Api.downloadVideosToTemp(videoURLs[0]);
     } else if (videoURLs.length > 1) {
       combinedURL = Combiner.combineVideos(videoURLs, videos, tempDir);
     }
     if (combinedURL == null) {
       response.sendRedirect("editor.jsp");
       return;
     }
     log.info("Now converting, if necessary");
     File file = new File(combinedURL);
     String ext = ".ogv";
     String converted = "";
     if (!format.equals("ogv")) {
       if (format.equals("avi")) {
         ext = ".avi";
       } else if (format.equals("wmv")) {
         ext = ".wmv";
       } else if (format.equals("mp4")) {
         ext = ".mp4";
       }
       converted = Combiner.convertTo(format, file, tempDir);
       file = new File(tempDir + "/" + converted);
     }
     log.info("Now Dowloading");
     final ServletOutputStream out = response.getOutputStream();
     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition", "attachment;filename=combinedVideo" + ext);
     BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
     byte[] buf = new byte[4 * 1024]; // 4K buffer
     int bytesRead;
     while ((bytesRead = is.read(buf)) != -1) {
       out.write(buf, 0, bytesRead);
     }
     is.close();
     out.flush();
     out.close();
     file.delete();
     log.info("Downloaded. End of handleCombinedVideos");
   }
 }