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
|
/*
cfourcc.c - this is the main (and only) module.
cfourcc - change the FOURCC code in the Microsoft RIFF AVI file.
Copyright (C) 2004,2005,2014 Mohammad Hafiz bin Ismail (mypapit) <mypapit@gmail.com>
This program 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Project's web : https://github.com/mypapit/cfourcc
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define AVILEN 224
#define FLAG_INFO 0x0001
#define FLAG_DESC 0x0010
#define FLAG_USED 0x0100
#define FLAG_HELP 0x1000
#define FLAG_FORCE 0x10000
typedef char AVIHDR[AVILEN];
extern char *strdup (const char *);
int
getDesc (AVIHDR header, char *str)
{
memcpy (str, &header[0x70], 4);
return 0;
}
int
getUsed (AVIHDR header, char *str)
{
memcpy (str, &header[0xbc], 4);
return 0;
}
int
setDesc (AVIHDR header, const char *str)
{
memcpy (&header[0x70], str, 4);
return 0;
}
int
setUsed (AVIHDR header, const char *str)
{
memcpy (&header[0xbc], str, 4);
return 0;
}
int
usage (void)
{
puts
("usage :\n\tcfoucc [-i] file.avi\n\tcfourcc -u DIVX file.avi\n\tcfourcc -d DIVX file.avi\n\tcfourcc -h\n");
return 0;
}
int
main (int argc, char *argv[])
{
AVIHDR avihdr;
FILE *fin;
int optchar;
int flags = 0x00;
static char strused[5], strdesc[5];
char *ptrused = NULL, *ptrdesc = NULL;
char MAGIC[] = { 'R', 'I', 'F', 'F' };
memset (&avihdr, 0, AVILEN); /*init avihdr just in case! */
opterr = 0;
puts
("cfourcc 0.1.3.1 - (stupid) console fourcc changer\nCopyright (C) 2014 Mohammad Hafiz <mypapit@gmail.com>\nLicensed under the terms of the GNU General Public License version 2 (or later)\n");
while ((optchar = getopt (argc, argv, "d:u:ihf::")) != -1) {
switch (optchar) {
case 'i':
/* printf("print info\n");*/
flags |= FLAG_INFO;
break;
case 'f':
flags |= FLAG_FORCE;
break;
case 'd':
flags |= FLAG_DESC;
ptrdesc = (char *) strdup (optarg);
break;
case 'u':
flags |= FLAG_USED;
ptrused = (char *) strdup (optarg);
break;
case '?':
/* flags|=FLAG_HELP;*/
/*usage(); */
break;
case 'h':
flags |= FLAG_HELP;
usage ();
puts ("\t-h\t\t Prints this help ;)");
puts ("\t-i\t\t Prints FOURCC information (default)");
puts ("\t-f\t\t Force changing FOURCC on unsupported file (dangerous)");
puts ("\t-u CODE\t\t Sets FOURCC \'used\' codec");
puts ("\t-d CODE\t\t Sets FOURCC \'description\' codec\n");
puts ("\t ** refer to \'codeclist.txt\' documentation for a list of FOURCC\n\n");
break;
default:
return 0x0;
}
}
if (!((optind > 0) && (optchar = -1) && (argv[optind] != NULL))) {
if ((flags & FLAG_HELP) != FLAG_HELP)
usage ();
return 0x0f;
}
fin = fopen (argv[optind], "rb");
if (fin == NULL) {
fprintf (stderr, "Error : Cannot access %s\n", argv[optind]);
return 0x01;
}
if (fread (avihdr, sizeof (char), AVILEN, fin) < AVILEN) {
fprintf (stderr, "Error : Read end unexpectedly, incomplete file?\n");
return 0x02;
}
fclose (fin);
/*lazy verifier! */
if (memcmp (avihdr, MAGIC, 4) && !(flags & FLAG_FORCE) ) {
fprintf (stderr, "Error : Probably not a supported AVI or media file\n");
return 0x03;
}
if (flags == 0x0 || ( (flags & FLAG_INFO) == FLAG_INFO)) { /*default behaviour */
getDesc (avihdr, strdesc);
getUsed (avihdr, strused);
printf ("FOURCC of \'%s\' :\nDescription : %s\nUse : %s\n\n",
argv[optind], strdesc, strused);
return 0x00;
}
if (((flags & FLAG_USED) == FLAG_USED)
|| ((flags & FLAG_DESC) == FLAG_DESC)) {
fin = fopen (argv[optind], "r+b");
if (fin == NULL) {
fprintf (stderr, "Error : Cannot access %s for writing\n",
argv[optind]);
return 0x01;
}
if (flags & FLAG_USED)
setUsed (avihdr, ptrused);
if (flags & FLAG_DESC)
setDesc (avihdr, ptrdesc);
fseek (fin, 0, SEEK_SET);
fwrite (avihdr, sizeof (char), AVILEN, fin);
fflush (fin);
fclose (fin);
puts ("Attempting to change FOURCC value...\n");
}
puts ("Done.\n");
return 0;
}
|