2 Convert avi movie files into swf.
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
27 #include "../config.h"
38 #include "../lib/args.h"
41 #include "videoreader_avifile.hh"
43 static char * filename = 0;
44 static char * outputfilename = "output.swf";
47 static int quality = 80;
48 static double scale = 1.0;
50 static int expensive = 0;
51 static int flashversion = 6;
52 static int keyframe_interval = -1;
54 static float audio_adjust = 0;
55 static int mp3_bitrate = 32;
56 static int samplerate = 11025;
58 static struct options_t options[] = {
64 {"r", "mp3-samplerate"},
69 {"T", "flashversion"},
74 int args_callback_option(char*name,char*val)
76 if(!strcmp(name, "V")) {
77 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
80 else if(!strcmp(name, "o")) {
84 else if(!strcmp(name, "q")) {
92 else if(!strcmp(name, "p")) {
96 else if(!strcmp(name, "A")) {
97 audio_adjust = atof(val);
100 else if(!strcmp(name, "v")) {
104 else if(!strcmp(name, "T")) {
105 flashversion = atoi(val);
108 else if(!strcmp(name, "x")) {
112 else if(!strcmp(name, "m")) {
113 mp3_bitrate = atoi(val);
116 else if(!strcmp(name, "r")) {
117 samplerate = atoi(val);
118 if(samplerate >= 11000 && samplerate <= 12000)
120 else if(samplerate >= 22000 && samplerate <= 23000)
122 else if(samplerate >= 44000 && samplerate <= 45000)
125 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
126 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
131 else if(!strcmp(name, "S")) {
135 else if(!strcmp(name, "s")) {
136 scale = atoi(val)/100.0;
137 if(scale>1.0 || scale<=0) {
138 fprintf(stderr, "Scale must be in the range 1-100!\n");
143 fprintf(stderr, "Unknown option: -%s\n", name);
146 int args_callback_longoption(char*name,char*val)
148 return args_long2shortoption(options, name, val);
150 void args_callback_usage(char *name)
153 printf("Usage: %s file.avi [-o output.swf]\n", name);
155 printf("-h , --help Print help and exit\n");
156 printf("-o , --output filename Specify output filename\n");
157 printf("-A , --adjust seconds Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
158 printf("-n , --num frames Number of frames to encode\n");
159 printf("-m , --mp3-bitrate <rate> (kbps) Set the mp3 bitrate to encode audio with\n");
160 printf("-r , --mp3-samplerate <rate> (Hz) Set the mp3 samplerate to encode audio with (default: 11025)\n");
161 printf("-d , --scale <val> Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
162 printf("-p , --flip Turn movie upside down\n");
163 printf("-q , --quality <val> Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
164 printf("-x , --extragood Enable some *very* expensive compression strategies.\n");
165 printf("-T , --flashversion <n> Set output flash version to <n>.\n");
166 printf("-V , --version Print program version and exit\n");
169 int args_callback_command(char*name,char*val)
172 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
179 static char toabuf[128];
180 static char*ftoa(double a)
182 sprintf(toabuf, "%f", a);
185 static char*itoa(int a)
187 sprintf(toabuf, "%d", a);
191 static int shutdown_avi2swf = 0;
192 static int frameno = 0;
195 pthread_t main_thread;
196 static void sigterm(int sig)
198 if(pthread_equal (pthread_self(), main_thread))
200 if(frameno>0 && !shutdown_avi2swf) {
202 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
211 int main (int argc,char ** argv)
219 signal(SIGTERM, sigterm);
220 signal(SIGINT , sigterm);
221 signal(SIGQUIT, sigterm);
222 main_thread = pthread_self();
225 processargs(argc, argv);
228 if(keyframe_interval<0) {
230 keyframe_interval=200;
235 fi = fopen(outputfilename, "wb");
237 fflush(stdout); fflush(stderr);
238 fprintf(stderr, "Couldn't open %s\n", outputfilename);
242 ret = videoreader_avifile_open(&video, filename);
245 printf("Error opening %s\n", filename);
250 printf("| video framerate: %f\n", video.fps);
251 printf("| video size: %dx%d\n", video.width, video.height);
252 printf("| audio rate: %d\n", video.rate);
253 printf("| audio channels: %d\n", video.channels);
256 ret = v2swf_init(&v2swf, &video);
258 v2swf_setparameter(&v2swf, "verbose", "1");
259 v2swf_setparameter(&v2swf, "quality", itoa(quality));
260 v2swf_setparameter(&v2swf, "blockdiff", "0");
261 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
262 v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
263 v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
264 //v2swf_setparameter(&v2swf, "fixheader", "1");
265 //v2swf_setparameter(&v2swf, "framerate", "15");
266 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
267 v2swf_setparameter(&v2swf, "prescale", "1");
268 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
269 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
271 v2swf_setparameter(&v2swf, "motioncompensation", "1");
277 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
278 void*buf = malloc(num);
279 video.getsamples(&video, buf, num);
281 } else if(audio_adjust<0) {
282 int num = (int)(-audio_adjust*video.fps);
283 void*buf = malloc(video.width*video.height*4);
286 video.getimage(&video, buf);
293 void*buf = malloc(video.width*video.height*4);
294 for(t=0;t<skip;t++) {
295 video.getimage(&video, buf);
296 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
298 printf("\rSkipping frame %d", frameno);fflush(stdout);
306 int l=v2swf_read(&v2swf, buffer, 4096);
307 fwrite(buffer, l, 1, fi);
311 printf("\rConverting frame %d", frameno);fflush(stdout);
317 v2swf_backpatch(&v2swf, outputfilename);