Mp4Info.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:9k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - MP4 File Info
  4. |
  5. |    Copyright 2002-2205 Gilles Boccon-Gibod & Julien Boeuf
  6. |
  7. |
  8. |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
  9. |
  10. |    Unless you have obtained Bento4 under a difference license,
  11. |    this version of Bento4 is Bento4|GPL.
  12. |    Bento4|GPL is free software; you can redistribute it and/or modify
  13. |    it under the terms of the GNU General Public License as published by
  14. |    the Free Software Foundation; either version 2, or (at your option)
  15. |    any later version.
  16. |
  17. |    Bento4|GPL is distributed in the hope that it will be useful,
  18. |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. |    GNU General Public License for more details.
  21. |
  22. |    You should have received a copy of the GNU General Public License
  23. |    along with Bento4|GPL; see the file COPYING.  If not, write to the
  24. |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  25. |    02111-1307, USA.
  26. |
  27.  ****************************************************************/
  28. /*----------------------------------------------------------------------
  29. |       includes
  30. +---------------------------------------------------------------------*/
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "Ap4.h"
  34. #include "Ap4FileByteStream.h"
  35. #include "Ap4Atom.h"
  36. #include "Ap4File.h"
  37. #include "Ap4Sample.h"
  38. #include "Ap4SampleDescription.h"
  39. #include "Ap4IsmaCryp.h"
  40. #include "Ap4IsfmAtom.h"
  41. #include "Ap4IkmsAtom.h"
  42. /*----------------------------------------------------------------------
  43. |       constants
  44. +---------------------------------------------------------------------*/
  45. #define BANNER "MP4 File Info - Version 0.1an"
  46.                "(c) 2002-2005 Gilles Boccon-Gibod & Julien Boeuf"
  47.  
  48. /*----------------------------------------------------------------------
  49. |       PrintUsageAndExit
  50. +---------------------------------------------------------------------*/
  51. static void
  52. PrintUsageAndExit()
  53. {
  54.     fprintf(stderr, 
  55.             BANNER 
  56.             "nnusage: mp4info [options] <input>n");
  57.     exit(1);
  58. }
  59. /*----------------------------------------------------------------------
  60. |       ShowIsmaSampleDescription
  61. +---------------------------------------------------------------------*/
  62. static void
  63. ShowIsmaSampleDescription(AP4_IsmaCrypSampleDescription* description)
  64. {
  65.     if (description == NULL) return;
  66.     AP4_Debug("    [ENCRYPTED]n");
  67.     AP4_UI32 st = description->GetSchemeType();
  68.     AP4_Debug("      Scheme Type:    %c%c%c%cn", 
  69.         (char)((st>>24) & 0xFF),
  70.         (char)((st>>16) & 0xFF),
  71.         (char)((st>> 8) & 0xFF),
  72.         (char)((st    ) & 0xFF));
  73.     AP4_Debug("      Scheme Version: %dn", description->GetSchemeVersion());
  74.     AP4_Debug("      Scheme URI:     %sn", description->GetSchemeUri().c_str());
  75.     AP4_IsmaCrypSchemeInfo* scheme_info = description->GetSchemeInfo();
  76.     if (scheme_info == NULL) return;
  77.     if (description->GetSchemeType() != AP4_ISMACRYP_SCHEME_TYPE_IAEC) {
  78.         return;
  79.     }
  80.     AP4_Debug("      iAEC Scheme Info:n");
  81.     AP4_IkmsAtom* ikms = (AP4_IkmsAtom*)scheme_info->GetSchiAtom().FindChild("iKMS");
  82.     if (ikms) {
  83.         AP4_Debug("        KMS URI:              %sn", ikms->GetKmsUri().c_str());
  84.     }
  85.     AP4_IsfmAtom* isfm = (AP4_IsfmAtom*)scheme_info->GetSchiAtom().FindChild("iSFM");
  86.     if (isfm) {
  87.         AP4_Debug("        Selective Encryption: %sn", isfm->GetSelectiveEncryption()?"yes":"no");
  88.         AP4_Debug("        Key Indicator Length: %dn", isfm->GetKeyIndicatorLength());
  89.         AP4_Debug("        IV Length:            %dn", isfm->GetIvLength());
  90.     }
  91. }
  92. /*----------------------------------------------------------------------
  93. |       ShowSampleDescription
  94. +---------------------------------------------------------------------*/
  95. static void
  96. ShowSampleDescription(AP4_SampleDescription* desc)
  97. {
  98.     AP4_SampleDescription::Type type = desc->GetType();
  99.     AP4_MpegSampleDescription* mpeg_desc = NULL;
  100.     if (type == AP4_SampleDescription::TYPE_MPEG) {
  101.         mpeg_desc = dynamic_cast<AP4_MpegSampleDescription*>(desc);
  102.     } else if (type == AP4_SampleDescription::TYPE_ISMACRYP) {
  103.         AP4_IsmaCrypSampleDescription* isma_desc = dynamic_cast<AP4_IsmaCrypSampleDescription*>(desc);
  104.         ShowIsmaSampleDescription(isma_desc);
  105.         mpeg_desc = isma_desc->GetOriginalSampleDescription();
  106.     }
  107.     if (mpeg_desc) {
  108.         AP4_Debug("    Stream Type: %sn", mpeg_desc->GetStreamTypeString(mpeg_desc->GetStreamType()));
  109.         AP4_Debug("    Object Type: %sn", mpeg_desc->GetObjectTypeString(mpeg_desc->GetObjectTypeId()));
  110.         AP4_Debug("    Max Bitrate: %dn", mpeg_desc->GetMaxBitrate());
  111.         AP4_Debug("    Avg Bitrate: %dn", mpeg_desc->GetAvgBitrate());
  112.         AP4_Debug("    Buffer Size: %dn", mpeg_desc->GetBufferSize());
  113.         if (mpeg_desc->GetStreamType() == AP4_AUDIO_STREAM_TYPE) {
  114.             AP4_MpegAudioSampleDescription* audio_desc = 
  115.                 dynamic_cast<AP4_MpegAudioSampleDescription*>(mpeg_desc);
  116.             AP4_Debug("    Sample Rate: %dn", audio_desc->GetSampleRate());
  117.             AP4_Debug("    Sample Size: %dn", audio_desc->GetSampleSize());
  118.             AP4_Debug("    Channels:    %dn", audio_desc->GetChannelCount());
  119.         } else if (mpeg_desc->GetStreamType() == AP4_VISUAL_STREAM_TYPE) {
  120.             AP4_MpegVideoSampleDescription* video_desc = 
  121.                 dynamic_cast<AP4_MpegVideoSampleDescription*>(mpeg_desc);
  122.             AP4_Debug("    Width:  %dn", video_desc->GetWidth());
  123.             AP4_Debug("    Height: %dn", video_desc->GetHeight());
  124.             AP4_Debug("    Depth:  %dn", video_desc->GetDepth());
  125.         }
  126.     }
  127. }
  128. /*----------------------------------------------------------------------
  129. |       ShowTrackInfo
  130. +---------------------------------------------------------------------*/
  131. static void
  132. ShowTrackInfo(AP4_Track* track)
  133. {
  134.     AP4_Debug("  type:         ");
  135.     switch (track->GetType()) {
  136.         case AP4_Track::TYPE_AUDIO: AP4_Debug("Audion"); break;
  137.         case AP4_Track::TYPE_VIDEO: AP4_Debug("Videon"); break;
  138.         case AP4_Track::TYPE_HINT:  AP4_Debug("Hintn");  break;
  139.         default:                    AP4_Debug("Systemn");break;
  140.     }
  141.     AP4_Debug("  duration:     %ld msn", track->GetDurationMs());
  142.     AP4_Debug("  timescale:    %ldn", track->GetMediaTimeScale());
  143.     AP4_Debug("  sample count: %ldn", track->GetSampleCount());
  144. AP4_Sample  sample;
  145. AP4_Ordinal index = 0;
  146.     AP4_Ordinal desc_index = 0xFFFFFFFF;
  147.     while (AP4_SUCCEEDED(track->GetSample(index, sample))) {
  148.         if (sample.GetDescriptionIndex() != desc_index) {
  149.             desc_index = sample.GetDescriptionIndex();
  150.             AP4_Debug("  [%d]: Format %dn", index, desc_index);
  151.             
  152.             // get the sample description
  153.             AP4_SampleDescription* sample_desc = 
  154.                 track->GetSampleDescription(desc_index);
  155.             if (sample_desc != NULL) {
  156.                 ShowSampleDescription(sample_desc);
  157.             }
  158.         }
  159.         index++;
  160.     }
  161. }
  162. /*----------------------------------------------------------------------
  163. |       ShowMovieInfo
  164. +---------------------------------------------------------------------*/
  165. static void
  166. ShowMovieInfo(AP4_Movie* movie)
  167. {
  168.     AP4_Debug("Movie:n");
  169.     AP4_Debug("  duration:   %ld msn", movie->GetDurationMs());
  170.     AP4_Debug("  time scale: %ldn", movie->GetTimeScale());
  171.     AP4_Debug("n");
  172. }
  173. /*----------------------------------------------------------------------
  174. |       main
  175. +---------------------------------------------------------------------*/
  176. int
  177. main(int argc, char** argv)
  178. {
  179.     if (argc < 2) {
  180.         PrintUsageAndExit();
  181.     }
  182.     
  183.     AP4_ByteStream* input;
  184.     try {
  185.         input = new AP4_FileByteStream(argv[1],
  186.                                AP4_FileByteStream::STREAM_MODE_READ);
  187.     } catch (AP4_Exception) {
  188.         fprintf(stderr, "ERROR: cannot open input file (%s)n", argv[1]);
  189.         return 1;
  190.     }
  191.     AP4_File* file = new AP4_File(*input);
  192.     
  193.     AP4_Movie* movie = file->GetMovie();
  194.     if (movie != NULL) {
  195.         ShowMovieInfo(movie);
  196.         AP4_List<AP4_Track>& tracks = movie->GetTracks();
  197.         AP4_Debug("Found %d Tracksn", tracks.ItemCount());
  198.         AP4_List<AP4_Track>::Item* track_item = tracks.FirstItem();
  199.         int index = 1;
  200.         while (track_item) {
  201.             AP4_Debug("Track %d:n", index); 
  202.             index++;
  203.             ShowTrackInfo(track_item->GetData());
  204.             track_item = track_item->GetNext();
  205.         }
  206.     } else {
  207.         AP4_Debug("No movie found in the filen");
  208.     }
  209.     delete file;
  210.     input->Release();
  211.     return 0;
  212. }