001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019
020package com.avpkit.core.demos;
021
022import com.avpkit.core.Configuration;
023import com.avpkit.core.Global;
024import com.avpkit.core.ICodec;
025import com.avpkit.core.IContainer;
026import com.avpkit.core.IStream;
027import com.avpkit.core.IStreamCoder;
028
029/**
030 * Opens up a media container, and prints out a summary of the contents.
031 * 
032 * If you pass -Davpkit.options we'll also tell you what every
033 * configurable option on the container and streams is set to.
034 * 
035 * @author aclarke
036 *
037 */
038public class GetContainerInfo
039{
040  /**
041   * Takes a media container (file) as the first argument, opens it, and tells you what's inside the container.
042   * @param args Must contain one string which represents a filename
043   */
044  public static void main(String[] args)
045  {
046    // If the user passes -Davpkit.options, then we print
047    // out all possible options as well.
048    String optionString = System.getProperty("avpkit.options");
049    if (optionString != null)
050    {
051      Configuration.printHelp(System.out);
052    }
053
054    if (args.length <= 0)
055      throw new IllegalArgumentException("must pass in a filename as the first argument");
056    
057    String filename = args[0];
058    // Create a AVPKit container object
059    IContainer container = IContainer.make();
060    
061    // Open up the container
062    if (container.open(filename, IContainer.Type.READ, null) < 0)
063      throw new IllegalArgumentException("could not open file: " + filename);
064    
065    // query how many streams the call to open found
066    int numStreams = container.getNumStreams();
067    System.out.printf("file \"%s\": %d stream%s; ",
068        filename,
069        numStreams,
070        numStreams == 1 ? "" : "s");
071    System.out.printf("duration (ms): %s; ", container.getDuration() == Global.NO_PTS ? "unknown" : "" + container.getDuration()/1000);
072    System.out.printf("start time (ms): %s; ", container.getStartTime() == Global.NO_PTS ? "unknown" : "" + container.getStartTime()/1000);
073    System.out.printf("file size (bytes): %d; ", container.getFileSize());
074    System.out.printf("bit rate: %d; ", container.getBitRate());
075    System.out.printf("\n");
076
077    // and iterate through the streams to print their meta data
078    for(int i = 0; i < numStreams; i++)
079    {
080      // Find the stream object
081      IStream stream = container.getStream(i);
082      // Get the pre-configured decoder that can decode this stream;
083      IStreamCoder coder = stream.getStreamCoder();
084      
085      // and now print out the meta data.
086      System.out.printf("stream %d: ",    i);
087      System.out.printf("type: %s; ",     coder.getCodecType());
088      System.out.printf("codec: %s; ",    coder.getCodecID());
089      System.out.printf("duration: %s; ", stream.getDuration() == Global.NO_PTS ? "unknown" : "" + stream.getDuration());
090      System.out.printf("start time: %s; ", container.getStartTime() == Global.NO_PTS ? "unknown" : "" + stream.getStartTime());
091      System.out.printf("language: %s; ", stream.getLanguage() == null ? "unknown" : stream.getLanguage());
092      System.out.printf("timebase: %d/%d; ", stream.getTimeBase().getNumerator(), stream.getTimeBase().getDenominator());
093      System.out.printf("coder tb: %d/%d; ", coder.getTimeBase().getNumerator(), coder.getTimeBase().getDenominator());
094      
095      if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO)
096      {
097        System.out.printf("sample rate: %d; ", coder.getSampleRate());
098        System.out.printf("channels: %d; ",    coder.getChannels());
099        System.out.printf("format: %s",        coder.getSampleFormat());
100      } else if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO)
101      {
102        System.out.printf("width: %d; ",  coder.getWidth());
103        System.out.printf("height: %d; ", coder.getHeight());
104        System.out.printf("format: %s; ", coder.getPixelType());
105        System.out.printf("frame-rate: %5.2f; ", coder.getFrameRate().getDouble());
106      }
107      System.out.printf("\n");
108    }
109    
110  }
111}