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.FilenameUtils;
022import org.apache.commons.lang.StringUtils;
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.util.Arrays;
031import java.util.Collections;
032import java.util.List;
033import java.util.Map;
034
035/**
036 * The {@link VideoTool} that extracts closed captions.
037 *
038 * @since 8.4
039 */
040public class VideoClosedCaptionsExtractor extends VideoTool {
041
042    public final static String NAME = "ccExtractorTool";
043
044    public final static String START_AT_PARAM = "startAt";
045
046    public final static String END_AT_PARAM = "endAt";
047
048    public final static String OUTPUT_FORMAT_PARAM = "outFormat";
049
050    protected static final String COMMANDLINE_FULL_VIDEO = "videoClosedCaptionsExtractor";
051
052    protected static final String COMMANDLINE_SLICED_VIDEO = "videoPartClosedCaptionsExtractor";
053
054    protected static final String DEFAULT_OUTFORMAT = "ttxt";
055
056    protected static final List<String> TEXT_OUTFORMATS = Arrays.asList("srt", "txt", "ttxt");
057
058    public VideoClosedCaptionsExtractor() {
059        super(NAME, COMMANDLINE_FULL_VIDEO);
060    }
061
062    public List<String> getSupportedFormats() {
063        return TEXT_OUTFORMATS;
064    }
065
066    public boolean isFormatSupported(String format) {
067        return TEXT_OUTFORMATS.contains(format);
068    }
069
070    @Override
071    public Map<String, String> setupParameters(BlobHolder blobHolder, Map<String, Object> parameters) {
072        Map<String, String> cmdParameters = super.setupParameters(blobHolder, parameters);
073
074        Blob video = blobHolder.getBlob();
075        String outputFormat = (String) parameters.get(OUTPUT_FORMAT_PARAM);
076        String startAt = (String) parameters.get(START_AT_PARAM);
077        String endAt = (String) parameters.get(END_AT_PARAM);
078
079        if (StringUtils.isEmpty(outputFormat)) {
080            outputFormat = DEFAULT_OUTFORMAT;
081        }
082        cmdParameters.put(OUTPUT_FORMAT_PARAM, outputFormat);
083        cmdParameters.put(OUTPUT_MIMETYPE_PARAM, outputFormat.equals(DEFAULT_OUTFORMAT) ? "text/xml" : "text/plain");
084
085        if (!StringUtils.isBlank(startAt) && !StringUtils.isBlank(endAt)) {
086            cmdParameters.put(START_AT_PARAM, startAt);
087            cmdParameters.put(END_AT_PARAM, endAt);
088            commandLineName = COMMANDLINE_SLICED_VIDEO;
089        }
090
091        try {
092            File outputFile = Framework.createTempFile(FilenameUtils.removeExtension(video.getFilename()),
093                    "-CC." + outputFormat);
094
095            cmdParameters.put(OUTPUT_FILE_PATH_PARAM, outputFile.getAbsolutePath());
096        } catch (IOException e) {
097            throw new NuxeoException("Cannot setup parameters for VideoClosedCaptionsExtractor", e);
098        }
099        return cmdParameters;
100    }
101}