Debian Codegraph @

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

raw
1 /*****************************************************************
2 |
3 | AP4 - MP4 fragmented file splitter
4 |
5 | Copyright 2002-2012 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 Fragment Splitter - Version 1.2\n"\
41 "(Bento4 Version " AP4_VERSION_STRING ")\n"\
42 "(c) 2002-2016 Axiomatic Systems, LLC"
43
44 #define AP4_SPLIT_DEFAULT_INIT_SEGMENT_NAME "init.mp4"
45 #define AP4_SPLIT_DEFAULT_MEDIA_SEGMENT_NAME "segment-%llu.%04llu.m4s"
46 #define AP4_SPLIT_DEFAULT_PATTERN_PARAMS "IN"
47
48 const unsigned int AP4_SPLIT_MAX_TRACK_IDS = 32;
49
50 /*----------------------------------------------------------------------
51 | options
52 +---------------------------------------------------------------------*/
53 struct Options {
54 bool verbose;
55 const char* input;
56 const char* init_segment_name;
57 const char* media_segment_name;
58 const char* pattern_params;
59 unsigned int start_number;
60 unsigned int track_ids[AP4_SPLIT_MAX_TRACK_IDS];
61 unsigned int track_id_count;
62 bool audio_only;
63 bool video_only;
64 bool init_only;
65 } Options;
66
67 /*----------------------------------------------------------------------
68 | PrintUsageAndExit
69 +---------------------------------------------------------------------*/
70 static void
71 PrintUsageAndExit()
72 {
73 fprintf(stderr,
74 BANNER
75 "\n\nusage: mp4split [options] <input>\n"
76 "Options:\n"
77 " --verbose : print verbose information when running\n"
78 " --init-segment <filename> : name of init segment (default: init.mp4)\n"
79 " --init-only : only output the init segment (no media segments)\n"
80 " --media-segment <filename-pattern> (default: segment-%%llu.%%04llu.m4s)\n"
81 " NOTE: all parameters are 64-bit integers, use %%llu in the pattern\n"
82 " --start-number <n> : start numbering segments at <n> (default=1)\n"
83 " --pattern-parameters <params> : one or more selector letter (default: IN)\n"
84 " I: track ID\n"
85 " N: segment number\n"
86 " --track-id <track-id> : only output segments with this track ID\n"
87 " More than one track IDs can be specified if <track-id> is a comma-separated\n"
88 " list of track IDs\n"
89 " --audio : only output audio segments\n"
90 " --video : only output video segments\n");
91 exit(1);
92 }
93
94 /*----------------------------------------------------------------------
95 | NextFragmentIndex
96 +---------------------------------------------------------------------*/
97 AP4_Array<unsigned int> TrackIds;
98 AP4_Array<unsigned int> TrackCounters;
99
100 static unsigned int
101 NextFragmentIndex(unsigned int track_id)
102 {
103 int track_index = -1;
104 for (unsigned int i=0; i<TrackIds.ItemCount(); i++) {
105 if (TrackIds[i] == track_id) {
106 track_index = i;
107 break;
108 }
109 }
110 if (track_index == -1) {
111 track_index = TrackCounters.ItemCount();
112 TrackIds.Append(track_id);
113 TrackCounters.Append(0);
114 }
115 return TrackCounters[track_index]++;
116 }
117
118 /*----------------------------------------------------------------------
119 | ParseTrackIds
120 +---------------------------------------------------------------------*/
121 static bool
122 ParseTrackIds(char* ids)
123 {
124 for (char* separator = ids; ; ++separator) {
125 if (*separator == ',' || *separator == 0) {
126 if (Options.track_id_count >= AP4_SPLIT_MAX_TRACK_IDS) {
127 return false;
128 }
129 bool the_end = (*separator == 0);
130 *separator = 0;
131 Options.track_ids[Options.track_id_count++] = (unsigned int)strtoul(ids, NULL, 10);
132 if (the_end) break;
133 ids = separator+1;
134 }
135 }
136
137 return true;
138 }
139
140 /*----------------------------------------------------------------------
141 | TrackIdMatches
142 +---------------------------------------------------------------------*/
143 static bool
144 TrackIdMatches(unsigned int track_id)
145 {
146 if (Options.track_id_count == 0) return true;
147 for (unsigned int i=0; i<Options.track_id_count; i++) {
148 if (Options.track_ids[i] == track_id) return true;
149 }
150
151 return false;
152 }
153
154 /*----------------------------------------------------------------------
155 | main
156 +---------------------------------------------------------------------*/
157 int
158 main(int argc, char** argv)
159 {
160 if (argc < 2) {
161 PrintUsageAndExit();
162 }
163
164 // default options
165 Options.verbose = false;
166 Options.input = NULL;
167 Options.init_segment_name = AP4_SPLIT_DEFAULT_INIT_SEGMENT_NAME;
168 Options.media_segment_name = AP4_SPLIT_DEFAULT_MEDIA_SEGMENT_NAME;
169 Options.pattern_params = AP4_SPLIT_DEFAULT_PATTERN_PARAMS;
170 Options.start_number = 1;
171 Options.track_id_count = 0;
172 Options.audio_only = false;
173 Options.video_only = false;
174 Options.init_only = false;
175
176 // parse command line
177 AP4_Result result;
178 char** args = argv+1;
179 while (char* arg = *args++) {
180 if (!strcmp(arg, "--verbose")) {
181 Options.verbose = true;
182 } else if (!strcmp(arg, "--init-segment")) {
183 if (*args == NULL) {
184 fprintf(stderr, "ERROR: missing argument after --init-segment option\n");
185 return 1;
186 }
187 Options.init_segment_name = *args++;
188 } else if (!strcmp(arg, "--media-segment")) {
189 if (*args == NULL) {
190 fprintf(stderr, "ERROR: missing argument after --media-segment option\n");
191 return 1;
192 }
193 Options.media_segment_name = *args++;
194 } else if (!strcmp(arg, "--pattern-parameters")) {
195 if (*args == NULL) {
196 fprintf(stderr, "ERROR: missing argument after --pattern-params option\n");
197 return 1;
198 }
199 Options.pattern_params = *args++;
200 } else if (!strcmp(arg, "--track-id")) {
201 if (!ParseTrackIds(*args++)) {
202 fprintf(stderr, "ERROR: invalid argument for --track-id\n");
203 return 1;
204 }
205 } else if (!strcmp(arg, "--start-number")) {
206 Options.start_number = (unsigned int)strtoul(*args++, NULL, 10);
207 } else if (!strcmp(arg, "--init-only")) {
208 Options.init_only = true;
209 } else if (!strcmp(arg, "--audio")) {
210 Options.audio_only = true;
211 } else if (!strcmp(arg, "--video")) {
212 Options.video_only = true;
213 } else if (Options.input == NULL) {
214 Options.input = arg;
215 } else {
216 fprintf(stderr, "ERROR: unexpected argument\n");
217 return 1;
218 }
219 }
220
221 // check args
222 if (Options.input == NULL) {
223 fprintf(stderr, "ERROR: missing input file name\n");
224 return 1;
225 }
226 if ((Options.audio_only && (Options.video_only || Options.track_id_count)) ||
227 (Options.video_only && (Options.audio_only || Options.track_id_count)) ||
228 (Options.track_id_count && (Options.audio_only || Options.video_only ))) {
229 fprintf(stderr, "ERROR: --audio, --video and --track-id options are mutualy exclusive\n");
230 return 1;
231 }
232 if (strlen(Options.pattern_params) < 1) {
233 fprintf(stderr, "ERROR: --pattern-params argument is too short\n");
234 return 1;
235 }
236 if (strlen(Options.pattern_params) > 2) {
237 fprintf(stderr, "ERROR: --pattern-params argument is too long\n");
238 return 1;
239 }
240 const char* cursor = Options.pattern_params;
241 while (*cursor) {
242 if (*cursor != 'I' && *cursor != 'N') {
243 fprintf(stderr, "ERROR: invalid pattern parameter '%c'\n", *cursor);
244 return 1;
245 }
246 ++cursor;
247 }
248
249 // create the input stream
250 AP4_ByteStream* input = NULL;
251 result = AP4_FileByteStream::Create(Options.input, AP4_FileByteStream::STREAM_MODE_READ, input);
252 if (AP4_FAILED(result)) {
253 fprintf(stderr, "ERROR: cannot open input (%d)\n", result);
254 return 1;
255 }
256
257 // get the movie
258 AP4_File* file = new AP4_File(*input, true);
259 AP4_Movie* movie = file->GetMovie();
260 if (movie == NULL) {
261 fprintf(stderr, "no movie found in file\n");
262 return 1;
263 }
264
265 // filter tracks if required
266 if (Options.audio_only) {
267 AP4_Track* track = movie->GetTrack(AP4_Track::TYPE_AUDIO);
268 if (track == NULL) {
269 fprintf(stderr, "--audio option specified, but no audio track found\n");
270 return 1;
271 }
272 Options.track_ids[0] = track->GetId();
273 Options.track_id_count = 1;
274 } else if (Options.video_only) {
275 AP4_Track* track = movie->GetTrack(AP4_Track::TYPE_VIDEO);
276 if (track == NULL) {
277 fprintf(stderr, "--video option specified, but no video track found\n");
278 return 1;
279 }
280 Options.track_ids[0] = track->GetId();
281 Options.track_id_count = 1;
282 } else if (Options.track_id_count) {
283 for (unsigned int i=0; i<Options.track_id_count; i++) {
284 AP4_Track* track = movie->GetTrack(Options.track_ids[i]);
285 if (track == NULL) {
286 fprintf(stderr, "--track-id option specified, but no such track found\n");
287 return 1;
288 }
289 }
290 }
291
292 // save the init segment
293 AP4_ByteStream* output = NULL;
294 result = AP4_FileByteStream::Create(Options.init_segment_name, AP4_FileByteStream::STREAM_MODE_WRITE, output);
295 if (AP4_FAILED(result)) {
296 fprintf(stderr, "ERROR: cannot open output file (%d)\n", result);
297 return 1;
298 }
299 AP4_FtypAtom* ftyp = file->GetFileType();
300 if (ftyp) {
301 result = ftyp->Write(*output);
302 if (AP4_FAILED(result)) {
303 fprintf(stderr, "ERROR: cannot write ftyp segment (%d)\n", result);
304 return 1;
305 }
306 }
307 if (Options.track_id_count) {
308 AP4_MoovAtom* moov = movie->GetMoovAtom();
309
310 // only keep the 'trak' atom that we need
311 AP4_List<AP4_Atom>::Item* child = moov->GetChildren().FirstItem();
312 while (child) {
313 AP4_Atom* atom = child->GetData();
314 child = child->GetNext();
315 if (atom->GetType() == AP4_ATOM_TYPE_TRAK) {
316 AP4_TrakAtom* trak = (AP4_TrakAtom*)atom;
317 AP4_TkhdAtom* tkhd = (AP4_TkhdAtom*)trak->GetChild(AP4_ATOM_TYPE_TKHD);
318 if (tkhd && !TrackIdMatches(tkhd->GetTrackId())) {
319 atom->Detach();
320 delete atom;
321 }
322 }
323 }
324
325 // only keep the 'trex' atom that we need
326 AP4_ContainerAtom* mvex = AP4_DYNAMIC_CAST(AP4_ContainerAtom, moov->GetChild(AP4_ATOM_TYPE_MVEX));
327 if (mvex) {
328 child = mvex->GetChildren().FirstItem();
329 while (child) {
330 AP4_Atom* atom = child->GetData();
331 child = child->GetNext();
332 if (atom->GetType() == AP4_ATOM_TYPE_TREX) {
333 AP4_TrexAtom* trex = AP4_DYNAMIC_CAST(AP4_TrexAtom, atom);
334 if (trex && !TrackIdMatches(trex->GetTrackId())) {
335 atom->Detach();
336 delete atom;
337 }
338 }
339 }
340 }
341 }
342 result = movie->GetMoovAtom()->Write(*output);
343 if (AP4_FAILED(result)) {
344 fprintf(stderr, "ERROR: cannot write init segment (%d)\n", result);
345 return 1;
346 }
347
348 AP4_Atom* atom = NULL;
349 unsigned int track_id = 0;
350 AP4_DefaultAtomFactory atom_factory;
351 for (;!Options.init_only;) {
352 // process the next atom
353 result = atom_factory.CreateAtomFromStream(*input, atom);
354 if (AP4_FAILED(result)) break;
355
356 if (atom->GetType() == AP4_ATOM_TYPE_MOOF) {
357 AP4_ContainerAtom* moof = AP4_DYNAMIC_CAST(AP4_ContainerAtom, atom);
358
359 unsigned int traf_count = 0;
360 AP4_ContainerAtom* traf = NULL;
361 do {
362 traf = AP4_DYNAMIC_CAST(AP4_ContainerAtom, moof->GetChild(AP4_ATOM_TYPE_TRAF, traf_count));
363 if (traf == NULL) break;
364 AP4_TfhdAtom* tfhd = AP4_DYNAMIC_CAST(AP4_TfhdAtom, traf->GetChild(AP4_ATOM_TYPE_TFHD));
365 if (tfhd == NULL) {
366 fprintf(stderr, "ERROR: invalid media format\n");
367 return 1;
368 }
369 track_id = tfhd->GetTrackId();
370 traf_count++;
371 } while (traf);
372
373 // check if this fragment has more than one traf
374 if (traf_count > 1) {
375 if (Options.audio_only) {
376 fprintf(stderr, "ERROR: --audio option incompatible with multi-track fragments");
377 return 1;
378 }
379 if (Options.video_only) {
380 fprintf(stderr, "ERROR: --video option incompatible with multi-track fragments");
381 return 1;
382 }
383 track_id = 0;
384 }
385
386 // open a new file for this fragment if this moof is a segment start
387 char segment_name[4096];
388 if (Options.track_id_count == 0 || track_id == Options.track_ids[0]) {
389 if (output) {
390 output->Release();
391 output = NULL;
392 }
393
394 AP4_UI64 p[2] = {0,0};
395 unsigned int params_len = (unsigned int)strlen(Options.pattern_params);
396 for (unsigned int i=0; i<params_len; i++) {
397 if (Options.pattern_params[i] == 'I') {
398 p[i] = track_id;
399 } else if (Options.pattern_params[i] == 'N') {
400 p[i] = NextFragmentIndex(track_id)+Options.start_number;
401 }
402 }
403 switch (params_len) {
404 case 1:
405 sprintf(segment_name, Options.media_segment_name, p[0]);
406 break;
407 case 2:
408 sprintf(segment_name, Options.media_segment_name, p[0], p[1]);
409 break;
410 default:
411 segment_name[0] = 0;
412 break;
413 }
414 result = AP4_FileByteStream::Create(segment_name, AP4_FileByteStream::STREAM_MODE_WRITE, output);
415 if (AP4_FAILED(result)) {
416 fprintf(stderr, "ERROR: cannot open output file (%d)\n", result);
417 return 1;
418 }
419 }
420 }
421
422 // write the atom
423 if (output && atom->GetType() != AP4_ATOM_TYPE_MFRA && TrackIdMatches(track_id)) {
424 atom->Write(*output);
425 }
426 delete atom;
427 }
428
429 // cleanup
430 delete file;
431 if (input) input->Release();
432 if (output) output->Release();
433
434 return 0;
435 }
436