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 org.apache.commons.io.FileUtils;
022import org.apache.commons.io.FilenameUtils;
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
026import org.nuxeo.runtime.api.Framework;
027
028import java.io.File;
029import java.io.IOException;
030import java.nio.charset.StandardCharsets;
031import java.nio.file.Files;
032import java.nio.file.StandardOpenOption;
033import java.util.ArrayList;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037
038/**
039 * The {@link VideoTool} that joins two or more video blobs.
040 *
041 * @since 8.4
042 */
043public class VideoConcat extends VideoTool {
044
045    public final static String NAME = "concatTool";
046
047    protected final static String VIDEOS_FILE_PATH_PARAM = "listFilePath";
048
049    protected final static String VIDEO_CONCAT_COMMANDLINE = "videoConcat";
050
051    public VideoConcat() {
052        super(VideoConcat.NAME, VIDEO_CONCAT_COMMANDLINE);
053    }
054
055    @Override
056    public Map<String, String> setupParameters(BlobHolder blobHolder, Map<String, Object> parameters) {
057        List<Blob> videos = blobHolder.getBlobs();
058        List<File> sourceFiles = new ArrayList<>();
059        File tempFile;
060        try {
061            //String list = "";
062            List<String> lines = new ArrayList<>();
063            for (Blob b : videos) {
064                File blobFile = b.getFile();
065                sourceFiles.add(blobFile);
066                lines.add("file '" + blobFile.getAbsolutePath());
067            }
068            tempFile = Framework.createTempFile("NxVTcv-", ".txt");
069            Files.write(tempFile.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
070
071            if (blobHolder.getBlobs().size() < 2) {
072                throw new NuxeoException("VideoConcat requires at least two videos to proceed.");
073            }
074
075            Blob firstVideo = blobHolder.getBlobs().get(0);
076            String outputFilename = firstVideo.getFilename();
077            File outputFile = Framework.createTempFile(FilenameUtils.removeExtension(outputFilename),
078                    "-concat." + FilenameUtils.getExtension(outputFilename));
079
080            // Run the command line
081            Map<String, String> params = new HashMap<>();
082            params.put(VIDEOS_FILE_PATH_PARAM, tempFile.getAbsolutePath());
083            params.put(OUTPUT_FILE_PATH_PARAM, outputFile.getAbsolutePath());
084            return params;
085        } catch (IOException e) {
086            throw new NuxeoException("VideoConcat could not prepare the parameters.", e);
087        }
088    }
089
090    @Override
091    public void cleanupInputs(Map<String, String> parameters) {
092        String path = parameters.get(VIDEOS_FILE_PATH_PARAM);
093        FileUtils.deleteQuietly(new File(path));
094    }
095
096}