org.jcodec.api.transcode.TranscodeMain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcodec Show documentation
Show all versions of jcodec Show documentation
Pure Java implementation of video/audio codecs and formats
package org.jcodec.api.transcode;
import static org.jcodec.common.Tuple._2;
import static org.jcodec.common.Tuple._3;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jcodec.api.transcode.Transcoder.TranscoderBuilder;
import org.jcodec.api.transcode.filters.DumpMvFilter;
import org.jcodec.api.transcode.filters.ScaleFilter;
import org.jcodec.common.Codec;
import org.jcodec.common.Demuxer;
import org.jcodec.common.DemuxerTrack;
import org.jcodec.common.DemuxerTrackMeta;
import org.jcodec.common.Format;
import org.jcodec.common.JCodecUtil;
import org.jcodec.common.TrackType;
import org.jcodec.common.logging.LogLevel;
import org.jcodec.common.logging.Logger;
import org.jcodec.common.logging.OutLogSink;
import org.jcodec.common.logging.OutLogSink.SimpleFormat;
import org.jcodec.common.model.Packet;
import org.jcodec.common.tools.MainUtils;
import org.jcodec.common.tools.MainUtils.Cmd;
import org.jcodec.common.tools.MainUtils.Flag;
import org.jcodec.common.tools.MainUtils.FlagType;
import org.jcodec.common.tools.MathUtil;
/**
* This class is part of JCodec ( www.jcodec.org ) This software is distributed
* under FreeBSD License
*
* Transcoder command line.
*
* @author The JCodec project
*
*/
public class TranscodeMain {
private static final Flag FLAG_INPUT = new Flag("input", "i", "Designates an input argument", FlagType.VOID);
private static final Flag FLAG_MAP_VIDEO = new Flag("map:v", "mv",
"Map a video from a specified input into this output");
private static final Flag FLAG_MAP_AUDIO = new Flag("map:a", "ma",
"Map a audio from a specified input into this output");
private static final Flag FLAG_SEEK_FRAMES = new Flag("seek-frames", "Seek frames");
private static final Flag FLAG_MAX_FRAMES = new Flag("max-frames", "limit", "Max frames");
private static final Flag FLAG_AUDIO_CODEC = new Flag("codec:audio", "acodec", "Audio codec [default=auto].");
private static final Flag FLAG_VIDEO_CODEC = new Flag("codec:video", "vcodec", "Video codec [default=auto].");
private static final Flag FLAG_FORMAT = new Flag("format", "f", "Format [default=auto].");
private static final Flag FLAG_PROFILE = new Flag("profile", "Profile to use (supported by some encoders).");
private static final Flag FLAG_INTERLACED = new Flag("interlaced",
"Encode output as interlaced (supported by Prores encoder).");
private static final Flag FLAG_DUMPMV = new Flag("dumpMv", "Dump motion vectors (supported by h.264 decoder).");
private static final Flag FLAG_DUMPMVJS = new Flag("dumpMvJs",
"Dump motion vectors in form of JASON file (supported by h.264 decoder).");
private static final Flag FLAG_DOWNSCALE = new Flag("downscale",
"Decode frames in downscale (supported by MPEG, Prores and Jpeg decoders).");
private static final Flag FLAG_VIDEO_FILTER = new Flag("videoFilter", "vf",
"Contains a comma separated list of video filters with arguments.");
private static final Flag[] ALL_FLAGS = new Flag[] { FLAG_INPUT, FLAG_FORMAT, FLAG_VIDEO_CODEC, FLAG_AUDIO_CODEC,
FLAG_SEEK_FRAMES, FLAG_MAX_FRAMES, FLAG_PROFILE, FLAG_INTERLACED, FLAG_DUMPMV, FLAG_DUMPMVJS,
FLAG_DOWNSCALE, FLAG_MAP_VIDEO, FLAG_MAP_AUDIO, FLAG_VIDEO_FILTER };
private static Map extensionToF = new HashMap();
private static Map extensionToC = new HashMap();
private static Map videoCodecsForF = new HashMap();
private static Map audioCodecsForF = new HashMap();
private static Set supportedDecoders = new HashSet();
private static Map> knownFilters = new HashMap>();
static {
extensionToF.put("mp3", Format.MPEG_AUDIO);
extensionToF.put("mp2", Format.MPEG_AUDIO);
extensionToF.put("mp1", Format.MPEG_AUDIO);
extensionToF.put("mpg", Format.MPEG_PS);
extensionToF.put("mpeg", Format.MPEG_PS);
extensionToF.put("m2p", Format.MPEG_PS);
extensionToF.put("ps", Format.MPEG_PS);
extensionToF.put("vob", Format.MPEG_PS);
extensionToF.put("evo", Format.MPEG_PS);
extensionToF.put("mod", Format.MPEG_PS);
extensionToF.put("tod", Format.MPEG_PS);
extensionToF.put("ts", Format.MPEG_TS);
extensionToF.put("m2t", Format.MPEG_TS);
extensionToF.put("mp4", Format.MOV);
extensionToF.put("m4a", Format.MOV);
extensionToF.put("m4v", Format.MOV);
extensionToF.put("mov", Format.MOV);
extensionToF.put("3gp", Format.MOV);
extensionToF.put("mkv", Format.MKV);
extensionToF.put("webm", Format.MKV);
extensionToF.put("264", Format.H264);
extensionToF.put("jsv", Format.H264);
extensionToF.put("h264", Format.H264);
extensionToF.put("raw", Format.RAW);
extensionToF.put("", Format.RAW);
extensionToF.put("flv", Format.FLV);
extensionToF.put("avi", Format.AVI);
extensionToF.put("jpg", Format.IMG);
extensionToF.put("jpeg", Format.IMG);
extensionToF.put("png", Format.IMG);
extensionToF.put("mjp", Format.MJPEG);
extensionToF.put("ivf", Format.IVF);
extensionToF.put("y4m", Format.Y4M);
extensionToF.put("wav", Format.WAV);
extensionToC.put("mpg", Codec.MPEG2);
extensionToC.put("mpeg", Codec.MPEG2);
extensionToC.put("m2p", Codec.MPEG2);
extensionToC.put("ps", Codec.MPEG2);
extensionToC.put("vob", Codec.MPEG2);
extensionToC.put("evo", Codec.MPEG2);
extensionToC.put("mod", Codec.MPEG2);
extensionToC.put("tod", Codec.MPEG2);
extensionToC.put("ts", Codec.MPEG2);
extensionToC.put("m2t", Codec.MPEG2);
extensionToC.put("m4a", Codec.AAC);
extensionToC.put("mkv", Codec.H264);
extensionToC.put("webm", Codec.VP8);
extensionToC.put("264", Codec.H264);
extensionToC.put("raw", Codec.RAW);
extensionToC.put("jpg", Codec.JPEG);
extensionToC.put("jpeg", Codec.JPEG);
extensionToC.put("png", Codec.PNG);
extensionToC.put("mjp", Codec.JPEG);
extensionToC.put("y4m", Codec.RAW);
videoCodecsForF.put(Format.MPEG_PS, Codec.MPEG2);
audioCodecsForF.put(Format.MPEG_PS, Codec.MP2);
videoCodecsForF.put(Format.MOV, Codec.H264);
audioCodecsForF.put(Format.MOV, Codec.AAC);
videoCodecsForF.put(Format.MKV, Codec.VP8);
audioCodecsForF.put(Format.MKV, Codec.VORBIS);
audioCodecsForF.put(Format.WAV, Codec.PCM);
videoCodecsForF.put(Format.H264, Codec.H264);
videoCodecsForF.put(Format.RAW, Codec.RAW);
videoCodecsForF.put(Format.FLV, Codec.H264);
videoCodecsForF.put(Format.AVI, Codec.MPEG4);
videoCodecsForF.put(Format.IMG, Codec.PNG);
videoCodecsForF.put(Format.MJPEG, Codec.JPEG);
videoCodecsForF.put(Format.IVF, Codec.VP8);
videoCodecsForF.put(Format.Y4M, Codec.RAW);
supportedDecoders.add(Codec.AAC);
supportedDecoders.add(Codec.H264);
supportedDecoders.add(Codec.JPEG);
supportedDecoders.add(Codec.MPEG2);
supportedDecoders.add(Codec.PCM);
supportedDecoders.add(Codec.PNG);
supportedDecoders.add(Codec.PRORES);
supportedDecoders.add(Codec.RAW);
supportedDecoders.add(Codec.VP8);
supportedDecoders.add(Codec.MP3);
supportedDecoders.add(Codec.MP2);
supportedDecoders.add(Codec.MP1);
knownFilters.put("scale", ScaleFilter.class);
}
public static void main(String[] args) throws Exception {
Logger.addSink(new OutLogSink(System.out, new SimpleFormat("#message"), LogLevel.INFO));
Cmd cmd = MainUtils.parseArguments(args, ALL_FLAGS);
TranscoderBuilder builder = Transcoder.newTranscoder();
List