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