| 1 |
/***************************************************************** |
| 2 |
| |
| 3 |
| AP4 - MP4 File Tagger |
| 4 |
| |
| 5 |
| Copyright 2002-2009 Axiomatic Systems, LLC Boccon-Gibod |
| 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 File Tagger - Version 1.2 "\ |
| 41 |
"(Bento4 Version " AP4_VERSION_STRING ")\n"\ |
| 42 |
"(c) 2002-2008 Axiomatic Systems, LLC" |
| 43 |
|
| 44 |
/*---------------------------------------------------------------------- |
| 45 |
| types |
| 46 |
+---------------------------------------------------------------------*/ |
| 47 |
class Command |
| 48 |
{ |
| 49 |
public: |
| 50 |
// types |
| 51 |
typedef enum { |
| 52 |
TYPE_SHOW_TAGS, |
| 53 |
TYPE_LIST_KEYS, |
| 54 |
TYPE_LIST_SYMBOLS, |
| 55 |
TYPE_SET, |
| 56 |
TYPE_ADD, |
| 57 |
TYPE_REMOVE, |
| 58 |
TYPE_EXTRACT |
| 59 |
} Type; |
| 60 |
|
| 61 |
// constructors |
| 62 |
Command(Type type, |
| 63 |
const char* arg1 = NULL, |
| 64 |
const char* arg2 = NULL) : |
| 65 |
m_Type(type), |
| 66 |
m_Arg1(arg1), |
| 67 |
m_Arg2(arg2) {} |
| 68 |
|
| 69 |
// members |
| 70 |
Type m_Type; |
| 71 |
AP4_String m_Arg1; |
| 72 |
AP4_String m_Arg2; |
| 73 |
}; |
| 74 |
|
| 75 |
typedef struct { |
| 76 |
const char* label; |
| 77 |
unsigned int width; |
| 78 |
} TableCell; |
| 79 |
|
| 80 |
/*---------------------------------------------------------------------- |
| 81 |
| options |
| 82 |
+---------------------------------------------------------------------*/ |
| 83 |
static struct { |
| 84 |
const char* input_filename; |
| 85 |
const char* output_filename; |
| 86 |
AP4_List<Command> commands; |
| 87 |
bool need_input; |
| 88 |
bool need_output; |
| 89 |
} Options; |
| 90 |
|
| 91 |
static const int LINE_WIDTH = 79; |
| 92 |
|
| 93 |
/*---------------------------------------------------------------------- |
| 94 |
| PrintUsageAndExit |
| 95 |
+---------------------------------------------------------------------*/ |
| 96 |
static void |
| 97 |
PrintUsageAndExit() |
| 98 |
{ |
| 99 |
fprintf(stderr, |
| 100 |
BANNER |
| 101 |
"\n\nusage: mp4tag [options] [commands...] <input> [<output>]\n" |
| 102 |
"commands:\n" |
| 103 |
" --help print this usage information\n" |
| 104 |
" --show-tags show tags found in the input file\n" |
| 105 |
" --list-symbols list all the builtin symbols\n" |
| 106 |
" --list-keys list all the builtin symbolic key names\n" |
| 107 |
" --set <key>:<type>:<value>\n" |
| 108 |
" set a tag (if the tag does not already exist, set behaves like add)\n" |
| 109 |
" --add <key>:<type>:<value>\n" |
| 110 |
" set/add a tag\n" |
| 111 |
" where <type> is:\n" |
| 112 |
" S if <value> is a UTF-8 string\n" |
| 113 |
" LS if <value> is a UTF-8 string with a language code (see notes)\n" |
| 114 |
" I8 if <value> is an 8-bit integer\n" |
| 115 |
" I16 if <value> is a 16-bit integer\n" |
| 116 |
" I32 if <value> is a 32-bit integer\n" |
| 117 |
" JPEG if <value> is the name of a JPEG file\n" |
| 118 |
" GIF if <value> is the name of a GIF file\n" |
| 119 |
" B if <value> is a binary string (see notes)\n" |
| 120 |
" Z if <value> is the name of a builtin symbol\n" |
| 121 |
" --remove <key>\n" |
| 122 |
" remove a tag\n" |
| 123 |
" --extract <key>:<file>\n" |
| 124 |
" extract the value of a tag and save it to a file\n" |
| 125 |
"\n" |
| 126 |
"NOTES:\n" |
| 127 |
" In all commands with a <key> argument, except for '--add', <key> can be \n" |
| 128 |
" <key-name> or <key-name>#n where n is the zero-based index of the key when\n" |
| 129 |
" there is more than one key with the same name (ex: multiple images for cover\n" |
| 130 |
" art).\n" |
| 131 |
" A <key-name> has the form <namespace>/<name> or simply <name> (in which case\n" |
| 132 |
" the namespace defaults to 'meta').\n" |
| 133 |
" The <name> of a key is either one of a symbolic keys\n" |
| 134 |
" (see --list-keys) or a 4-character atom name.\n" |
| 135 |
" The namespace can be 'meta' for itunes-style metadata or 'dcf' for\n" |
| 136 |
" OMA-DCF-style metadata, or a user-defined long-form namespace\n" |
| 137 |
" (ex: com.mycompany.foo).\n" |
| 138 |
" Binary strings can be expressed as a normal string of ASCII characters\n" |
| 139 |
" prefixed by a + character if all the bytes fall in the ASCII range,\n" |
| 140 |
" or hex-encoded prefixed by a # character (ex: +hello, or #0FC4)\n" |
| 141 |
" Strings with a language code are expressed as: <lang>:<string>,\n" |
| 142 |
" where <lang> is a 3 character language code (ex: eng:hello)\n"); |
| 143 |
exit(1); |
| 144 |
} |
| 145 |
|
| 146 |
/*---------------------------------------------------------------------- |
| 147 |
| ParseCommandLine |
| 148 |
+---------------------------------------------------------------------*/ |
| 149 |
static void |
| 150 |
ParseCommandLine(int argc, char** argv) |
| 151 |
{ |
| 152 |
for (int i=0; i<argc; i++) { |
| 153 |
if (AP4_CompareStrings("--help", argv[i]) == 0) { |
| 154 |
PrintUsageAndExit(); |
| 155 |
} else if (AP4_CompareStrings("--show-tags", argv[i]) == 0) { |
| 156 |
Options.commands.Add(new Command(Command::TYPE_SHOW_TAGS)); |
| 157 |
Options.need_input = true; |
| 158 |
} else if (AP4_CompareStrings("--list-keys", argv[i]) == 0) { |
| 159 |
Options.commands.Add(new Command(Command::TYPE_LIST_KEYS)); |
| 160 |
} else if (AP4_CompareStrings("--list-symbols", argv[i]) == 0) { |
| 161 |
Options.commands.Add(new Command(Command::TYPE_LIST_SYMBOLS)); |
| 162 |
} else if (AP4_CompareStrings("--add", argv[i]) == 0) { |
| 163 |
if (i == argc-1) { |
| 164 |
fprintf(stderr, "ERROR: missing argument after --add command"); |
| 165 |
PrintUsageAndExit(); |
| 166 |
} |
| 167 |
Options.commands.Add(new Command(Command::TYPE_ADD, argv[++i])); |
| 168 |
Options.need_input = true; |
| 169 |
Options.need_output = true; |
| 170 |
} else if (AP4_CompareStrings("--set", argv[i]) == 0) { |
| 171 |
if (i == argc-1) { |
| 172 |
fprintf(stderr, "ERROR: missing argument after --set command"); |
| 173 |
PrintUsageAndExit(); |
| 174 |
} |
| 175 |
Options.commands.Add(new Command(Command::TYPE_SET, argv[++i])); |
| 176 |
Options.need_input = true; |
| 177 |
Options.need_output = true; |
| 178 |
} else if (AP4_CompareStrings("--remove", argv[i]) == 0) { |
| 179 |
if (i == argc-1) { |
| 180 |
fprintf(stderr, "ERROR: missing argument after --remove command"); |
| 181 |
PrintUsageAndExit(); |
| 182 |
} |
| 183 |
Options.commands.Add(new Command(Command::TYPE_REMOVE, argv[++i])); |
| 184 |
Options.need_input = true; |
| 185 |
Options.need_output = true; |
| 186 |
} else if (AP4_CompareStrings("--extract", argv[i]) == 0) { |
| 187 |
if (i == argc-1) { |
| 188 |
fprintf(stderr, "ERROR: missing argument after --extract command"); |
| 189 |
PrintUsageAndExit(); |
| 190 |
} |
| 191 |
Options.commands.Add(new Command(Command::TYPE_EXTRACT, argv[++i])); |
| 192 |
Options.need_input = true; |
| 193 |
} else { |
| 194 |
if (Options.input_filename == NULL) { |
| 195 |
Options.input_filename = argv[i]; |
| 196 |
} else if (Options.output_filename == NULL) { |
| 197 |
Options.output_filename = argv[i]; |
| 198 |
} else { |
| 199 |
fprintf(stderr, "ERROR: unpexpected option \"%s\"\n", argv[i]); |
| 200 |
PrintUsageAndExit(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (Options.commands.ItemCount() == 0) { |
| 206 |
if (Options.input_filename) { |
| 207 |
Options.commands.Add(new Command(Command::TYPE_SHOW_TAGS)); |
| 208 |
Options.need_input = true; |
| 209 |
} else { |
| 210 |
PrintUsageAndExit(); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/*---------------------------------------------------------------------- |
| 216 |
| PrintTableSeparator |
| 217 |
+---------------------------------------------------------------------*/ |
| 218 |
static void |
| 219 |
PrintTableSeparator(TableCell* cells, AP4_Cardinal cell_count) |
| 220 |
{ |
| 221 |
while (cell_count--) { |
| 222 |
printf("+"); |
| 223 |
for (unsigned int i=0; i<cells->width; i++) { |
| 224 |
printf("-"); |
| 225 |
} |
| 226 |
cells++; |
| 227 |
} |
| 228 |
printf("+\n"); |
| 229 |
} |
| 230 |
|
| 231 |
/*---------------------------------------------------------------------- |
| 232 |
| PrintTableRow |
| 233 |
+---------------------------------------------------------------------*/ |
| 234 |
static void |
| 235 |
PrintTableRow(TableCell* cells, AP4_Cardinal cell_count) |
| 236 |
{ |
| 237 |
while (cell_count--) { |
| 238 |
printf("|"); |
| 239 |
printf("%s", cells->label); |
| 240 |
for (unsigned int x=(unsigned int)AP4_StringLength(cells->label); x<cells->width; x++) { |
| 241 |
printf(" "); |
| 242 |
} |
| 243 |
cells++; |
| 244 |
} |
| 245 |
printf("|\n"); |
| 246 |
} |
| 247 |
|
| 248 |
/*---------------------------------------------------------------------- |
| 249 |
| TypeCode |
| 250 |
+---------------------------------------------------------------------*/ |
| 251 |
static const char* |
| 252 |
TypeCode(AP4_MetaData::Value::Type type) { |
| 253 |
switch (type) { |
| 254 |
case AP4_MetaData::Value::TYPE_BINARY: return "B"; |
| 255 |
case AP4_MetaData::Value::TYPE_STRING_UTF_8: return "S"; |
| 256 |
case AP4_MetaData::Value::TYPE_STRING_UTF_16: return "W"; |
| 257 |
case AP4_MetaData::Value::TYPE_INT_08_BE: return "I8"; |
| 258 |
case AP4_MetaData::Value::TYPE_INT_16_BE: return "I16"; |
| 259 |
case AP4_MetaData::Value::TYPE_INT_32_BE: return "I32"; |
| 260 |
case AP4_MetaData::Value::TYPE_FLOAT_32_BE: return "F32"; |
| 261 |
case AP4_MetaData::Value::TYPE_FLOAT_64_BE: return "F64"; |
| 262 |
case AP4_MetaData::Value::TYPE_JPEG: return "JPEG"; |
| 263 |
case AP4_MetaData::Value::TYPE_GIF: return "GIF"; |
| 264 |
default: return "?"; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/*---------------------------------------------------------------------- |
| 269 |
| MeaningCode |
| 270 |
+---------------------------------------------------------------------*/ |
| 271 |
static const char* |
| 272 |
MeaningCode(AP4_MetaData::Value::Meaning meaning) { |
| 273 |
switch (meaning) { |
| 274 |
case AP4_MetaData::Value::MEANING_BOOLEAN: return "/Bool"; |
| 275 |
case AP4_MetaData::Value::MEANING_ID3_GENRE: return "/ID3"; |
| 276 |
default: return ""; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/*---------------------------------------------------------------------- |
| 281 |
| ListKeys |
| 282 |
+---------------------------------------------------------------------*/ |
| 283 |
static void |
| 284 |
ListKeys() |
| 285 |
{ |
| 286 |
// measure the widest key |
| 287 |
unsigned int c1_width = 0; |
| 288 |
for (AP4_Ordinal i=0; i<AP4_MetaData::KeyInfos.ItemCount(); i++) { |
| 289 |
AP4_MetaData::KeyInfo& key = AP4_MetaData::KeyInfos[i]; |
| 290 |
unsigned int key_width = (unsigned int)AP4_StringLength(key.name); |
| 291 |
if (key_width > c1_width) c1_width = key_width; |
| 292 |
} |
| 293 |
c1_width += 2; // account for padding |
| 294 |
unsigned int c3_width = 4; |
| 295 |
unsigned int c2_width = LINE_WIDTH-c1_width-c3_width-1-8; |
| 296 |
TableCell cells[4] = { |
| 297 |
{" Key", c1_width}, |
| 298 |
{" Description", c2_width}, |
| 299 |
{" 4CC", 4 }, |
| 300 |
{"Type", 4 } |
| 301 |
}; |
| 302 |
PrintTableSeparator(cells, 4); |
| 303 |
PrintTableRow(cells, 4); |
| 304 |
PrintTableSeparator(cells, 4); |
| 305 |
|
| 306 |
char four_cc[5]; |
| 307 |
for (AP4_Ordinal i=0; i<AP4_MetaData::KeyInfos.ItemCount(); i++) { |
| 308 |
AP4_MetaData::KeyInfo& key = AP4_MetaData::KeyInfos[i]; |
| 309 |
AP4_FormatFourCharsPrintable(four_cc, key.four_cc); |
| 310 |
cells[0].label = key.name; |
| 311 |
cells[1].label = key.description; |
| 312 |
cells[2].label = four_cc; |
| 313 |
cells[3].label = TypeCode(key.value_type); |
| 314 |
PrintTableRow(cells, 4); |
| 315 |
} |
| 316 |
PrintTableSeparator(cells, 4); |
| 317 |
|
| 318 |
printf("\nDCF Special 4CC types:\n"); |
| 319 |
printf("dcfD\n\n"); |
| 320 |
printf("DCF String 4CC types:\n"); |
| 321 |
const char* sep = ""; |
| 322 |
for (unsigned int i=0; i<AP4_MetaDataAtomTypeHandler::DcfStringTypeList.m_Size; i++) { |
| 323 |
AP4_FormatFourCharsPrintable(four_cc, AP4_MetaDataAtomTypeHandler::DcfStringTypeList.m_Types[i]); |
| 324 |
printf("%s%s", sep, four_cc); |
| 325 |
sep = ", "; |
| 326 |
} |
| 327 |
printf("\n\n3GPP Localized String 4CC types:\n"); |
| 328 |
sep = ""; |
| 329 |
for (unsigned int i=0; i<AP4_MetaDataAtomTypeHandler::_3gppLocalizedStringTypeList.m_Size; i++) { |
| 330 |
AP4_FormatFourCharsPrintable(four_cc, AP4_MetaDataAtomTypeHandler::_3gppLocalizedStringTypeList.m_Types[i]); |
| 331 |
printf("%s%s", sep, four_cc); |
| 332 |
sep = ", "; |
| 333 |
} |
| 334 |
printf("\n\n3GPP Other 4CC types:\n"); |
| 335 |
sep = ""; |
| 336 |
for (unsigned int i=0; i<AP4_MetaDataAtomTypeHandler::_3gppOtherTypeList.m_Size; i++) { |
| 337 |
AP4_FormatFourCharsPrintable(four_cc, AP4_MetaDataAtomTypeHandler::_3gppOtherTypeList.m_Types[i]); |
| 338 |
printf("%s%s", sep, four_cc); |
| 339 |
sep = ", "; |
| 340 |
} |
| 341 |
printf("\n"); |
| 342 |
} |
| 343 |
|
| 344 |
/*---------------------------------------------------------------------- |
| 345 |
| ListSymbols |
| 346 |
+---------------------------------------------------------------------*/ |
| 347 |
static void |
| 348 |
ListSymbols() |
| 349 |
{ |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
/*---------------------------------------------------------------------- |
| 354 |
| ShowTag |
| 355 |
+---------------------------------------------------------------------*/ |
| 356 |
static void |
| 357 |
ShowTag(AP4_MetaData::Entry* entry) |
| 358 |
{ |
| 359 |
// look for a match in the key infos |
| 360 |
const char* key_name = entry->m_Key.GetName().GetChars(); |
| 361 |
bool show_namespace = true; |
| 362 |
AP4_Atom::Type atom_type = AP4_Atom::TypeFromString(entry->m_Key.GetName().GetChars()); |
| 363 |
for (unsigned int i=0; i<AP4_MetaData::KeyInfos.ItemCount(); ++i) { |
| 364 |
if (AP4_MetaData::KeyInfos[i].four_cc == atom_type) { |
| 365 |
key_name = AP4_MetaData::KeyInfos[i].name; |
| 366 |
show_namespace = false; |
| 367 |
break; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
// print the key namespace unless we have a symbolic name |
| 372 |
if (show_namespace) { |
| 373 |
printf("%s/", entry->m_Key.GetNamespace().GetChars()); |
| 374 |
} |
| 375 |
|
| 376 |
// print the key name |
| 377 |
printf("%s:", key_name); |
| 378 |
|
| 379 |
// print the value type |
| 380 |
printf("[%s%s]", |
| 381 |
TypeCode(entry->m_Value->GetType()), |
| 382 |
MeaningCode(entry->m_Value->GetMeaning())); |
| 383 |
|
| 384 |
// print the language, unless it is not set |
| 385 |
if (entry->m_Value->GetLanguage().GetLength()) { |
| 386 |
printf(" (%s)", entry->m_Value->GetLanguage().GetChars()); |
| 387 |
} |
| 388 |
|
| 389 |
// print the value |
| 390 |
printf(" %s", entry->m_Value->ToString().GetChars()); |
| 391 |
|
| 392 |
printf("\n"); |
| 393 |
} |
| 394 |
|
| 395 |
/*---------------------------------------------------------------------- |
| 396 |
| ShowTags |
| 397 |
+---------------------------------------------------------------------*/ |
| 398 |
static void |
| 399 |
ShowTags(AP4_File* file) |
| 400 |
{ |
| 401 |
const AP4_MetaData* metadata = file->GetMetaData(); |
| 402 |
|
| 403 |
// iterator over the entries |
| 404 |
AP4_List<AP4_MetaData::Entry>::Item* item = metadata->GetEntries().FirstItem(); |
| 405 |
while (item) { |
| 406 |
AP4_MetaData::Entry* entry = item->GetData(); |
| 407 |
ShowTag(entry); |
| 408 |
item = item->GetNext(); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/*---------------------------------------------------------------------- |
| 413 |
| SplitString |
| 414 |
+---------------------------------------------------------------------*/ |
| 415 |
static AP4_Result |
| 416 |
SplitString(const AP4_String& arg, |
| 417 |
char separator, |
| 418 |
AP4_List<AP4_String>& components, |
| 419 |
unsigned int count) |
| 420 |
{ |
| 421 |
AP4_String* component; |
| 422 |
int start=0, end; |
| 423 |
for (unsigned int i=0; i<count; i++) { |
| 424 |
end = i==count-1?(int)arg.GetLength():arg.Find(separator, start); |
| 425 |
if (end == -1) return AP4_FAILURE; |
| 426 |
component = new AP4_String(); |
| 427 |
component->Assign(arg.GetChars()+start, end-start); |
| 428 |
components.Add(component); |
| 429 |
start = end+1; |
| 430 |
} |
| 431 |
|
| 432 |
return AP4_SUCCESS; |
| 433 |
} |
| 434 |
|
| 435 |
/*---------------------------------------------------------------------- |
| 436 |
| ParseKeySpec |
| 437 |
+---------------------------------------------------------------------*/ |
| 438 |
static AP4_Result |
| 439 |
ParseKeySpec(AP4_String& key, |
| 440 |
AP4_String*& key_namespace, |
| 441 |
AP4_String*& key_name, |
| 442 |
AP4_Ordinal& key_index) |
| 443 |
{ |
| 444 |
key_namespace = NULL; |
| 445 |
key_name = NULL; |
| 446 |
key_index = 0; |
| 447 |
|
| 448 |
// deal with the key index specifier |
| 449 |
AP4_String processed_key(key); |
| 450 |
for (int i=key.GetLength()-1; i>=0; --i) { |
| 451 |
if (key[i] == '#') { |
| 452 |
processed_key.Assign(key.GetChars(), i); |
| 453 |
key_index = (AP4_Ordinal)strtoul(key.GetChars()+i+1, NULL, 10); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if (processed_key.Find('/') != -1) { |
| 458 |
// the key is in the form namespace/name |
| 459 |
AP4_List<AP4_String> key_components; |
| 460 |
SplitString(processed_key, '/', key_components, 2); |
| 461 |
key_components.Get(0, key_namespace); |
| 462 |
key_components.Get(1, key_name); |
| 463 |
} else { |
| 464 |
// assume the namespace is 'meta' |
| 465 |
key_namespace = new AP4_String("meta"); |
| 466 |
key_name = new AP4_String(processed_key); |
| 467 |
} |
| 468 |
|
| 469 |
// look for the name in the list of keys |
| 470 |
for (AP4_Ordinal i=0; i<AP4_MetaData::KeyInfos.ItemCount(); i++) { |
| 471 |
AP4_MetaData::KeyInfo& key_info = AP4_MetaData::KeyInfos[i]; |
| 472 |
if (*key_name == key_info.name) { |
| 473 |
char four_cc[5]; |
| 474 |
four_cc[0] = key_info.four_cc>>24; |
| 475 |
four_cc[1] = key_info.four_cc>>16; |
| 476 |
four_cc[2] = key_info.four_cc>> 8; |
| 477 |
four_cc[3] = key_info.four_cc ; |
| 478 |
four_cc[4] = '\0'; |
| 479 |
delete key_name; |
| 480 |
key_name = new AP4_String(four_cc); |
| 481 |
break; |
| 482 |
} |
| 483 |
} |
| 484 |
if (key_name == NULL) { |
| 485 |
fprintf(stderr, "ERROR: unknown key\n"); |
| 486 |
delete key_namespace; |
| 487 |
key_namespace = NULL; |
| 488 |
return AP4_FAILURE; |
| 489 |
} |
| 490 |
return AP4_SUCCESS; |
| 491 |
} |
| 492 |
|
| 493 |
/*---------------------------------------------------------------------- |
| 494 |
| GetNibble |
| 495 |
+---------------------------------------------------------------------*/ |
| 496 |
static unsigned int |
| 497 |
GetNibble(char c) |
| 498 |
{ |
| 499 |
switch (c) { |
| 500 |
case '0': return 0; |
| 501 |
case '1': return 1; |
| 502 |
case '2': return 2; |
| 503 |
case '3': return 3; |
| 504 |
case '4': return 4; |
| 505 |
case '5': return 5; |
| 506 |
case '6': return 6; |
| 507 |
case '7': return 7; |
| 508 |
case '8': return 8; |
| 509 |
case '9': return 9; |
| 510 |
case 'a': |
| 511 |
case 'A': return 0xA; |
| 512 |
case 'b': |
| 513 |
case 'B': return 0xB; |
| 514 |
case 'c': |
| 515 |
case 'C': return 0xC; |
| 516 |
case 'd': |
| 517 |
case 'D': return 0xD; |
| 518 |
case 'e': |
| 519 |
case 'E': return 0xE; |
| 520 |
case 'f': |
| 521 |
case 'F': return 0xF; |
| 522 |
default: return 0; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/*---------------------------------------------------------------------- |
| 527 |
| RemoveTag |
| 528 |
+---------------------------------------------------------------------*/ |
| 529 |
static AP4_Result |
| 530 |
RemoveTag(AP4_File* file, AP4_String& key, bool silent) |
| 531 |
{ |
| 532 |
AP4_String* key_namespace; |
| 533 |
AP4_String* key_name; |
| 534 |
AP4_Ordinal key_index; |
| 535 |
AP4_Result result = ParseKeySpec(key, key_namespace, key_name, key_index); |
| 536 |
if (AP4_FAILED(result)) { |
| 537 |
fprintf(stderr, "ERROR: invalid key name (%s)\n", key.GetChars()); |
| 538 |
return AP4_ERROR_INVALID_PARAMETERS; |
| 539 |
} |
| 540 |
|
| 541 |
AP4_MetaData::Entry entry(key_name->GetChars(), key_namespace->GetChars(), NULL); |
| 542 |
result = entry.RemoveFromFile(*file, key_index); |
| 543 |
if (AP4_FAILED(result) && !silent) { |
| 544 |
fprintf(stderr, "ERROR: failed to remove entry (%d)\n", result); |
| 545 |
} |
| 546 |
|
| 547 |
delete key_namespace; |
| 548 |
delete key_name; |
| 549 |
|
| 550 |
return result; |
| 551 |
} |
| 552 |
|
| 553 |
/*---------------------------------------------------------------------- |
| 554 |
| AddTag |
| 555 |
+---------------------------------------------------------------------*/ |
| 556 |
static AP4_Result |
| 557 |
AddTag(AP4_File* file, AP4_String& arg, bool remove_first) |
| 558 |
{ |
| 559 |
AP4_Result result; |
| 560 |
|
| 561 |
AP4_List<AP4_String> arg_components; |
| 562 |
result = SplitString(arg, ':', arg_components, 3); |
| 563 |
if (AP4_FAILED(result)) { |
| 564 |
fprintf(stderr, "ERROR: invalid argument syntax for --add command\n"); |
| 565 |
return result; |
| 566 |
} |
| 567 |
|
| 568 |
// split the components |
| 569 |
AP4_String* key = NULL; |
| 570 |
AP4_String* type = NULL; |
| 571 |
AP4_String* value = NULL; |
| 572 |
arg_components.Get(0, key); |
| 573 |
arg_components.Get(1, type); |
| 574 |
arg_components.Get(2, value); |
| 575 |
|
| 576 |
AP4_String* key_namespace = NULL; |
| 577 |
AP4_String* key_name = NULL; |
| 578 |
AP4_Ordinal key_index = 0; |
| 579 |
result = ParseKeySpec(*key, key_namespace, key_name, key_index); |
| 580 |
if (AP4_FAILED(result)) { |
| 581 |
fprintf(stderr, "ERROR: invalid key name (%s)\n", key->GetChars()); |
| 582 |
delete key; |
| 583 |
delete type; |
| 584 |
delete value; |
| 585 |
return result; |
| 586 |
} |
| 587 |
|
| 588 |
// remove the entry first if necessary |
| 589 |
if (remove_first) { |
| 590 |
RemoveTag(file, *key, true); |
| 591 |
} |
| 592 |
|
| 593 |
// create an entry |
| 594 |
AP4_MetaData::Value* vobj = NULL; |
| 595 |
if (*type == "S") { |
| 596 |
vobj = new AP4_StringMetaDataValue(value->GetChars()); |
| 597 |
} else if (*type == "LS") { |
| 598 |
char lang[4]; |
| 599 |
if (value->GetLength() < 4 || (*value)[3] != ':') { |
| 600 |
fprintf(stderr, "ERROR: invalid syntax for LS value\n"); |
| 601 |
result = AP4_ERROR_INVALID_FORMAT; |
| 602 |
goto end; |
| 603 |
} |
| 604 |
lang[0] = (*value)[0]; |
| 605 |
lang[1] = (*value)[1]; |
| 606 |
lang[2] = (*value)[2]; |
| 607 |
lang[3] = 0; |
| 608 |
vobj = new AP4_StringMetaDataValue(value->GetChars()+4); |
| 609 |
vobj->SetLanguage(lang); |
| 610 |
} else if (*type == "I8") { |
| 611 |
long v = strtol(value->GetChars(), NULL, 10); |
| 612 |
vobj = new AP4_IntegerMetaDataValue(AP4_MetaData::Value::TYPE_INT_08_BE, v); |
| 613 |
} else if (*type == "I16") { |
| 614 |
long v = strtol(value->GetChars(), NULL, 10); |
| 615 |
vobj = new AP4_IntegerMetaDataValue(AP4_MetaData::Value::TYPE_INT_16_BE, v); |
| 616 |
} else if (*type == "I32") { |
| 617 |
long v = strtol(value->GetChars(), NULL, 10); |
| 618 |
vobj = new AP4_IntegerMetaDataValue(AP4_MetaData::Value::TYPE_INT_32_BE, v); |
| 619 |
} else if (*type == "JPEG" || *type == "GIF") { |
| 620 |
AP4_MetaData::Value::Type data_type = |
| 621 |
(*type == "JPEG" ? AP4_MetaData::Value::TYPE_JPEG : AP4_MetaData::Value::TYPE_GIF); |
| 622 |
AP4_ByteStream* data_file = NULL; |
| 623 |
result = AP4_FileByteStream::Create(value->GetChars(), AP4_FileByteStream::STREAM_MODE_READ, data_file); |
| 624 |
if (AP4_FAILED(result)) { |
| 625 |
fprintf(stderr, "ERROR: cannot open file %s\n", value->GetChars()); |
| 626 |
goto end; |
| 627 |
} |
| 628 |
AP4_DataBuffer buffer; |
| 629 |
AP4_LargeSize data_size; |
| 630 |
data_file->GetSize(data_size); |
| 631 |
buffer.SetDataSize((AP4_Size)data_size); |
| 632 |
data_file->Read(buffer.UseData(), (AP4_Size)data_size); |
| 633 |
vobj = new AP4_BinaryMetaDataValue(data_type, buffer.GetData(), buffer.GetDataSize()); |
| 634 |
if (data_file) data_file->Release(); |
| 635 |
} else if (*type == "B") { |
| 636 |
if (value->GetLength() == 0) { |
| 637 |
fprintf(stderr, "ERROR: invalid binary encoding\n"); |
| 638 |
result = AP4_ERROR_INVALID_PARAMETERS; |
| 639 |
goto end; |
| 640 |
} else if (value->GetChars()[0] == '+') { |
| 641 |
vobj = new AP4_BinaryMetaDataValue(AP4_MetaData::Value::TYPE_BINARY, |
| 642 |
(const AP4_UI08*)value->GetChars()+1, |
| 643 |
value->GetLength()-1); |
| 644 |
} else if (value->GetChars()[0] == '#') { |
| 645 |
if (((value->GetLength()-1)%2) != 0) { |
| 646 |
fprintf(stderr, "ERROR: invalid hex encoding\n"); |
| 647 |
result = AP4_ERROR_INVALID_PARAMETERS; |
| 648 |
goto end; |
| 649 |
} else { |
| 650 |
AP4_Size binary_string_length = (value->GetLength()-1)/2; |
| 651 |
AP4_UI08* binary_string = new AP4_UI08[binary_string_length]; |
| 652 |
for (unsigned int i=0; i<binary_string_length; i++) { |
| 653 |
unsigned int nibble_0 = GetNibble(value->GetChars()[1+i*2 ]); |
| 654 |
unsigned int nibble_1 = GetNibble(value->GetChars()[1+i*2+1]); |
| 655 |
binary_string[i] = (nibble_0<<4)|nibble_1; |
| 656 |
} |
| 657 |
vobj = new AP4_BinaryMetaDataValue(AP4_MetaData::Value::TYPE_BINARY, |
| 658 |
binary_string, |
| 659 |
binary_string_length); |
| 660 |
} |
| 661 |
} else { |
| 662 |
fprintf(stderr, "ERROR: invalid binary string (does not start with + or #)\n"); |
| 663 |
result = AP4_ERROR_INVALID_PARAMETERS; |
| 664 |
goto end; |
| 665 |
} |
| 666 |
} else { |
| 667 |
fprintf(stderr, "ERROR: unsupported type '%s'\n", type->GetChars()); |
| 668 |
result = AP4_ERROR_INVALID_PARAMETERS; |
| 669 |
goto end; |
| 670 |
} |
| 671 |
{ |
| 672 |
AP4_MetaData::Entry entry(key_name->GetChars(), key_namespace->GetChars(), vobj); |
| 673 |
|
| 674 |
// add the entry to the file |
| 675 |
result = entry.AddToFile(*file, key_index); |
| 676 |
if (AP4_FAILED(result)) { |
| 677 |
fprintf(stderr, |
| 678 |
"ERROR: cannot add entry %s (%d:%s)\n", |
| 679 |
key->GetChars(), |
| 680 |
result, AP4_ResultText(result)); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
end: |
| 685 |
delete key; |
| 686 |
delete key_namespace; |
| 687 |
delete key_name; |
| 688 |
delete type; |
| 689 |
delete value; |
| 690 |
|
| 691 |
return result; |
| 692 |
} |
| 693 |
|
| 694 |
/*---------------------------------------------------------------------- |
| 695 |
| ExtractTag |
| 696 |
+---------------------------------------------------------------------*/ |
| 697 |
static AP4_Result |
| 698 |
ExtractTag(AP4_File* file, AP4_String& arg) |
| 699 |
{ |
| 700 |
AP4_List<AP4_String> arg_components; |
| 701 |
AP4_Result result = SplitString(arg, ':', arg_components, 2); |
| 702 |
if (AP4_FAILED(result)) { |
| 703 |
fprintf(stderr, "ERROR: invalid argument syntax for --extract command\n"); |
| 704 |
return result; |
| 705 |
} |
| 706 |
|
| 707 |
// split the components |
| 708 |
AP4_String* key = NULL; |
| 709 |
AP4_String* output_filename = NULL; |
| 710 |
arg_components.Get(0, key); |
| 711 |
arg_components.Get(1, output_filename); |
| 712 |
|
| 713 |
AP4_String* key_namespace = NULL; |
| 714 |
AP4_String* key_name = NULL; |
| 715 |
AP4_Ordinal key_index; |
| 716 |
result = ParseKeySpec(*key, key_namespace, key_name, key_index); |
| 717 |
if (AP4_FAILED(result)) { |
| 718 |
fprintf(stderr, "ERROR: invalid key name (%s)\n", key->GetChars()); |
| 719 |
delete key; |
| 720 |
delete output_filename; |
| 721 |
return result; |
| 722 |
} |
| 723 |
|
| 724 |
// get the metadata |
| 725 |
const AP4_MetaData* metadata = file->GetMetaData(); |
| 726 |
|
| 727 |
// iterate over the entries and look for a match |
| 728 |
bool found = false; |
| 729 |
AP4_List<AP4_MetaData::Entry>::Item* item = NULL; // we init the var here to work around a gcc/mingw bug |
| 730 |
for (item = metadata->GetEntries().FirstItem(); |
| 731 |
item; |
| 732 |
item = item->GetNext()) { |
| 733 |
AP4_MetaData::Entry* entry = item->GetData(); |
| 734 |
if (entry->m_Key.GetNamespace() == *key_namespace && |
| 735 |
entry->m_Key.GetName() == *key_name && |
| 736 |
key_index-- == 0) { |
| 737 |
// match |
| 738 |
if (entry->m_Value == NULL) break; |
| 739 |
AP4_DataBuffer buffer; |
| 740 |
if (AP4_FAILED(entry->m_Value->ToBytes(buffer))) break; |
| 741 |
found = true; |
| 742 |
AP4_ByteStream* output = NULL; |
| 743 |
result = AP4_FileByteStream::Create(output_filename->GetChars(), AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 744 |
if (AP4_FAILED(result)) { |
| 745 |
fprintf(stderr, "ERROR: cannot open output/extract file\n"); |
| 746 |
goto end; |
| 747 |
} |
| 748 |
output->Write(buffer.GetData(), buffer.GetDataSize()); |
| 749 |
output->Release(); |
| 750 |
} |
| 751 |
} |
| 752 |
if (!found) { |
| 753 |
fprintf(stderr, "ERROR: cannot extract tag, entry not found\n"); |
| 754 |
result = AP4_ERROR_NO_SUCH_ITEM; |
| 755 |
} |
| 756 |
|
| 757 |
end: |
| 758 |
delete key; |
| 759 |
delete key_namespace; |
| 760 |
delete key_name; |
| 761 |
delete output_filename; |
| 762 |
|
| 763 |
return result; |
| 764 |
} |
| 765 |
|
| 766 |
/*---------------------------------------------------------------------- |
| 767 |
| main |
| 768 |
+---------------------------------------------------------------------*/ |
| 769 |
int |
| 770 |
main(int argc, char** argv) |
| 771 |
{ |
| 772 |
if (argc < 2) { |
| 773 |
PrintUsageAndExit(); |
| 774 |
} |
| 775 |
|
| 776 |
// init options |
| 777 |
Options.input_filename = NULL; |
| 778 |
Options.output_filename = NULL; |
| 779 |
Options.need_input = false; |
| 780 |
Options.need_output = false; |
| 781 |
|
| 782 |
// parse command line |
| 783 |
ParseCommandLine(argc-1, argv+1); |
| 784 |
|
| 785 |
// check options |
| 786 |
if (Options.need_input) { |
| 787 |
if (Options.input_filename == NULL) { |
| 788 |
fprintf(stderr, "ERROR: input file name missing\n"); |
| 789 |
PrintUsageAndExit(); |
| 790 |
} |
| 791 |
} else { |
| 792 |
if (Options.input_filename != NULL) { |
| 793 |
fprintf(stderr, "ERROR: unexpected input file name\n"); |
| 794 |
PrintUsageAndExit(); |
| 795 |
} |
| 796 |
} |
| 797 |
if (Options.need_output) { |
| 798 |
if (Options.output_filename == NULL) { |
| 799 |
fprintf(stderr, "ERROR: output file name missing\n"); |
| 800 |
PrintUsageAndExit(); |
| 801 |
} |
| 802 |
} else { |
| 803 |
if (Options.output_filename != NULL) { |
| 804 |
fprintf(stderr, "ERROR: unexpected output file name\n"); |
| 805 |
PrintUsageAndExit(); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
AP4_ByteStream* input = NULL; |
| 810 |
AP4_File* file = NULL; |
| 811 |
AP4_Movie* movie = NULL; |
| 812 |
AP4_MoovAtom* moov = NULL; |
| 813 |
AP4_LargeSize moov_size = 0; |
| 814 |
AP4_Result result = AP4_SUCCESS; |
| 815 |
if (Options.need_input) { |
| 816 |
result =AP4_FileByteStream::Create(Options.input_filename, AP4_FileByteStream::STREAM_MODE_READ, input); |
| 817 |
if (AP4_FAILED(result)) { |
| 818 |
fprintf(stderr, "ERROR: cannot open input file\n"); |
| 819 |
return 1; |
| 820 |
} |
| 821 |
file = new AP4_File(*input); |
| 822 |
|
| 823 |
// remember the size of the moov atom |
| 824 |
movie = file->GetMovie(); |
| 825 |
if (movie) { |
| 826 |
moov = movie->GetMoovAtom(); |
| 827 |
if (moov) { |
| 828 |
moov_size = moov->GetSize(); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
AP4_ByteStream* output = NULL; |
| 834 |
if (Options.need_output) { |
| 835 |
result = AP4_FileByteStream::Create(Options.output_filename, AP4_FileByteStream::STREAM_MODE_WRITE, output); |
| 836 |
if (AP4_FAILED(result)) { |
| 837 |
fprintf(stderr, "ERROR: cannot open output file for writing\n"); |
| 838 |
return 1; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
for (AP4_List<Command>::Item* item = Options.commands.FirstItem(); |
| 843 |
item; |
| 844 |
item = item->GetNext()) { |
| 845 |
result = AP4_SUCCESS; |
| 846 |
switch (item->GetData()->m_Type) { |
| 847 |
case Command::TYPE_LIST_KEYS: |
| 848 |
ListKeys(); |
| 849 |
break; |
| 850 |
|
| 851 |
case Command::TYPE_LIST_SYMBOLS: |
| 852 |
ListSymbols(); |
| 853 |
break; |
| 854 |
|
| 855 |
case Command::TYPE_SHOW_TAGS: |
| 856 |
ShowTags(file); |
| 857 |
break; |
| 858 |
|
| 859 |
case Command::TYPE_SET: |
| 860 |
result = AddTag(file, item->GetData()->m_Arg1, true); |
| 861 |
break; |
| 862 |
|
| 863 |
case Command::TYPE_ADD: |
| 864 |
result = AddTag(file, item->GetData()->m_Arg1, false); |
| 865 |
break; |
| 866 |
|
| 867 |
case Command::TYPE_REMOVE: |
| 868 |
result = RemoveTag(file, item->GetData()->m_Arg1, false); |
| 869 |
break; |
| 870 |
|
| 871 |
case Command::TYPE_EXTRACT: |
| 872 |
result = ExtractTag(file, item->GetData()->m_Arg1); |
| 873 |
break; |
| 874 |
|
| 875 |
default: |
| 876 |
break; |
| 877 |
} |
| 878 |
|
| 879 |
if (AP4_FAILED(result)) goto end; |
| 880 |
} |
| 881 |
|
| 882 |
if (output) { |
| 883 |
// adjust the chunk offsets if the moov is before the mdat |
| 884 |
if (moov && file->IsMoovBeforeMdat()) { |
| 885 |
AP4_LargeSize new_moov_size = moov->GetSize(); |
| 886 |
AP4_SI64 size_diff = new_moov_size-moov_size; |
| 887 |
if (size_diff) { |
| 888 |
moov->AdjustChunkOffsets(size_diff); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
// write the modified file |
| 893 |
AP4_FileCopier::Write(*file, *output); |
| 894 |
} |
| 895 |
|
| 896 |
end: |
| 897 |
delete file; |
| 898 |
delete input; |
| 899 |
delete output; |
| 900 |
Options.commands.DeleteReferences(); |
| 901 |
|
| 902 |
return result; |
| 903 |
} |