001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.platform.video.convert;
020
021import java.util.List;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import org.nuxeo.common.utils.StringUtils;
026import org.nuxeo.ecm.core.convert.api.ConversionException;
027import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
028
029/**
030 * Factorize common code for video converter implementations.
031 *
032 * @author ogrisel
033 */
034public abstract class BaseVideoConverter {
035
036    protected static final Pattern DURATION_PATTERN = Pattern.compile("Duration: (\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d+)");
037
038    /**
039     * @deprecated since 5.5. The duration is now extracted with the other information stored in the VideoInfo.
040     */
041    @Deprecated
042    protected static Double extractDuration(List<String> output) throws ConversionException {
043        for (String line : output) {
044            Matcher matcher = DURATION_PATTERN.matcher(line);
045            if (matcher.find()) {
046                return Double.parseDouble(matcher.group(1)) * 3600 + Double.parseDouble(matcher.group(2)) * 60
047                        + Double.parseDouble(matcher.group(3)) + Double.parseDouble(matcher.group(3)) / 100;
048            }
049        }
050        // could not find the duration
051        throw new ConversionException("failed to extract the duration from output: " + StringUtils.join(output, " "));
052    }
053
054    /**
055     * @deprecated since 5.5.
056     */
057    @Deprecated
058    public static String quoteFilePath(String filePath) {
059        return String.format("\"%s\"", filePath);
060    }
061
062}