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