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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - AAC to MP4 Converter
  4. |
  5. |    Copyright 2003 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 "Ap4File.h"
  35. #include "Ap4FileWriter.h"
  36. #include "Ap4FileByteStream.h"
  37. #include "Ap4SyntheticSampleTable.h"
  38. #include "Ap4AdtsParser.h"
  39. /*----------------------------------------------------------------------
  40. |       constants
  41. +---------------------------------------------------------------------*/
  42. #define BANNER "AAC to MP4 Converter - Version 0.1a - (c) 2002-2005 Gilles Boccon-Gibod"
  43.  
  44. /*----------------------------------------------------------------------
  45. |       PrintUsageAndExit
  46. +---------------------------------------------------------------------*/
  47. static void
  48. PrintUsageAndExit()
  49. {
  50.     fprintf(stderr, 
  51.             BANNER 
  52.             "nnusage: aac2mp4 [options] <input> <output>n");
  53.     exit(1);
  54. }
  55. /*----------------------------------------------------------------------
  56. |       main
  57. +---------------------------------------------------------------------*/
  58. int
  59. main(int argc, char** argv)
  60. {
  61.     AP4_Result result;
  62.     if (argc < 2) {
  63.         PrintUsageAndExit();
  64.     }
  65.     
  66.     // open the input
  67.     AP4_ByteStream* input;
  68.     try {
  69.         input = new AP4_FileByteStream(argv[1], AP4_FileByteStream::STREAM_MODE_READ);
  70.     } catch (AP4_Exception&) {
  71.         AP4_Debug("ERROR: cannot open input (%s)n", argv[1]);
  72.         return 1;
  73.     }
  74.     // open the output
  75.     AP4_ByteStream* output = new AP4_FileByteStream(
  76.         argv[2],
  77.         AP4_FileByteStream::STREAM_MODE_WRITE);
  78.     
  79.     // create a sample table
  80.     AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable();
  81.     // create an ADTS parser
  82.     AP4_AdtsParser parser;
  83.     bool           initialized = false;
  84.     unsigned int   sample_description_index = 0;
  85.     // read from the input, feed, and get AAC frames
  86.     AP4_UI32     sample_rate = 0;
  87.     AP4_Cardinal sample_count = 0;
  88.     bool eos = false;
  89.     for(;;) {
  90.         // try to get a frame
  91.         AP4_AacFrame frame;
  92.         result = parser.FindFrame(frame);
  93.         if (AP4_SUCCEEDED(result)) {
  94.             AP4_Debug("AAC frame [%06d]: size = %d, %d kHz, %d chn",
  95.                        sample_count,
  96.                        frame.m_Info.m_FrameLength,
  97.                        frame.m_Info.m_SamplingFrequency,
  98.                        frame.m_Info.m_ChannelConfiguration);
  99.             if (!initialized) {
  100.                 initialized = true;
  101.                 // create a sample description for our samples
  102.                 AP4_DataBuffer dsi;
  103.                 unsigned char aac_dsi[2] = {0x12, 0x10};
  104.                 dsi.SetData(aac_dsi, 2);
  105.                 AP4_MpegAudioSampleDescription* sample_description = 
  106.                     new AP4_MpegAudioSampleDescription(
  107.                     AP4_MPEG4_AUDIO_OTI,   // object type
  108.                     frame.m_Info.m_SamplingFrequency,
  109.                     16,                    // sample size
  110.                     frame.m_Info.m_ChannelConfiguration,
  111.                     &dsi,                  // decoder info
  112.                     6144,                  // buffer size
  113.                     128000,                // max bitrate
  114.                     128000);               // average bitrate
  115.                 sample_description_index = sample_table->AddSampleDescription(sample_description);
  116.                 sample_rate = frame.m_Info.m_SamplingFrequency;
  117.             }
  118.             AP4_MemoryByteStream* sample_data = new AP4_MemoryByteStream(frame.m_Info.m_FrameLength);
  119.             frame.m_Source->ReadBytes(sample_data->GetBuffer(), frame.m_Info.m_FrameLength);
  120.             printf("%02x %02x %02x %02xn", 
  121.                 sample_data->GetBuffer()[0],
  122.                 sample_data->GetBuffer()[1],
  123.                 sample_data->GetBuffer()[2],
  124.                 sample_data->GetBuffer()[3]);
  125.             sample_table->AddSample(*sample_data, 0, frame.m_Info.m_FrameLength, sample_description_index);
  126.             sample_data->Release();
  127.             sample_count++;
  128.         } else {
  129.             if (eos) break;
  130.         }
  131.         // read some data and feed the parser
  132.         AP4_UI08 input_buffer[4096];
  133.         AP4_Size bytes_read = 0;
  134.         AP4_Size to_read = parser.GetBytesFree();
  135.         if (to_read) {
  136.             if (to_read > sizeof(input_buffer)) to_read = sizeof(input_buffer);
  137.             result = input->Read(input_buffer, to_read, &bytes_read);
  138.             if (AP4_SUCCEEDED(result)) {
  139.                 AP4_Size to_feed = bytes_read;
  140.                 result = parser.Feed(input_buffer, &to_feed);
  141.                 if (AP4_FAILED(result)) {
  142.                     AP4_Debug("ERROR: parser.Feed() failed (%d)n", result);
  143.                     return 1;
  144.                 }
  145.             } else {
  146.                 if (result == AP4_ERROR_EOS) {
  147.                     eos = true;
  148.                 }
  149.             }
  150.         }
  151.    }
  152.     // create an audio track
  153.     AP4_Track* track = new AP4_Track(AP4_Track::TYPE_AUDIO, 
  154.                                      sample_table, 
  155.                                      0,     // track id
  156.                                      sample_rate, // movie time scale              
  157.                                      sample_rate, // track time scale
  158.                                      sample_count*1024, // track duration
  159.                                      "eng", // language
  160.                                      0, 0); // width, height
  161.     // create a movie
  162.     AP4_Movie* movie = new AP4_Movie();
  163.     // add the track to the movie
  164.     movie->AddTrack(track);
  165.     // create a multimedia file
  166.     AP4_File* file = new AP4_File(movie);
  167.     // create a writer to write the file
  168.     AP4_FileWriter* writer = new AP4_FileWriter(*file);
  169.     // write the file to the output
  170.     writer->Write(*output);
  171.     delete writer;
  172.     delete file;
  173.     delete output;
  174.     
  175.     return 0;
  176. }