fixed --help output
[swftools.git] / avi2swf / avi2swf.cc
1 /* avi2swf.cc
2    Convert avi movie files into swf.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26
27 #include "../config.h"
28
29 #include "../lib/args.h"
30 #include "v2swf.h"
31 #ifdef WIN32
32 #include "videoreader_vfw.hh"
33 #else
34 #include "videoreader_avifile.hh"
35 #endif
36
37 static char * filename = 0;
38 static char * outputfilename = "output.swf";
39 int verbose = 0;
40
41 static int quality = 80;
42 static double scale = 1.0;
43 static int flip = 0;
44 static int expensive = 0;
45 static int flashversion = 6;
46 static int keyframe_interval = -1;
47 static int skip = 0;
48 static float audio_adjust = 0;
49 static int mp3_bitrate = 32;
50 static int samplerate = 11025;
51 static int numframes = 0;
52
53 static struct options_t options[] = {
54 {"h", "help"},
55 {"o", "output"},
56 {"A", "adjust"},
57 {"n", "num"},
58 {"m", "mp3-bitrate"},
59 {"r", "mp3-samplerate"},
60 {"s", "scale"},
61 {"S", "skipframes"},
62 {"p", "flip"},
63 {"q", "quality"},
64 {"k", "keyframe"},
65 {"x", "extragood"},
66 {"T", "flashversion"},
67 {"V", "version"},
68 {0,0}
69 };
70
71 int args_callback_option(char*name,char*val)
72 {
73     if(!strcmp(name, "V")) {
74         printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
75         exit(0);
76     } 
77     else if(!strcmp(name, "o")) {
78         outputfilename = val;
79         return 1;
80     }
81     else if(!strcmp(name, "n")) {
82         numframes = atoi(val);
83         return 1;
84     }
85     else if(!strcmp(name, "d")) {
86         scale = atof(val);
87         return 1;
88     }
89     else if(!strcmp(name, "q")) {
90         quality = atoi(val);
91         if(quality<0)
92             quality = 0;
93         if(quality>100)
94             quality = 100;
95         return 1;
96     }
97     else if(!strcmp(name, "p")) {
98         flip = 1;
99         return 0;
100     }
101     else if(!strcmp(name, "k")) {
102         keyframe_interval = atoi(val);
103         return 1;
104     }
105     else if(!strcmp(name, "A")) {
106         audio_adjust = atof(val);
107         return 1;
108     }
109     else if(!strcmp(name, "v")) {
110         verbose = 1;
111         return 0;
112     }
113     else if(!strcmp(name, "T")) {
114         flashversion = atoi(val);
115         return 1;
116     }
117     else if(!strcmp(name, "x")) {
118         expensive = 1;
119         return 0;
120     }
121     else if(!strcmp(name, "m")) {
122         mp3_bitrate = atoi(val);
123         return 1;
124     }
125     else if(!strcmp(name, "r")) {
126         samplerate = atoi(val);
127         if(samplerate >= 11000 && samplerate <= 12000)
128             samplerate = 11025;
129         else if(samplerate >= 22000 && samplerate <= 23000)
130             samplerate = 22050;
131         else if(samplerate >= 44000 && samplerate <= 45000)
132             samplerate = 44100;
133         else {
134             fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
135             fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
136             exit(1);
137         }
138         return 1;
139     }
140     else if(!strcmp(name, "S")) {
141         skip = atoi(val);
142         return 1;
143     }
144     else if(!strcmp(name, "s")) {
145         scale = atoi(val)/100.0;
146         if(scale>1.0 || scale<=0) {
147             fprintf(stderr, "Scale must be in the range 1-100!\n");
148             exit(1);
149         }
150         return 1;
151     }
152     fprintf(stderr, "Unknown option: -%s\n", name);
153     exit(1);
154 }
155 int args_callback_longoption(char*name,char*val)
156 {
157     return args_long2shortoption(options, name, val);
158 }
159 void args_callback_usage(char *name)
160 {
161     printf("\n");
162     printf("Usage: %s file.avi [-o output.swf]\n", name);
163     printf("\n");
164     printf("-h , --help                    Print help and exit\n");
165     printf("-o , --output filename         Specify output filename\n");
166     printf("-A , --adjust seconds          Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
167     printf("-n , --num frames              Number of frames to encode\n");
168     printf("-m , --mp3-bitrate <kbps>      Set the mp3 bitrate to encode audio with\n");
169     printf("-r , --mp3-samplerate <hz>     Set the mp3 samplerate to encode audio with (default: 11025)\n");
170     printf("-s , --scale <val>             Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
171     printf("-S , --skipframes <num>        Skip <num> frames before starting the conversion.\n");
172     printf("-p , --flip                    Turn movie upside down\n");
173     printf("-q , --quality <val>           Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
174     printf("-k , --keyframe                Set the number of intermediate frames between keyframes.\n");
175     printf("-x , --extragood               Enable some *very* expensive compression strategies.\n");
176     printf("-T , --flashversion <n>        Set output flash version to <n>.\n");
177     printf("-V , --version                 Print program version and exit\n");
178     printf("\n");
179 }
180 int args_callback_command(char*name,char*val)
181 {
182     if(filename) {
183         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
184                  filename, name);
185     }
186     filename = name;
187     return 0;
188 }
189
190 static char toabuf[128];
191 static char*ftoa(double a)
192 {
193     sprintf(toabuf, "%f", a);
194     return toabuf;
195 }
196 static char*itoa(int a)
197 {
198     sprintf(toabuf, "%d", a);
199     return toabuf;
200 }
201
202 #ifdef DO_SIGNALS
203 pthread_t main_thread;
204 static void sigterm(int sig)
205 {
206     if(pthread_equal (pthread_self(), main_thread))
207     {
208         if(frameno>0 && !shutdown_avi2swf) {
209             if(verbose)
210                 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
211             shutdown_avi2swf++;
212         } else {
213             exit(1);
214         }
215     }
216 }
217 #endif
218
219 int main (int argc,char ** argv)
220
221     videoreader_t video;
222     v2swf_t v2swf;
223     int ret;
224     FILE*fi;
225
226 #ifdef DO_SIGNALS
227     signal(SIGTERM, sigterm);
228     signal(SIGINT , sigterm);
229     signal(SIGQUIT, sigterm);
230     main_thread = pthread_self();
231 #endif
232
233     processargs(argc, argv);
234     if(!filename) {
235         fprintf(stderr, "You must supply a filename");
236         exit(0);
237     }
238     if(keyframe_interval<0) {
239         if(flashversion>=6)
240             keyframe_interval=20;
241         else
242             keyframe_interval=5;
243     }
244     
245     fi = fopen(outputfilename, "wb");
246     if(!fi) {
247         fflush(stdout); fflush(stderr);
248         fprintf(stderr, "Couldn't open %s\n", outputfilename);
249         exit(1);
250     }
251     
252 #ifdef WIN32
253     ret = videoreader_vfw_open(&video, filename);
254 #else
255     ret = videoreader_avifile_open(&video, filename);
256 #endif
257
258     if(ret<0) {
259         fprintf(stderr, "Error opening %s\n", filename);
260         exit(1);
261     }
262
263     if(verbose) {
264         printf("| video framerate: %f\n", video.fps);
265         printf("| video size: %dx%d\n", video.width, video.height);
266         printf("| audio rate: %d\n", video.samplerate);
267         printf("| audio channels: %d\n", video.channels);
268     }
269
270     ret = v2swf_init(&v2swf, &video);
271     if(verbose)
272         v2swf_setparameter(&v2swf, "verbose", "1");
273     if(numframes)
274         v2swf_setparameter(&v2swf, "numframes", itoa(numframes));
275     v2swf_setparameter(&v2swf, "quality", itoa(quality));
276     v2swf_setparameter(&v2swf, "blockdiff", "0");
277     v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
278     v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
279     v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
280     //v2swf_setparameter(&v2swf, "fixheader", "1");
281     //v2swf_setparameter(&v2swf, "framerate", "15");
282     v2swf_setparameter(&v2swf, "scale", ftoa(scale));
283     v2swf_setparameter(&v2swf, "prescale", "1");
284     v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
285     v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
286     if(expensive)
287         v2swf_setparameter(&v2swf, "motioncompensation", "1");
288     if(flip)
289         video.setparameter(&video, "flip", "1");
290     if(verbose)
291         video.setparameter(&video, "verbose", "1");
292
293     if(!verbose)
294         printf("\n");
295
296     if(audio_adjust>0) {
297         int num = ((int)(audio_adjust*video.samplerate))*video.channels*2;
298         void*buf = malloc(num);
299         video.getsamples(&video, buf, num);
300         free(buf);
301     } else if(audio_adjust<0) {
302         int num = (int)(-audio_adjust*video.fps);
303         void*buf = malloc(video.width*video.height*4);
304         int t;
305         for(t=0;t<num;t++) {
306             video.getimage(&video, buf);
307         }
308         free(buf);
309     }
310
311     if(skip) {
312         int t;
313         void*buf = malloc(video.width*video.height*4);
314         for(t=0;t<skip;t++) {
315             video.getimage(&video, buf);
316             video.getsamples(&video, buf, (int)((video.samplerate/video.fps)*video.channels*2));
317             if(!verbose) {
318                 printf("\rSkipping frame %d", video.frame);fflush(stdout);
319             }
320         }
321         free(buf);
322     }
323
324     char buffer[4096];
325     while(1) {
326         int l=v2swf_read(&v2swf, buffer, 4096);
327         fwrite(buffer, l, 1, fi);
328         if(!l)
329             break;
330         if(!verbose) {
331             printf("\rConverting frame %d", video.frame);fflush(stdout);
332         }
333     }
334     if(!verbose)
335         printf("\n");
336     fclose(fi);
337     v2swf_backpatch(&v2swf, outputfilename);
338     v2swf_close(&v2swf);
339 }
340