001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Ricardo Dias 018 */ 019package org.nuxeo.ecm.platform.video.tools; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.io.File; 024import java.io.IOException; 025import java.nio.file.Files; 026import java.nio.file.StandardOpenOption; 027import java.util.ArrayList; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import org.apache.commons.io.FileUtils; 033import org.apache.commons.io.FilenameUtils; 034import org.nuxeo.ecm.core.api.Blob; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * The {@link VideoTool} that joins two or more video blobs. 041 * 042 * @since 8.4 043 */ 044public class VideoConcat extends VideoTool { 045 046 public final static String NAME = "concatTool"; 047 048 protected final static String VIDEOS_FILE_PATH_PARAM = "listFilePath"; 049 050 protected final static String VIDEO_CONCAT_COMMANDLINE = "videoConcat"; 051 052 public VideoConcat() { 053 super(VideoConcat.NAME, VIDEO_CONCAT_COMMANDLINE); 054 } 055 056 @Override 057 public Map<String, String> setupParameters(BlobHolder blobHolder, Map<String, Object> parameters) { 058 List<Blob> videos = blobHolder.getBlobs(); 059 List<File> sourceFiles = new ArrayList<>(); 060 File tempFile; 061 try { 062 //String list = ""; 063 List<String> lines = new ArrayList<>(); 064 for (Blob b : videos) { 065 File blobFile = b.getFile(); 066 sourceFiles.add(blobFile); 067 lines.add("file '" + blobFile.getAbsolutePath()); 068 } 069 tempFile = Framework.createTempFile("NxVTcv-", ".txt"); 070 Files.write(tempFile.toPath(), lines, UTF_8, StandardOpenOption.CREATE); 071 072 if (blobHolder.getBlobs().size() < 2) { 073 throw new NuxeoException("VideoConcat requires at least two videos to proceed."); 074 } 075 076 Blob firstVideo = blobHolder.getBlobs().get(0); 077 String outputFilename = firstVideo.getFilename(); 078 File outputFile = Framework.createTempFile(FilenameUtils.removeExtension(outputFilename), 079 "-concat." + FilenameUtils.getExtension(outputFilename)); 080 081 // Run the command line 082 Map<String, String> params = new HashMap<>(); 083 params.put(VIDEOS_FILE_PATH_PARAM, tempFile.getAbsolutePath()); 084 params.put(OUTPUT_FILE_PATH_PARAM, outputFile.getAbsolutePath()); 085 return params; 086 } catch (IOException e) { 087 throw new NuxeoException("VideoConcat could not prepare the parameters.", e); 088 } 089 } 090 091 @Override 092 public void cleanupInputs(Map<String, String> parameters) { 093 String path = parameters.get(VIDEOS_FILE_PATH_PARAM); 094 FileUtils.deleteQuietly(new File(path)); 095 } 096 097}