forked from ponchio/untrunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
126 lines (109 loc) · 3.42 KB
/
main.cpp
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
/*
Untrunc - main.cpp
Untrunc is GPL software; you can freely distribute,
redistribute, modify & use under the terms of the GNU General
Public License; either version 2 or its successor.
Untrunc is distributed under the GPL "AS IS", without
any warranty; without the implied warranty of merchantability
or fitness for either an expressed or implied particular purpose.
Please see the included GNU General Public License (GPL) for
your rights and further details; see the file COPYING. If you
cannot, write to the Free Software Foundation, 59 Temple Place
Suite 330, Boston, MA 02111-1307, USA. Or www.fsf.org
Copyright 2010 Federico Ponchio
*/
#include "mp4.h"
#include "atom.h"
#include "log.h"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void usage() {
cout << "Usage: untrunc [-a -i -v -w] <ok.mp4> [<corrupt.mp4>]\n\n"
<< " -i: info about codecs and mov structure\n"
<< " -a: test the ok video\n"
<< " -s: simulate recovering the ok video for debug purposes\n"
<< " -t: analyze track\n"
<< " -m: use the same offset for mdat beginning\n"
<< " -M: serach for probable packet starts for mdat\n"
<< " -b: specify initial byte for mdat content\n"
<< " -q: silent\n"
<< " -e: error\n"
<< " -v; verbose\n"
<< " -w: debug info\n\n";
}
int main(int argc, char *argv[]) {
bool info = false;
bool analyze = false;
bool simulate = false;
int analyze_track = -1;
bool same_mdat_start = false; //if mdat can be found or starting of packets try using the same absolute offset.
bool ignore_mdat_start = false; //ignore mdat string and look for first recognizable packet.
int64_t begin = -1; //start of packets if specified.
int i = 1;
for(; i < argc; i++) {
string arg(argv[i]);
if(arg[0] == '-') {
switch(arg[1]) {
case 'i': info = true; break;
case 'a': analyze = true; break;
case 's': simulate = true; break;
case 't': analyze_track = atoi(argv[i+1]); i++; break;
case 'q': Logger::log_level = Logger::SILENT; break;
case 'e': Logger::log_level = Logger::ERROR; break;
case 'v': Logger::log_level = Logger::INFO; break;
case 'w': Logger::log_level = Logger::DEBUG; break;
case 'm': same_mdat_start = true; break;
case 'M': ignore_mdat_start = true; break;
case 'b': begin = atoi(argv[i+1]); i++; break;
}
} else
break;
}
if(argc == i) {
usage();
return -1;
}
string ok = argv[i];
string corrupt;
i++;
if(i < argc)
corrupt = argv[i];
Log::info << "Reading: " << ok << endl;
Mp4 mp4;
try {
mp4.open(ok);
if(info) {
mp4.printMediaInfo();
mp4.printAtoms();
}
if(analyze) {
mp4.analyze(analyze_track);
}
if(simulate)
mp4.simulate(same_mdat_start, ignore_mdat_start, begin);
if(corrupt.size()) {
bool success = mp4.repair(corrupt, same_mdat_start, ignore_mdat_start, begin);
if(!success && !same_mdat_start) {
same_mdat_start = true;
success = mp4.repair(corrupt, same_mdat_start, ignore_mdat_start, begin);
if(!success && !ignore_mdat_start)
success = mp4.repair(corrupt, same_mdat_start, ignore_mdat_start, begin);
}
if(!success) {
Log::error << "Failed recovering the file\n";
}
size_t lastindex = corrupt.find_last_of(".");
string fixed = corrupt.substr(0, lastindex);
mp4.saveVideo(fixed + "_fixed.mp4");
}
} catch(string e) {
Log::error << e << endl;
return -1;
} catch(const char *e) {
Log::error << e << endl;
return -1;
}
return 0;
}