| 1 | /***************************************************************** |
| 2 | | |
| 3 | | AP4 - MP4 File Dumper |
| 4 | | |
| 5 | | Copyright 2002-2008 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 | #ifdef __EMSCRIPTEN__ |
| 38 | #include "emscripten.h" |
| 39 | #endif |
| 40 | |
| 41 | /*---------------------------------------------------------------------- |
| 42 | | constants |
| 43 | +---------------------------------------------------------------------*/ |
| 44 | #define BANNER "MP4 File Dumper - Version 1.2\n"\ |
| 45 | "(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 46 | "(c) 2002-2011 Axiomatic Systems, LLC" |
| 47 | |
| 48 | /*---------------------------------------------------------------------- |
| 49 | | PrintUsageAndExit |
| 50 | +---------------------------------------------------------------------*/ |
| 51 | static void |
| 52 | PrintUsageAndExit() |
| 53 | { |
| 54 | fprintf(stderr, |
| 55 | BANNER |
| 56 | "\n\nusage: mp4dump [options] <input>\n" |
| 57 | "options are:\n" |
| 58 | " --verbosity <n>\n" |
| 59 | " sets the verbosity (details) level to <n> (between 0 and 3)\n" |
| 60 | " --track <track_id>[:<key>]\n" |
| 61 | " writes the track data into a file\n" |
| 62 | " (<mp4filename>.<track_id>) and optionally\n" |
| 63 | " tries to decrypt it with the key (128-bit in hex)\n" |
| 64 | " (several --track options can be used, one for each track)\n" |
| 65 | " Each sample is written preceded by its size encoded as a 32-bit\n" |
| 66 | " value in big-endian byte order\n" |
| 67 | " --format <format>\n" |
| 68 | " format to use for the output, where <format> is either \n" |
| 69 | " 'text' (default) or 'json'\n"); |
| 70 | exit(1); |
| 71 | } |
| 72 | |
| 73 | /*---------------------------------------------------------------------- |
| 74 | | CreateTrackDumpByteStream |
| 75 | +---------------------------------------------------------------------*/ |
| 76 | static AP4_ByteStream* |
| 77 | CreateTrackDumpByteStream(const char* mp4_filename, |
| 78 | AP4_Ordinal track_id) |
| 79 | { |
| 80 | // create the output file name |
| 81 | AP4_Size mp4_filename_len = (AP4_Size)strlen(mp4_filename); |
| 82 | char* dump_filename = new char[mp4_filename_len+16]; // <filename>.<trackid> |
| 83 | strcpy(dump_filename, mp4_filename); |
| 84 | dump_filename[mp4_filename_len] = '.'; |
| 85 | sprintf(dump_filename+mp4_filename_len+1, "%d", track_id); |
| 86 | |
| 87 | // create a FileByteStream |
| 88 | AP4_ByteStream* output = NULL; |
| 89 | AP4_Result result = AP4_FileByteStream::Create(dump_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 90 | if (AP4_FAILED(result)) { |
| 91 | fprintf(stderr, "ERROR: %d cannot open file for dumping track %d", |
| 92 | result, track_id); |
| 93 | } |
| 94 | |
| 95 | delete [] dump_filename; |
| 96 | return output; |
| 97 | } |
| 98 | |
| 99 | /*---------------------------------------------------------------------- |
| 100 | | DumpSamples |
| 101 | +---------------------------------------------------------------------*/ |
| 102 | static void |
| 103 | DumpSamples(AP4_Track* track, AP4_ByteStream* dump) |
| 104 | { |
| 105 | // write the data |
| 106 | AP4_Sample sample; |
| 107 | AP4_DataBuffer sample_data; |
| 108 | AP4_Ordinal index = 0; |
| 109 | |
| 110 | while (AP4_SUCCEEDED(track->ReadSample(index, sample, sample_data))) { |
| 111 | // write the sample size |
| 112 | dump->WriteUI32(sample_data.GetDataSize()); |
| 113 | |
| 114 | // write the sample |
| 115 | dump->Write(sample_data.GetData(), sample_data.GetDataSize()); |
| 116 | index++; |
| 117 | |
| 118 | // print progress info |
| 119 | if (index%10 == 0) printf("."); |
| 120 | } |
| 121 | printf("\n"); |
| 122 | } |
| 123 | |
| 124 | /*---------------------------------------------------------------------- |
| 125 | | DecryptAndDumpSamples |
| 126 | +---------------------------------------------------------------------*/ |
| 127 | static void |
| 128 | DecryptAndDumpSamples(AP4_Track* track, |
| 129 | AP4_SampleDescription* sample_desc, |
| 130 | const AP4_UI08* key, |
| 131 | AP4_Size key_size, |
| 132 | AP4_ByteStream* dump) |
| 133 | { |
| 134 | AP4_ProtectedSampleDescription* pdesc = |
| 135 | AP4_DYNAMIC_CAST(AP4_ProtectedSampleDescription, sample_desc); |
| 136 | if (pdesc == NULL) { |
| 137 | fprintf(stderr, "ERROR: unable to obtain cipher info\n"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // create the decrypter |
| 142 | AP4_SampleDecrypter* decrypter = AP4_SampleDecrypter::Create(pdesc, key, key_size); |
| 143 | if (decrypter == NULL) { |
| 144 | fprintf(stderr, "ERROR: unable to create decrypter\n"); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | AP4_Sample sample; |
| 149 | AP4_DataBuffer encrypted_data; |
| 150 | AP4_DataBuffer decrypted_data; |
| 151 | AP4_Ordinal index = 0; |
| 152 | while (AP4_SUCCEEDED(track->ReadSample(index, sample, encrypted_data))) { |
| 153 | if (AP4_FAILED(decrypter->DecryptSampleData(encrypted_data, decrypted_data))) { |
| 154 | fprintf(stderr, "ERROR: failed to decrypt sample\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // write the sample size |
| 159 | dump->WriteUI32(decrypted_data.GetDataSize()); |
| 160 | |
| 161 | // write the sample |
| 162 | dump->Write(decrypted_data.GetData(), decrypted_data.GetDataSize()); |
| 163 | index++; |
| 164 | if (index%10 == 0) printf("."); |
| 165 | } |
| 166 | printf("\n"); |
| 167 | } |
| 168 | |
| 169 | /*---------------------------------------------------------------------- |
| 170 | | DumpTrackData |
| 171 | +---------------------------------------------------------------------*/ |
| 172 | void |
| 173 | DumpTrackData(const char* mp4_filename, |
| 174 | AP4_File& mp4_file, |
| 175 | const AP4_Array<AP4_Ordinal>& tracks_to_dump, |
| 176 | const AP4_ProtectionKeyMap& key_map) |
| 177 | { |
| 178 | // dump all the tracks that need to be dumped |
| 179 | AP4_Cardinal tracks_to_dump_count = tracks_to_dump.ItemCount(); |
| 180 | for (AP4_Ordinal i=0; i<tracks_to_dump_count; i++) { |
| 181 | // get the track |
| 182 | AP4_Ordinal track_id = tracks_to_dump[i]; |
| 183 | AP4_Track* track = mp4_file.GetMovie()->GetTrack(track_id); |
| 184 | if (track == NULL) { |
| 185 | fprintf(stderr, "track not found (id = %d)", track_id); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // get the sample description |
| 190 | AP4_SampleDescription* sample_description = track->GetSampleDescription(0); |
| 191 | if (sample_description == NULL) { |
| 192 | fprintf(stderr, "WARNING: unable to parse sample description\n"); |
| 193 | } |
| 194 | |
| 195 | // get the dump data byte stream |
| 196 | AP4_ByteStream* dump = CreateTrackDumpByteStream(mp4_filename, track_id); |
| 197 | if (dump == NULL) return; |
| 198 | |
| 199 | printf("\nDumping data for track %d:\n", track_id); |
| 200 | switch(sample_description ? |
| 201 | sample_description->GetType() : |
| 202 | AP4_SampleDescription::TYPE_UNKNOWN) { |
| 203 | case AP4_SampleDescription::TYPE_PROTECTED: |
| 204 | { |
| 205 | const AP4_DataBuffer* key = key_map.GetKey(track_id); |
| 206 | if (key == NULL) { |
| 207 | fprintf(stderr, |
| 208 | "WARNING: No key found for encrypted track %d... " |
| 209 | "dumping encrypted samples\n", |
| 210 | track_id); |
| 211 | DumpSamples(track, dump); |
| 212 | } else { |
| 213 | DecryptAndDumpSamples(track, sample_description, key->GetData(), key->GetDataSize(), dump); |
| 214 | } |
| 215 | } |
| 216 | break; |
| 217 | default: |
| 218 | DumpSamples(track, dump); |
| 219 | |
| 220 | } |
| 221 | dump->Release(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /*---------------------------------------------------------------------- |
| 226 | | main |
| 227 | +---------------------------------------------------------------------*/ |
| 228 | int |
| 229 | main(int argc, char** argv) |
| 230 | { |
| 231 | if (argc < 2) { |
| 232 | PrintUsageAndExit(); |
| 233 | } |
| 234 | |
| 235 | // init the variables |
| 236 | AP4_ByteStream* input = NULL; |
| 237 | const char* filename = NULL; |
| 238 | AP4_ProtectionKeyMap key_map; |
| 239 | AP4_Array<AP4_Ordinal> tracks_to_dump; |
| 240 | AP4_Ordinal verbosity = 0; |
| 241 | bool json_format = false; |
| 242 | |
| 243 | // parse the command line |
| 244 | argv++; |
| 245 | char* arg; |
| 246 | while ((arg = *argv++)) { |
| 247 | if (!strcmp(arg, "--track")) { |
| 248 | arg = *argv++; |
| 249 | if (arg == NULL) { |
| 250 | fprintf(stderr, "ERROR: missing argument after --track option\n"); |
| 251 | return 1; |
| 252 | } |
| 253 | char* track_ascii = arg; |
| 254 | char* key_ascii = NULL; |
| 255 | char* delimiter = strchr(arg, ':'); |
| 256 | if (delimiter != NULL) { |
| 257 | *delimiter = '\0'; |
| 258 | key_ascii = delimiter+1; |
| 259 | } |
| 260 | |
| 261 | // this track will be dumped |
| 262 | AP4_Ordinal track_id = (AP4_Ordinal) strtoul(track_ascii, NULL, 10); |
| 263 | tracks_to_dump.Append(track_id); |
| 264 | |
| 265 | // see if we have a key for this track |
| 266 | if (key_ascii != NULL) { |
| 267 | unsigned char key[16]; |
| 268 | if (AP4_ParseHex(key_ascii, key, 16)) { |
| 269 | fprintf(stderr, "ERROR: invalid hex format for key\n"); |
| 270 | return 1; |
| 271 | } |
| 272 | // set the key in the map |
| 273 | key_map.SetKey(track_id, key, 16); |
| 274 | } |
| 275 | } else if (!strcmp(arg, "--verbosity")) { |
| 276 | arg = *argv++; |
| 277 | if (arg == NULL) { |
| 278 | fprintf(stderr, "ERROR: missing argument after --verbosity option\n"); |
| 279 | return 1; |
| 280 | } |
| 281 | verbosity = (unsigned int)strtoul(arg, NULL, 10); |
| 282 | } else if (!strcmp(arg, "--format")) { |
| 283 | arg = *argv++; |
| 284 | if (arg == NULL) { |
| 285 | fprintf(stderr, "ERROR: missing argument after --format option\n"); |
| 286 | return 1; |
| 287 | } |
| 288 | if (strcmp(arg, "json") == 0) { |
| 289 | json_format = true; |
| 290 | } else if (strcmp(arg, "text")) { |
| 291 | fprintf(stderr, "ERROR: unknown output format\n"); |
| 292 | return 1; |
| 293 | } |
| 294 | } else { |
| 295 | #ifdef __EMSCRIPTEN__ |
| 296 | // Emscripten has to "mount" the filesystem to a virtual directory. |
| 297 | EM_ASM(FS.mkdir('/working')); |
| 298 | EM_ASM(FS.mount(NODEFS, { root: '.' }, '/working')); |
| 299 | const char* const mount_name = "/working/%s"; |
| 300 | const int filename_size = snprintf(NULL, 0, mount_name, arg); |
| 301 | char* mounted_filename = (char*) malloc(filename_size + 1); |
| 302 | sprintf(mounted_filename, mount_name, arg); |
| 303 | filename = mounted_filename; |
| 304 | #else |
| 305 | filename = arg; |
| 306 | #endif |
| 307 | AP4_Result result = AP4_FileByteStream::Create(filename, AP4_FileByteStream::STREAM_MODE_READ, input); |
| 308 | |
| 309 | #ifdef __EMSCRIPTEN__ |
| 310 | free(mounted_filename); |
| 311 | mounted_filename = NULL; |
| 312 | #endif |
| 313 | |
| 314 | if (AP4_FAILED(result)) { |
| 315 | fprintf(stderr, "ERROR: cannot open input (%d)\n", result); |
| 316 | return 1; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (input == NULL) { |
| 322 | fprintf(stderr, "ERROR: no input specified\n"); |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | // open the output |
| 327 | AP4_ByteStream* output = NULL; |
| 328 | AP4_FileByteStream::Create("-stdout", AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 329 | |
| 330 | // create an inspector |
| 331 | AP4_AtomInspector* inspector = NULL; |
| 332 | if (json_format) { |
| 333 | inspector = new AP4_JsonInspector(*output); |
| 334 | } else { |
| 335 | inspector = new AP4_PrintInspector(*output); |
| 336 | } |
| 337 | inspector->SetVerbosity(verbosity); |
| 338 | |
| 339 | // inspect the atoms one by one |
| 340 | AP4_Atom* atom; |
| 341 | AP4_DefaultAtomFactory atom_factory; |
| 342 | while (atom_factory.CreateAtomFromStream(*input, atom) == AP4_SUCCESS) { |
| 343 | // remember the current stream position because the Inspect method |
| 344 | // may read from the stream (there may be stream references in some |
| 345 | // of the atoms |
| 346 | AP4_Position position; |
| 347 | input->Tell(position); |
| 348 | |
| 349 | // inspect the atom |
| 350 | atom->Inspect(*inspector); |
| 351 | |
| 352 | // restore the previous stream position |
| 353 | input->Seek(position); |
| 354 | |
| 355 | // destroy the atom |
| 356 | delete atom; |
| 357 | } |
| 358 | if (output) output->Release(); |
| 359 | |
| 360 | // inspect the track data if needed |
| 361 | if (tracks_to_dump.ItemCount() != 0) { |
| 362 | // rewind |
| 363 | input->Seek(0); |
| 364 | |
| 365 | // dump the track data |
| 366 | AP4_File file(*input); |
| 367 | DumpTrackData(filename, file, tracks_to_dump, key_map); |
| 368 | } |
| 369 | |
| 370 | if (input) input->Release(); |
| 371 | delete inspector; |
| 372 | |
| 373 | return 0; |
| 374 | } |