Debian Codegraph @

bento4-embedded/Source/C++/Apps/Mp4AudioClip/Mp4AudioClip.cpp

raw symbols SCIP
1 /*****************************************************************
2 |
3 | AP4 - MP4 AUdio Clip Extractor
4 |
5 | Copyright 2002-2013 Axiomatic Systems, LLC
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 /*----------------------------------------------------------------------
30 | includes
31 +---------------------------------------------------------------------*/
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "Ap4.h"
36
37 /*----------------------------------------------------------------------
38 | constants
39 +---------------------------------------------------------------------*/
40 #define BANNER "MP4 Audio Clip Extractor - Version 1.0\n"\
41 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
42 "(c) 2002-2013 Axiomatic Systems, LLC"
43
44 /*----------------------------------------------------------------------
45 | PrintUsageAndExit
46 +---------------------------------------------------------------------*/
47 static void
48 PrintUsageAndExit()
49 {
50 fprintf(stderr,
51 BANNER
52 "\n\n"
53 "usage: mp4audioclip [options] <input> <output>\n"
54 "Options:\n"
55 " --start <time> : start time in milliseconds (default = 0)\n"
56 " --duration <duration> : clip duration in milliseconds (default = until end)\n"
57 );
58 exit(1);
59 }
60
61 /*----------------------------------------------------------------------
62 | main
63 +---------------------------------------------------------------------*/
64 int
65 main(int argc, char** argv)
66 {
67 if (argc == 1) PrintUsageAndExit();
68
69 // parse options
70 const char* input_filename = NULL;
71 const char* output_filename = NULL;
72 unsigned int start_time_ms = 0;
73 unsigned int duration_ms = 0;
74 AP4_Result result;
75
76 // parse the command line arguments
77 char* arg;
78 while ((arg = *++argv)) {
79 if (!AP4_CompareStrings(arg, "--start")) {
80 arg = *++argv;
81 if (arg == NULL) {
82 fprintf(stderr, "ERROR: missing argument after --start option\n");
83 return 1;
84 }
85 start_time_ms = AP4_ParseIntegerU(arg);
86 } else if (!AP4_CompareStrings(arg, "--duration")) {
87 arg = *++argv;
88 if (arg == NULL) {
89 fprintf(stderr, "ERROR: missing argument after --duration option\n");
90 return 1;
91 }
92 duration_ms = AP4_ParseIntegerU(arg);
93 } else if (input_filename == NULL) {
94 input_filename = arg;
95 } else if (output_filename == NULL) {
96 output_filename = arg;
97 } else {
98 fprintf(stderr, "ERROR: unexpected argument (%s)\n", arg);
99 return 1;
100 }
101 }
102
103 // check arguments
104 if (!input_filename) {
105 fprintf(stderr, "ERROR: missing input file name argument\n");
106 return 1;
107 }
108 if (!output_filename) {
109 fprintf(stderr, "ERROR: missing output file name argument\n");
110 return 1;
111 }
112
113 // create the input stream
114 AP4_ByteStream* input = NULL;
115 result = AP4_FileByteStream::Create(input_filename, AP4_FileByteStream::STREAM_MODE_READ, input);
116 if (AP4_FAILED(result)) {
117 fprintf(stderr, "ERROR: cannot open input file (%s)\n", input_filename);
118 return 1;
119 }
120
121 // parse the input
122 AP4_File input_file(*input);
123 AP4_Movie* input_movie = input_file.GetMovie();
124 if (input_movie == NULL) {
125 fprintf(stderr, "ERROR: cannot parse input (%d)\n", result);
126 return 1;
127 }
128
129 // find the first audio track
130 AP4_Track* input_track = input_movie->GetTrack(AP4_Track::TYPE_AUDIO);
131 if (input_track == NULL) {
132 fprintf(stderr, "ERROR: no audio track found\n");
133 return 1;
134 }
135
136 // create a synthetic sample table to hold the output samples
137 AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable();
138
139 // use the same sample description(s) in the output as what's in the input
140 // (in almost all cases, there will only be one sample description)
141 // NOTE: we don't transfer the ownership of the object
142 for (unsigned int i=0; i<input_track->GetSampleDescriptionCount(); i++) {
143 AP4_SampleDescription* sample_description = input_track->GetSampleDescription(i);
144 if (!sample_description) {
145 fprintf(stderr, "ERROR: invalid/unsupported sample description\n");
146 return 1;
147 }
148 sample_table->AddSampleDescription(sample_description, false);
149 }
150
151 // the first output timestamp is 0
152 AP4_UI64 dts = 0;
153
154 // compute the maximum timestamp based on the duration
155 AP4_UI64 max_dts;
156 if (duration_ms == 0) {
157 // go until the end
158 max_dts = input_track->GetDuration();
159 } else {
160 max_dts = AP4_ConvertTime(duration_ms, 1000, input_track->GetMediaTimeScale());
161 }
162
163 // compute the first sample index
164 AP4_Ordinal start_sample = 0;
165 if (AP4_FAILED(input_track->GetSampleIndexForTimeStampMs(start_time_ms, start_sample))) {
166 fprintf(stderr, "ERROR: start time out of range\n");
167 return 1;
168 }
169
170 // add all the input samples to the sample table
171 for (unsigned int i=start_sample; i<input_track->GetSampleCount(); i++) {
172 AP4_Sample sample;
173 if (AP4_FAILED(input_track->GetSample(i, sample))) break;
174
175 // obtain the sample data stream in a variable so that we can release the reference
176 AP4_ByteStream* sample_stream = sample.GetDataStream();
177
178 // add a sample to the sample table
179 sample_table->AddSample(*sample_stream,
180 sample.GetOffset(),
181 sample.GetSize(),
182 sample.GetDuration(),
183 sample.GetDescriptionIndex(),
184 dts,
185 0,
186 true);
187 sample_stream->Release(); // the sample table has kept its own reference
188
189 // update the current timestamp
190 dts += sample.GetDuration();
191
192 // stop if we've exceeded the duration
193 if (dts > max_dts) break;
194 }
195
196 // compute the actual effective duration (could be less than what was asked for)
197 duration_ms = (AP4_UI32)AP4_ConvertTime(dts, input_track->GetMediaTimeScale(), 1000);
198
199 // create an output track
200 AP4_Track* output_track =
201 new AP4_Track(AP4_Track::TYPE_AUDIO,
202 sample_table,
203 1, // track id
204 input_movie->GetTimeScale(), // movie time scale
205 AP4_ConvertTime(duration_ms, 1000, input_movie->GetTimeScale()), // track duration (in the timescale of the movie)
206 input_track->GetMediaTimeScale(), // media time scale
207 AP4_ConvertTime(duration_ms, 1000, input_track->GetMediaTimeScale()), // media duration (in the timescale of the media)
208 "eng", // language
209 0, 0); // width, height
210
211
212 // create the output movie
213 AP4_Movie* output_movie = new AP4_Movie(input_movie->GetTimeScale());
214
215 // create the output file
216 AP4_File output_file(output_movie);
217
218 // add the output track
219 output_movie->AddTrack(output_track);
220
221 // set the output file type
222 AP4_UI32 compatible_brands[2] = {
223 AP4_FILE_BRAND_ISOM,
224 AP4_FILE_BRAND_MP42
225 };
226 output_file.SetFileType(AP4_FILE_BRAND_M4A_, 0, compatible_brands, 2);
227
228 // create the output stream
229 AP4_ByteStream* output = NULL;
230 result = AP4_FileByteStream::Create(output_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output);
231 if (AP4_FAILED(result)) {
232 fprintf(stderr, "ERROR: cannot open output file (%s)\n", output_filename);
233 return 1;
234 }
235
236 // write the output file
237 AP4_FileWriter::Write(output_file, *output);
238
239 // cleanup
240 input->Release();
241 output->Release();
242
243 return 0;
244 }