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"
29 #include "../lib/args.h"
31 #include "videoreader_avifile.hh"
33 static char * filename = 0;
34 static char * outputfilename = "output.swf";
37 static int quality = 80;
38 static double scale = 1.0;
40 static int expensive = 0;
41 static int flashversion = 6;
42 static int keyframe_interval = -1;
44 static float audio_adjust = 0;
45 static int mp3_bitrate = 32;
46 static int samplerate = 11025;
48 static struct options_t options[] = {
54 {"r", "mp3-samplerate"},
59 {"T", "flashversion"},
64 int args_callback_option(char*name,char*val)
66 if(!strcmp(name, "V")) {
67 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
70 else if(!strcmp(name, "o")) {
74 else if(!strcmp(name, "q")) {
82 else if(!strcmp(name, "p")) {
86 else if(!strcmp(name, "A")) {
87 audio_adjust = atof(val);
90 else if(!strcmp(name, "v")) {
94 else if(!strcmp(name, "T")) {
95 flashversion = atoi(val);
98 else if(!strcmp(name, "x")) {
102 else if(!strcmp(name, "m")) {
103 mp3_bitrate = atoi(val);
106 else if(!strcmp(name, "r")) {
107 samplerate = atoi(val);
108 if(samplerate >= 11000 && samplerate <= 12000)
110 else if(samplerate >= 22000 && samplerate <= 23000)
112 else if(samplerate >= 44000 && samplerate <= 45000)
115 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
116 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
121 else if(!strcmp(name, "S")) {
125 else if(!strcmp(name, "s")) {
126 scale = atoi(val)/100.0;
127 if(scale>1.0 || scale<=0) {
128 fprintf(stderr, "Scale must be in the range 1-100!\n");
133 fprintf(stderr, "Unknown option: -%s\n", name);
136 int args_callback_longoption(char*name,char*val)
138 return args_long2shortoption(options, name, val);
140 void args_callback_usage(char *name)
143 printf("Usage: %s file.avi [-o output.swf]\n", name);
145 printf("-h , --help Print help and exit\n");
146 printf("-o , --output filename Specify output filename\n");
147 printf("-A , --adjust seconds Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
148 printf("-n , --num frames Number of frames to encode\n");
149 printf("-m , --mp3-bitrate <rate> (kbps) Set the mp3 bitrate to encode audio with\n");
150 printf("-r , --mp3-samplerate <rate> (Hz) Set the mp3 samplerate to encode audio with (default: 11025)\n");
151 printf("-d , --scale <val> Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
152 printf("-p , --flip Turn movie upside down\n");
153 printf("-q , --quality <val> Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
154 printf("-x , --extragood Enable some *very* expensive compression strategies.\n");
155 printf("-T , --flashversion <n> Set output flash version to <n>.\n");
156 printf("-V , --version Print program version and exit\n");
159 int args_callback_command(char*name,char*val)
162 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
169 static char toabuf[128];
170 static char*ftoa(double a)
172 sprintf(toabuf, "%f", a);
175 static char*itoa(int a)
177 sprintf(toabuf, "%d", a);
182 pthread_t main_thread;
183 static void sigterm(int sig)
185 if(pthread_equal (pthread_self(), main_thread))
187 if(frameno>0 && !shutdown_avi2swf) {
189 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
198 int main (int argc,char ** argv)
206 signal(SIGTERM, sigterm);
207 signal(SIGINT , sigterm);
208 signal(SIGQUIT, sigterm);
209 main_thread = pthread_self();
212 processargs(argc, argv);
215 if(keyframe_interval<0) {
217 keyframe_interval=200;
222 fi = fopen(outputfilename, "wb");
224 fflush(stdout); fflush(stderr);
225 fprintf(stderr, "Couldn't open %s\n", outputfilename);
229 ret = videoreader_avifile_open(&video, filename);
232 printf("Error opening %s\n", filename);
237 printf("| video framerate: %f\n", video.fps);
238 printf("| video size: %dx%d\n", video.width, video.height);
239 printf("| audio rate: %d\n", video.rate);
240 printf("| audio channels: %d\n", video.channels);
243 ret = v2swf_init(&v2swf, &video);
245 v2swf_setparameter(&v2swf, "verbose", "1");
246 v2swf_setparameter(&v2swf, "quality", itoa(quality));
247 v2swf_setparameter(&v2swf, "blockdiff", "0");
248 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
249 v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
250 v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
251 //v2swf_setparameter(&v2swf, "fixheader", "1");
252 //v2swf_setparameter(&v2swf, "framerate", "15");
253 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
254 v2swf_setparameter(&v2swf, "prescale", "1");
255 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
256 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
258 v2swf_setparameter(&v2swf, "motioncompensation", "1");
260 video.setparameter(&video, "flip", "1");
262 video.setparameter(&video, "verbose", "1");
268 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
269 void*buf = malloc(num);
270 video.getsamples(&video, buf, num);
272 } else if(audio_adjust<0) {
273 int num = (int)(-audio_adjust*video.fps);
274 void*buf = malloc(video.width*video.height*4);
277 video.getimage(&video, buf);
284 void*buf = malloc(video.width*video.height*4);
285 for(t=0;t<skip;t++) {
286 video.getimage(&video, buf);
287 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
289 printf("\rSkipping frame %d", video.frame);fflush(stdout);
297 int l=v2swf_read(&v2swf, buffer, 4096);
298 fwrite(buffer, l, 1, fi);
302 printf("\rConverting frame %d", video.frame);fflush(stdout);
308 v2swf_backpatch(&v2swf, outputfilename);