OSDN Git Service

HandBrake 0.3
[handbrake-jp/handbrake-jp-git.git] / test / Test.cpp
1 /* $Id: Test.cpp,v 1.5 2003/10/05 14:28:40 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://beos.titer.org/handbrake/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <signal.h>
8
9 #include "Manager.h"
10
11 volatile bool die;
12
13 void SigHandler( int signal )
14 {
15     die = true;
16 }
17
18 int main( int argc, char ** argv )
19 {
20     die = false;
21
22     /* Exit ASAP on Ctrl-C */
23     signal( SIGINT,  SigHandler );
24
25     /* Default values */
26     bool   debug       = false;
27     char * device      = NULL;
28     char * outputFile  = NULL;
29     int    titleIdx    = 1;
30     bool   twoPass     = false;
31     bool   deinterlace = false;
32     int    width       = 0;
33     int    topCrop     = 0;
34     int    bottomCrop  = 0;
35     int    leftCrop    = 0;
36     int    rightCrop   = 0;
37
38     /* Parse command line */
39     int c;
40     while( ( c = getopt( argc, argv, "vd:o:t:piw:j:k:l:m:" ) ) != -1 )
41     {
42         switch( c )
43         {
44             case 'v':
45                 debug = true;
46                 break;
47
48             case 'd':
49                 device = strdup( optarg );
50                 break;
51
52             case 'o':
53                 outputFile = strdup( optarg );
54                 break;
55
56             case 't':
57                 titleIdx = atoi( optarg );
58                 break;
59
60             case 'p':
61                 twoPass = true;
62                 break;
63
64             case 'i':
65                 deinterlace = true;
66                 break;
67
68             case 'w':
69                 width = atoi( optarg );
70                 break;
71
72             case 'j':
73                 topCrop = atoi( optarg );
74                 break;
75
76             case 'k':
77                 bottomCrop = atoi( optarg );
78                 break;
79
80             case 'l':
81                 leftCrop = atoi( optarg );
82                 break;
83
84             case 'm':
85                 rightCrop = atoi( optarg );
86                 break;
87
88             default:
89                 break;
90         }
91     }
92
93     /* Check parsed options */
94     if( !device || !outputFile )
95     {
96         fprintf( stderr,
97                  "Syntax: HBTest [options] -d <device> -o <file>\n"
98                  "Possible options are :\n"
99                  "    -v                   verbose output\n"
100                  "    -t <value>           select a title (default is 1)\n"
101                  "    -p                   2-pass encoding\n"
102                  "    -i                   deinterlace picture\n"
103                  "    -w                   output width\n"
104                  "    -j <value>           top cropping\n"
105                  "    -k <value>           bottom cropping\n"
106                  "    -l <value>           left cropping\n"
107                  "    -m <value>           right cropping\n" );
108         return 1;
109     }
110
111     /* Create the manager thread */
112     HBManager * manager = new HBManager( debug );
113
114     /* Tell the manager to scan the specified volume */
115     manager->ScanVolumes( device );
116
117     HBStatus status;
118     while( !die )
119     {
120         if( !manager->NeedUpdate() )
121         {
122             Snooze( 10000 );
123             continue;
124         }
125
126         status = manager->GetStatus();
127
128         switch( status.fMode )
129         {
130             case HB_MODE_UNDEF:
131                 break;
132
133             case HB_MODE_SCANNING:
134                 if( !status.fScannedTitle )
135                 {
136                     fprintf( stderr, "Scanning %s\n",
137                              status.fScannedVolume );
138                 }
139                 else
140                 {
141                     fprintf( stderr, "Scanning %s, title %d\n",
142                              status.fScannedVolume,
143                              status.fScannedTitle );
144                 }
145                 break;
146
147             case HB_MODE_READY_TO_RIP:
148             {
149                 /* Find the title */
150                 HBTitle * title = NULL;
151                 for( uint32_t i = 0; i < status.fTitleList->CountItems(); i++ )
152                 {
153                     title = (HBTitle*) status.fTitleList->ItemAt( i );
154                     if( title->fIndex == titleIdx )
155                     {
156                         break;
157                     }
158                     else
159                     {
160                         title = NULL;
161                     }
162                 }
163
164                 if( !title )
165                 {
166                     fprintf( stderr, "Error: unvalid title. Possible "
167                              "choices are: " );
168                     for( uint32_t i = 0;
169                          i < status.fTitleList->CountItems(); i++ )
170                     {
171                         title = (HBTitle*) status.fTitleList->ItemAt( i );
172                         fprintf( stderr, "%d%s", title->fIndex,
173                                  ( i == status.fTitleList->CountItems() - 1 )
174                                  ? ".\n" : ", " );
175                     }
176                     die = true;
177                     break;
178                 }
179                 title->fTwoPass = twoPass;
180                 title->fDeinterlace = deinterlace;
181                 if( width ) title->fOutWidth = width;
182                 title->fTopCrop = topCrop;
183                 title->fBottomCrop = bottomCrop;
184                 title->fLeftCrop = leftCrop;
185                 title->fRightCrop = rightCrop;
186
187                 HBAudio * audio = (HBAudio*) title->fAudioList->ItemAt( 0 );
188
189                 manager->StartRip( title, audio, NULL, outputFile );
190                 break;
191             }
192
193             case HB_MODE_ENCODING:
194                 fprintf( stderr, "Progress = %.2f %%, %.2f fps "
195                          "(%02d:%02d:%02d remaining)\n",
196                          100 * status.fPosition, status.fFrameRate,
197                          status.fRemainingTime / 3600,
198                          ( status.fRemainingTime % 3600 ) / 60,
199                          status.fRemainingTime % 60 );
200                 break;
201
202             case HB_MODE_DONE:
203                 fprintf( stderr, "Done\n" );
204                 die = true;
205                 break;
206
207             case HB_MODE_CANCELED:
208                 fprintf( stderr, "Canceled\n" );
209                 die = true;
210                 break;
211
212             case HB_MODE_ERROR:
213                 fprintf( stderr, "Error\n" );
214                 die = true;
215                 break;
216
217             default:
218                 break;
219         }
220     }
221
222     delete manager;
223
224     if( device )     free( device );
225     if( outputFile ) free( outputFile );
226
227     return 0;
228 }
229