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