fixed EXEEXT.
[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
52 static struct options_t options[] = {
53 {"h", "help"},
54 {"o", "output"},
55 {"A", "adjust"},
56 {"n", "num"},
57 {"m", "mp3-bitrate"},
58 {"r", "mp3-samplerate"},
59 {"d", "scale"},
60 {"p", "flip"},
61 {"q", "quality"},
62 {"x", "extragood"},
63 {"T", "flashversion"},
64 {"V", "version"},
65 {0,0}
66 };
67
68 int args_callback_option(char*name,char*val)
69 {
70     if(!strcmp(name, "V")) {
71         printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
72         exit(0);
73     } 
74     else if(!strcmp(name, "o")) {
75         outputfilename = val;
76         return 1;
77     }
78     else if(!strcmp(name, "q")) {
79         quality = atoi(val);
80         if(quality<0)
81             quality = 0;
82         if(quality>100)
83             quality = 100;
84         return 1;
85     }
86     else if(!strcmp(name, "p")) {
87         flip = 1;
88         return 0;
89     }
90     else if(!strcmp(name, "A")) {
91         audio_adjust = atof(val);
92         return 1;
93     }
94     else if(!strcmp(name, "v")) {
95         verbose = 1;
96         return 0;
97     }
98     else if(!strcmp(name, "T")) {
99         flashversion = atoi(val);
100         return 1;
101     }
102     else if(!strcmp(name, "x")) {
103         expensive = 1;
104         return 0;
105     }
106     else if(!strcmp(name, "m")) {
107         mp3_bitrate = atoi(val);
108         return 0;
109     }
110     else if(!strcmp(name, "r")) {
111         samplerate = atoi(val);
112         if(samplerate >= 11000 && samplerate <= 12000)
113             samplerate = 11025;
114         else if(samplerate >= 22000 && samplerate <= 23000)
115             samplerate = 22050;
116         else if(samplerate >= 44000 && samplerate <= 45000)
117             samplerate = 44100;
118         else {
119             fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
120             fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
121             exit(1);
122         }
123         return 1;
124     }
125     else if(!strcmp(name, "S")) {
126         skip = atoi(val);
127         return 1;
128     }
129     else if(!strcmp(name, "s")) {
130         scale = atoi(val)/100.0;
131         if(scale>1.0 || scale<=0) {
132             fprintf(stderr, "Scale must be in the range 1-100!\n");
133             exit(1);
134         }
135         return 1;
136     }
137     fprintf(stderr, "Unknown option: -%s\n", name);
138     exit(1);
139 }
140 int args_callback_longoption(char*name,char*val)
141 {
142     return args_long2shortoption(options, name, val);
143 }
144 void args_callback_usage(char *name)
145 {
146     printf("\n");
147     printf("Usage: %s file.avi [-o output.swf]\n", name);
148     printf("\n");
149     printf("-h , --help                    Print help and exit\n");
150     printf("-o , --output filename         Specify output filename\n");
151     printf("-A , --adjust seconds          Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
152     printf("-n , --num frames              Number of frames to encode\n");
153     printf("-m , --mp3-bitrate <kbps>      Set the mp3 bitrate to encode audio with\n");
154     printf("-r , --mp3-samplerate <hz>     Set the mp3 samplerate to encode audio with (default: 11025)\n");
155     printf("-d , --scale <val>             Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
156     printf("-p , --flip                    Turn movie upside down\n");
157     printf("-q , --quality <val>           Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
158     printf("-x , --extragood               Enable some *very* expensive compression strategies.\n");
159     printf("-T , --flashversion <n>        Set output flash version to <n>.\n");
160     printf("-V , --version                 Print program version and exit\n");
161     printf("\n");
162 }
163 int args_callback_command(char*name,char*val)
164 {
165     if(filename) {
166         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
167                  filename, name);
168     }
169     filename = name;
170     return 0;
171 }
172
173 static char toabuf[128];
174 static char*ftoa(double a)
175 {
176     sprintf(toabuf, "%f", a);
177     return toabuf;
178 }
179 static char*itoa(int a)
180 {
181     sprintf(toabuf, "%d", a);
182     return toabuf;
183 }
184
185 #ifdef DO_SIGNALS
186 pthread_t main_thread;
187 static void sigterm(int sig)
188 {
189     if(pthread_equal (pthread_self(), main_thread))
190     {
191         if(frameno>0 && !shutdown_avi2swf) {
192             if(verbose)
193                 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
194             shutdown_avi2swf++;
195         } else {
196             exit(1);
197         }
198     }
199 }
200 #endif
201
202 int main (int argc,char ** argv)
203
204     videoreader_t video;
205     v2swf_t v2swf;
206     int ret;
207     FILE*fi;
208
209 #ifdef DO_SIGNALS
210     signal(SIGTERM, sigterm);
211     signal(SIGINT , sigterm);
212     signal(SIGQUIT, sigterm);
213     main_thread = pthread_self();
214 #endif
215
216     processargs(argc, argv);
217     if(!filename) {
218         fprintf(stderr, "You must supply a filename");
219         exit(0);
220     }
221     if(keyframe_interval<0) {
222         if(flashversion>=6)
223             keyframe_interval=200;
224         else
225             keyframe_interval=5;
226     }
227     
228     fi = fopen(outputfilename, "wb");
229     if(!fi) {
230         fflush(stdout); fflush(stderr);
231         fprintf(stderr, "Couldn't open %s\n", outputfilename);
232         exit(1);
233     }
234     
235 #ifdef WIN32
236     ret = videoreader_vfw_open(&video, filename);
237 #else
238     ret = videoreader_avifile_open(&video, filename);
239 #endif
240
241     if(ret<0) {
242         fprintf(stderr, "Error opening %s\n", filename);
243         exit(1);
244     }
245
246     if(verbose) {
247         printf("| video framerate: %f\n", video.fps);
248         printf("| video size: %dx%d\n", video.width, video.height);
249         printf("| audio rate: %d\n", video.samplerate);
250         printf("| audio channels: %d\n", video.channels);
251     }
252
253     ret = v2swf_init(&v2swf, &video);
254     if(verbose)
255         v2swf_setparameter(&v2swf, "verbose", "1");
256     v2swf_setparameter(&v2swf, "quality", itoa(quality));
257     v2swf_setparameter(&v2swf, "blockdiff", "0");
258     v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
259     v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
260     v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
261     //v2swf_setparameter(&v2swf, "fixheader", "1");
262     //v2swf_setparameter(&v2swf, "framerate", "15");
263     v2swf_setparameter(&v2swf, "scale", ftoa(scale));
264     v2swf_setparameter(&v2swf, "prescale", "1");
265     v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
266     v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
267     if(expensive)
268         v2swf_setparameter(&v2swf, "motioncompensation", "1");
269     if(flip)
270         video.setparameter(&video, "flip", "1");
271     if(verbose)
272         video.setparameter(&video, "verbose", "1");
273
274     if(!verbose)
275         printf("\n");
276
277     if(audio_adjust>0) {
278         int num = ((int)(audio_adjust*video.samplerate))*video.channels*2;
279         void*buf = malloc(num);
280         video.getsamples(&video, buf, num);
281         free(buf);
282     } else if(audio_adjust<0) {
283         int num = (int)(-audio_adjust*video.fps);
284         void*buf = malloc(video.width*video.height*4);
285         int t;
286         for(t=0;t<num;t++) {
287             video.getimage(&video, buf);
288         }
289         free(buf);
290     }
291
292     if(skip) {
293         int t;
294         void*buf = malloc(video.width*video.height*4);
295         for(t=0;t<skip;t++) {
296             video.getimage(&video, buf);
297             video.getsamples(&video, buf, (int)((video.samplerate/video.fps)*video.channels*2));
298             if(!verbose) {
299                 printf("\rSkipping frame %d", video.frame);fflush(stdout);
300             }
301         }
302         free(buf);
303     }
304
305     char buffer[4096];
306     while(1) {
307         int l=v2swf_read(&v2swf, buffer, 4096);
308         fwrite(buffer, l, 1, fi);
309         if(!l)
310             break;
311         if(!verbose) {
312             printf("\rConverting frame %d", video.frame);fflush(stdout);
313         }
314     }
315     if(!verbose)
316         printf("\n");
317     fclose(fi);
318     v2swf_backpatch(&v2swf, outputfilename);
319     v2swf_close(&v2swf);
320 }
321