]> git.sesse.net Git - mlt/blob - src/melt/melt.c
177ab5af36c45f399aa391a4763470badcbb51f2
[mlt] / src / melt / melt.c
1 /*
2  * melt.c -- MLT command line utility
3  * Copyright (C) 2002-2011 Ushodaya Enterprises Limited
4  * Authors: Charles Yates <charles.yates@pandora.be>
5  *          Dan Dennedy <dan@dennedy.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sched.h>
29 #include <libgen.h>
30 #include <limits.h>
31
32 #include <framework/mlt.h>
33
34 #if defined(__DARWIN__) || defined(WIN32)
35 #include <SDL.h>
36 #endif
37
38 #include "io.h"
39
40 static void transport_action( mlt_producer producer, char *value )
41 {
42         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
43         mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
44         mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
45         mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
46         mlt_position position = mlt_producer_position( producer );
47
48         mlt_properties_set_int( properties, "stats_off", 1 );
49
50         if ( strlen( value ) == 1 )
51         {
52                 switch( value[ 0 ] )
53                 {
54                         case 'q':
55                         case 'Q':
56                                 mlt_properties_set_int( properties, "done", 1 );
57                                 mlt_events_fire( jack, "jack-stop", NULL );
58                                 break;
59                         case '0':
60                                 position = 0;
61                                 mlt_producer_set_speed( producer, 1 );
62                                 mlt_producer_seek( producer, position );
63                                 mlt_consumer_purge( consumer );
64                                 mlt_events_fire( jack, "jack-seek", &position, NULL );
65                                 break;
66                         case '1':
67                                 mlt_producer_set_speed( producer, -10 );
68                                 break;
69                         case '2':
70                                 mlt_producer_set_speed( producer, -5 );
71                                 break;
72                         case '3':
73                                 mlt_producer_set_speed( producer, -2 );
74                                 break;
75                         case '4':
76                                 mlt_producer_set_speed( producer, -1 );
77                                 break;
78                         case '5':
79                                 mlt_producer_set_speed( producer, 0 );
80                                 mlt_consumer_purge( consumer );
81                                 mlt_events_fire( jack, "jack-stop", NULL );
82                                 break;
83                         case '6':
84                         case ' ':
85                                 if ( !jack || mlt_producer_get_speed( producer ) != 0 )
86                                         mlt_producer_set_speed( producer, 1 );
87                                 mlt_consumer_purge( consumer );
88                                 mlt_events_fire( jack, "jack-start", NULL );
89                                 break;
90                         case '7':
91                                 mlt_producer_set_speed( producer, 2 );
92                                 break;
93                         case '8':
94                                 mlt_producer_set_speed( producer, 5 );
95                                 break;
96                         case '9':
97                                 mlt_producer_set_speed( producer, 10 );
98                                 break;
99                         case 'd':
100                                 if ( multitrack != NULL )
101                                 {
102                                         int i = 0;
103                                         mlt_position last = -1;
104                                         fprintf( stderr, "\n" );
105                                         for ( i = 0; 1; i ++ )
106                                         {
107                                                 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
108                                                 if ( position == last )
109                                                         break;
110                                                 last = position;
111                                                 fprintf( stderr, "%d: %d\n", i, (int)position );
112                                         }
113                                 }
114                                 break;
115
116                         case 'g':
117                                 if ( multitrack != NULL )
118                                 {
119                                         position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
120                                         mlt_producer_seek( producer, position );
121                                         mlt_consumer_purge( consumer );
122                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
123                                 }
124                                 break;
125                         case 'H':
126                                 if ( producer != NULL )
127                                 {
128                                         position -= mlt_producer_get_fps( producer ) * 60;
129                                         mlt_consumer_purge( consumer );
130                                         mlt_producer_seek( producer, position );
131                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
132                                 }
133                                 break;
134                         case 'h':
135                                 if ( producer != NULL )
136                                 {
137                                         position--;
138                                         mlt_producer_set_speed( producer, 0 );
139                                         mlt_consumer_purge( consumer );
140                                         mlt_producer_seek( producer, position );
141                                         mlt_events_fire( jack, "jack-stop", NULL );
142                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
143                                 }
144                                 break;
145                         case 'j':
146                                 if ( multitrack != NULL )
147                                 {
148                                         position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
149                                         mlt_consumer_purge( consumer );
150                                         mlt_producer_seek( producer, position );
151                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
152                                 }
153                                 break;
154                         case 'k':
155                                 if ( multitrack != NULL )
156                                 {
157                                         position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
158                                         mlt_consumer_purge( consumer );
159                                         mlt_producer_seek( producer, position );
160                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
161                                 }
162                                 break;
163                         case 'l':
164                                 if ( producer != NULL )
165                                 {
166                                         position++;
167                                         mlt_consumer_purge( consumer );
168                                         if ( mlt_producer_get_speed( producer ) != 0 )
169                                         {
170                                                 mlt_producer_set_speed( producer, 0 );
171                                                 mlt_events_fire( jack, "jack-stop", NULL );
172                                         }
173                                         else
174                                         {
175                                                 mlt_producer_seek( producer, position );
176                                                 mlt_events_fire( jack, "jack-seek", &position, NULL );
177                                         }
178                                 }
179                                 break;
180                         case 'L':
181                                 if ( producer != NULL )
182                                 {
183                                         position += mlt_producer_get_fps( producer ) * 60;
184                                         mlt_consumer_purge( consumer );
185                                         mlt_producer_seek( producer, position );
186                                         mlt_events_fire( jack, "jack-seek", &position, NULL );
187                                 }
188                                 break;
189                 }
190
191                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
192         }
193
194         mlt_properties_set_int( properties, "stats_off", 0 );
195 }
196
197 static void on_jack_started( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
198 {
199         mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
200         if ( producer )
201         {
202                 if ( mlt_producer_get_speed( producer ) != 0 )
203                 {
204                         mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
205                         mlt_events_fire( jack, "jack-stop", NULL );
206                 }
207                 else
208                 {
209                         mlt_producer_set_speed( producer, 1 );
210                         mlt_consumer_purge( consumer );
211                         mlt_producer_seek( producer, *position );
212                         mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
213                 }
214         }
215 }
216
217 static void on_jack_stopped( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
218 {
219         mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
220         if ( producer )
221         {
222                 mlt_producer_set_speed( producer, 0 );
223                 mlt_consumer_purge( consumer );
224                 mlt_producer_seek( producer, *position );
225                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
226         }
227 }
228
229 static void setup_jack_transport( mlt_consumer consumer, mlt_profile profile )
230 {
231         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
232         mlt_filter jack = mlt_factory_filter( profile, "jackrack", NULL );
233         mlt_properties jack_properties = MLT_FILTER_PROPERTIES(jack);
234
235         mlt_service_attach( MLT_CONSUMER_SERVICE(consumer), jack );
236         mlt_properties_set_int( properties, "audio_off", 1 );
237         mlt_properties_set_data( properties, "jack_filter", jack, 0, (mlt_destructor) mlt_filter_close, NULL );
238 //      mlt_properties_set( jack_properties, "out_1", "system:playback_1" );
239 //      mlt_properties_set( jack_properties, "out_2", "system:playback_2" );
240         mlt_events_listen( jack_properties, consumer, "jack-started", (mlt_listener) on_jack_started );
241         mlt_events_listen( jack_properties, consumer, "jack-stopped", (mlt_listener) on_jack_stopped );
242 }
243
244 static mlt_consumer create_consumer( mlt_profile profile, char *id )
245 {
246         char *myid = id ? strdup( id ) : NULL;
247         char *arg = myid ? strchr( myid, ':' ) : NULL;
248         if ( arg != NULL )
249                 *arg ++ = '\0';
250         mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
251         if ( consumer != NULL )
252         {
253                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
254                 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
255         }
256         if ( myid )
257                 free( myid );
258         return consumer;
259 }
260
261 static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
262 {
263         int i;
264         for ( i = 1; i < argc; i ++ )
265         {
266                 if ( !strcmp( argv[ i ], "-consumer" ) )
267                 {
268                         if ( *consumer )
269                                 mlt_consumer_close( *consumer );
270                         *consumer = create_consumer( profile, argv[ ++ i ] );
271                         if ( *consumer )
272                         {
273                                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
274                                 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
275                                         mlt_properties_parse( properties, argv[ ++ i ] );
276                         }
277                 }
278         }
279 }
280
281 #if defined(__DARWIN__) || defined(WIN32)
282
283 static void event_handling( mlt_producer producer, mlt_consumer consumer )
284 {
285         SDL_Event event;
286
287         while ( SDL_PollEvent( &event ) )
288         {
289                 switch( event.type )
290                 {
291                         case SDL_QUIT:
292                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
293                                 break;
294
295                         case SDL_KEYDOWN:
296                                 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
297                                 {
298                                         char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
299                                         transport_action( producer, keyboard );
300                                 }
301                                 break;
302                 }
303         }
304 }
305
306 #endif
307
308 static void transport( mlt_producer producer, mlt_consumer consumer )
309 {
310         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
311         int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
312         int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
313         struct timespec tm = { 0, 40000 };
314         int total_length = mlt_producer_get_length( producer );
315         int last_position = 0;
316
317         if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
318         {
319                 if ( !silent && !progress )
320                 {
321                         term_init( );
322
323                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
324                         fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5=  0| |6=  1| |7=  2| |8=  5| |9= 10|\n" );
325                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
326
327                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
328                         fprintf( stderr, "|               H = back 1 minute,  L = forward 1 minute              |\n" );
329                         fprintf( stderr, "|                 h = previous frame,  l = next frame                 |\n" );
330                         fprintf( stderr, "|           g = start of clip, j = next clip, k = previous clip       |\n" );
331                         fprintf( stderr, "|                0 = restart, q = quit, space = play                  |\n" );
332                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
333                 }
334
335                 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
336                 {
337                         int value = ( silent || progress )? -1 : term_read( );
338
339                         if ( value != -1 )
340                         {
341                                 char string[ 2 ] = { value, 0 };
342                                 transport_action( producer, string );
343                         }
344
345 #if defined(__DARWIN__) || defined(WIN32)
346                         event_handling( producer, consumer );
347 #endif
348
349                         if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
350                         {
351                                 if ( progress )
352                                 {
353                                         int current_position = mlt_producer_position( producer );
354                                         if ( current_position > last_position )
355                                         {
356                                                 fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
357                                                         current_position, 100 * current_position / total_length );
358                                                 last_position = current_position;
359                                         }
360                                 }
361                                 else
362                                 {
363                                         fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
364                                 }
365                         }
366
367                         if ( silent || progress )
368                                 nanosleep( &tm, NULL );
369                 }
370
371                 if ( !silent )
372                         fprintf( stderr, "\n" );
373         }
374 }
375
376 static void show_usage( char *program_name )
377 {
378         fprintf( stderr,
379 "Usage: %s [options] [producer [name=value]* ]+\n"
380 "Options:\n"
381 "  -attach filter[:arg] [name=value]*       Attach a filter to the output\n"
382 "  -attach-cut filter[:arg] [name=value]*   Attach a filter to a cut\n"
383 "  -attach-track filter[:arg] [name=value]* Attach a filter to a track\n"
384 "  -attach-clip filter[:arg] [name=value]*  Attach a filter to a producer\n"
385 "  -audio-track | -hide-video               Add an audio-only track\n"
386 "  -blank frames                            Add blank silence to a track\n"
387 "  -consumer id[:arg] [name=value]*         Set the consumer (sink)\n"
388 "  -debug                                   Set the logging level to debug\n"
389 "  -filter filter[:arg] [name=value]*       Add a filter to the current track\n"
390 "  -group [name=value]*                     Apply properties repeatedly\n"
391 "  -help                                    Show this message\n"
392 "  -join clips                              Join multiple clips into one cut\n"
393 "  -mix length                              Add a mix between the last two cuts\n"
394 "  -mixer transition                        Add a transition to the mix\n"
395 "  -null-track | -hide-track                Add a hidden track\n"
396 "  -profile name                            Set the processing settings\n"
397 "  -progress                                Display progress along with position\n"
398 "  -remove                                  Remove the most recent cut\n"
399 "  -repeat times                            Repeat the last cut\n"
400 "  -query                                   List all of the registered services\n"
401 "  -query \"consumers\" | \"consumer\"=id       List consumers or show info about one\n"
402 "  -query \"filters\" | \"filter\"=id           List filters or show info about one\n"
403 "  -query \"producers\" | \"producer\"=id       List producers or show info about one\n"
404 "  -query \"transitions\" | \"transition\"=id   List transitions, show info about one\n"
405 "  -query \"profiles\" | \"profile\"=id         List profiles, show info about one\n"
406 "  -query \"presets\" | \"preset\"=id           List presets, show info about one\n"
407 "  -query \"formats\"                         List audio/video formats\n"
408 "  -query \"audio_codecs\"                    List audio codecs\n"
409 "  -query \"video_codecs\"                    List video codecs\n"
410 "  -serialise [filename]                    Write the commands to a text file\n"
411 "  -silent                                  Do not display position/transport\n"
412 "  -split relative-frame                    Split the last cut into two cuts\n"
413 "  -swap                                    Rearrange the last two cuts\n"
414 "  -track                                   Add a track\n"
415 "  -transition id[:arg] [name=value]*       Add a transition\n"
416 "  -verbose                                 Set the logging level to verbose\n"
417 "  -version                                 Show the version and copyright\n"
418 "  -video-track | -hide-audio               Add a video-only track\n"
419 "For more help: <http://www.mltframework.org/>\n",
420         basename( program_name ) );
421 }
422
423 static void guess_profile( mlt_producer melt, mlt_profile profile )
424 {
425         mlt_frame fr = NULL;
426         uint8_t *buffer;
427         mlt_image_format fmt = mlt_image_yuv422;
428         mlt_properties p;
429         int w = profile->width;
430         int h = profile->height;
431
432         if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 ) && fr )
433         {
434                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
435                 if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
436                 {
437                         // Some source properties are not exposed until after the first get_image call.
438                         mlt_frame_close( fr );
439                         mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 );
440                         p = MLT_FRAME_PROPERTIES( fr );
441 //                      mlt_properties_dump(p, stderr);
442                         if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
443                         {
444                                 profile->width = mlt_properties_get_int( p, "meta.media.width" );
445                                 profile->height = mlt_properties_get_int( p, "meta.media.height" );
446                                 profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
447                                 profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
448                                 profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
449                                 // AVCHD is mis-reported as double frame rate.
450                                 if ( profile->progressive == 0 && (
451                                      profile->frame_rate_num / profile->frame_rate_den == 50 ||
452                                      profile->frame_rate_num / profile->frame_rate_den == 59 ) )
453                                         profile->frame_rate_num /= 2;
454                                 profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
455                                 profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
456                                 profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
457                                 profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
458                                 profile->display_aspect_den = profile->height;
459                                 free( profile->description );
460                                 profile->description = strdup( "automatic" );
461                                 profile->is_explicit = 0;
462                         }
463                 }
464         }
465         mlt_frame_close( fr );
466         mlt_producer_seek( melt, 0 );
467 }
468
469 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
470 {
471         mlt_properties metadata = mlt_repository_metadata( repo, type, id );
472         if ( metadata )
473         {
474                 char *s = mlt_properties_serialise_yaml( metadata );
475                 fprintf( stderr, "%s", s );
476                 free( s );
477         }
478         else
479         {
480                 fprintf( stderr, "# No metadata for %s \"%s\"\n", typestr, id );
481         }
482 }
483
484 static void query_services( mlt_repository repo, mlt_service_type type )
485 {
486         mlt_properties services = NULL;
487         const char *typestr = NULL;
488         switch ( type )
489         {
490                 case consumer_type:
491                         services = mlt_repository_consumers( repo );
492                         typestr = "consumers";
493                         break;
494                 case filter_type:
495                         services = mlt_repository_filters( repo );
496                         typestr = "filters";
497                         break;
498                 case producer_type:
499                         services = mlt_repository_producers( repo );
500                         typestr = "producers";
501                         break;
502                 case transition_type:
503                         services = mlt_repository_transitions( repo );
504                         typestr = "transitions";
505                         break;
506                 default:
507                         return;
508         }
509         fprintf( stderr, "---\n%s:\n", typestr );
510         if ( services )
511         {
512                 int j;
513                 for ( j = 0; j < mlt_properties_count( services ); j++ )
514                         fprintf( stderr, "  - %s\n", mlt_properties_get_name( services, j ) );
515         }
516         fprintf( stderr, "...\n" );
517 }
518
519 static void query_profiles()
520 {
521         mlt_properties profiles = mlt_profile_list();
522         fprintf( stderr, "---\nprofiles:\n" );
523         if ( profiles )
524         {
525                 int j;
526                 for ( j = 0; j < mlt_properties_count( profiles ); j++ )
527                         fprintf( stderr, "  - %s\n", mlt_properties_get_name( profiles, j ) );
528         }
529         fprintf( stderr, "...\n" );
530         mlt_properties_close( profiles );
531 }
532
533 static void query_profile( const char *id )
534 {
535         mlt_properties profiles = mlt_profile_list();
536         mlt_properties profile = mlt_properties_get_data( profiles, id, NULL );
537         if ( profile )
538         {
539                 char *s = mlt_properties_serialise_yaml( profile );
540                 fprintf( stderr, "%s", s );
541                 free( s );
542         }
543         else
544         {
545                 fprintf( stderr, "# No metadata for profile \"%s\"\n", id );
546         }
547         mlt_properties_close( profiles );
548 }
549
550 static void query_presets()
551 {
552         mlt_properties presets = mlt_repository_presets();
553         fprintf( stderr, "---\npresets:\n" );
554         if ( presets )
555         {
556                 int j;
557                 for ( j = 0; j < mlt_properties_count( presets ); j++ )
558                         fprintf( stderr, "  - %s\n", mlt_properties_get_name( presets, j ) );
559         }
560         fprintf( stderr, "...\n" );
561         mlt_properties_close( presets );
562 }
563
564 static void query_preset( const char *id )
565 {
566         mlt_properties presets = mlt_repository_presets();
567         mlt_properties preset = mlt_properties_get_data( presets, id, NULL );
568         if ( preset )
569         {
570                 char *s = mlt_properties_serialise_yaml( preset );
571                 fprintf( stderr, "%s", s );
572                 free( s );
573         }
574         else
575         {
576                 fprintf( stderr, "# No metadata for preset \"%s\"\n", id );
577         }
578         mlt_properties_close( presets );
579 }
580
581 static void query_formats( )
582 {
583         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
584         if ( consumer )
585         {
586                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "f", "list" );
587                 mlt_consumer_start( consumer );
588                 mlt_consumer_close( consumer );
589         }
590         else
591         {
592                 fprintf( stderr, "# No formats - failed to load avformat consumer\n" );
593         }
594 }
595
596 static void query_acodecs( )
597 {
598         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
599         if ( consumer )
600         {
601                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "acodec", "list" );
602                 mlt_consumer_start( consumer );
603                 mlt_consumer_close( consumer );
604         }
605         else
606         {
607                 fprintf( stderr, "# No audio codecs - failed to load avformat consumer\n" );
608         }
609 }
610
611 static void query_vcodecs( )
612 {
613         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
614         if ( consumer )
615         {
616                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "vcodec", "list" );
617                 mlt_consumer_start( consumer );
618                 mlt_consumer_close( consumer );
619         }
620         else
621         {
622                 fprintf( stderr, "# No video codecs - failed to load avformat consumer\n" );
623         }
624 }
625
626 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
627 {
628         mlt_consumer_stop( consumer );
629         exit( EXIT_FAILURE );
630 }
631
632 int main( int argc, char **argv )
633 {
634         int i;
635         mlt_consumer consumer = NULL;
636         mlt_producer melt = NULL;
637         FILE *store = NULL;
638         char *name = NULL;
639         mlt_profile profile = NULL;
640         int is_progress = 0;
641         int is_silent = 0;
642         mlt_profile backup_profile;
643
644         // Construct the factory
645         mlt_repository repo = mlt_factory_init( NULL );
646
647 #ifdef WIN32
648         is_silent = 1;
649 #endif
650         
651         for ( i = 1; i < argc; i ++ )
652         {
653                 // Check for serialisation switch
654                 if ( !strcmp( argv[ i ], "-serialise" ) )
655                 {
656                         name = argv[ ++ i ];
657                         if ( name != NULL && strstr( name, ".melt" ) )
658                                 store = fopen( name, "w" );
659                         else
660                         {
661                                 if ( name == NULL || name[0] == '-' )
662                                         store = stdout;
663                                 name = NULL;
664                         }
665                 }
666                 // Look for the profile option
667                 else if ( !strcmp( argv[ i ], "-profile" ) )
668                 {
669                         const char *pname = argv[ ++ i ];
670                         if ( pname && pname[0] != '-' )
671                                 profile = mlt_profile_init( pname );
672                 }
673                 else if ( !strcmp( argv[ i ], "-progress" ) )
674                 {
675                         is_progress = 1;
676                 }
677                 // Look for the query option
678                 else if ( !strcmp( argv[ i ], "-query" ) )
679                 {
680                         const char *pname = argv[ ++ i ];
681                         if ( pname && pname[0] != '-' )
682                         {
683                                 if ( !strcmp( pname, "consumers" ) || !strcmp( pname, "consumer" ) )
684                                         query_services( repo, consumer_type );
685                                 else if ( !strcmp( pname, "filters" ) || !strcmp( pname, "filter" ) )
686                                         query_services( repo, filter_type );
687                                 else if ( !strcmp( pname, "producers" ) || !strcmp( pname, "producer" ) )
688                                         query_services( repo, producer_type );
689                                 else if ( !strcmp( pname, "transitions" ) || !strcmp( pname, "transition" ) )
690                                         query_services( repo, transition_type );
691                                 else if ( !strcmp( pname, "profiles" ) || !strcmp( pname, "profile" ) )
692                                         query_profiles();
693                                 else if ( !strcmp( pname, "presets" ) || !strcmp( pname, "preset" ) )
694                                         query_presets();
695                                 else if ( !strncmp( pname, "format", 6 ) )
696                                         query_formats();
697                                 else if ( !strncmp( pname, "acodec", 6 ) || !strcmp( pname, "audio_codecs" ) )
698                                         query_acodecs();
699                                 else if ( !strncmp( pname, "vcodec", 6 ) || !strcmp( pname, "video_codecs" ) )
700                                         query_vcodecs();
701
702                                 else if ( !strncmp( pname, "consumer=", 9 ) )
703                                         query_metadata( repo, consumer_type, "consumer", strchr( pname, '=' ) + 1 );
704                                 else if ( !strncmp( pname, "filter=", 7 ) )
705                                         query_metadata( repo, filter_type, "filter", strchr( pname, '=' ) + 1 );
706                                 else if ( !strncmp( pname, "producer=", 9 ) )
707                                         query_metadata( repo, producer_type, "producer", strchr( pname, '=' ) + 1 );
708                                 else if ( !strncmp( pname, "transition=", 11 ) )
709                                         query_metadata( repo, transition_type, "transition", strchr( pname, '=' ) + 1 );
710                                 else if ( !strncmp( pname, "profile=", 8 ) )
711                                         query_profile( strchr( pname, '=' ) + 1 );
712                                 else if ( !strncmp( pname, "preset=", 7 ) )
713                                         query_preset( strchr( pname, '=' ) + 1 );
714                                 else
715                                         goto query_all;
716                         }
717                         else
718                         {
719 query_all:
720                                 query_services( repo, consumer_type );
721                                 query_services( repo, filter_type );
722                                 query_services( repo, producer_type );
723                                 query_services( repo, transition_type );
724                                 fprintf( stderr, "# You can query the metadata for a specific service using:\n"
725                                         "# -query <type>=<identifer>\n"
726                                         "# where <type> is one of: consumer, filter, producer, or transition.\n" );
727                         }
728                         goto exit_factory;
729                 }
730                 else if ( !strcmp( argv[ i ], "-silent" ) )
731                 {
732                         is_silent = 1;
733                 }
734                 else if ( !strcmp( argv[ i ], "-verbose" ) )
735                 {
736                         mlt_log_set_level( MLT_LOG_VERBOSE );
737                 }
738                 else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
739                 {
740                         fprintf( stderr, "MLT %s " VERSION "\n"
741                                 "Copyright (C) 2002-2011 Ushodaya Enterprises Limited\n"
742                                 "<http://www.mltframework.org/>\n"
743                                 "This is free software; see the source for copying conditions.  There is NO\n"
744                                 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
745                                 basename( argv[0] ) );
746                         goto exit_factory;
747                 }
748                 else if ( !strcmp( argv[ i ], "-debug" ) )
749                 {
750                         mlt_log_set_level( MLT_LOG_DEBUG );
751                 }
752         }
753
754         // Create profile if not set explicitly
755         if ( getenv( "MLT_PROFILE" ) )
756                 profile = mlt_profile_init( NULL );
757         if ( profile == NULL )
758                 profile = mlt_profile_init( NULL );
759         else
760                 profile->is_explicit = 1;
761
762         // Look for the consumer option to load profile settings from consumer properties
763         backup_profile = mlt_profile_clone( profile );
764         load_consumer( &consumer, profile, argc, argv );
765
766         // If the consumer changed the profile, then it is explicit.
767         if ( backup_profile && !profile->is_explicit && (
768              profile->width != backup_profile->width ||
769              profile->height != backup_profile->height ||
770              profile->sample_aspect_num != backup_profile->sample_aspect_num ||
771              profile->sample_aspect_den != backup_profile->sample_aspect_den ||
772              profile->frame_rate_den != backup_profile->frame_rate_den ||
773              profile->frame_rate_num != backup_profile->frame_rate_num ||
774              profile->colorspace != backup_profile->colorspace ) )
775                 profile->is_explicit = 1;
776         mlt_profile_close( backup_profile );
777
778         // Get melt producer
779         if ( argc > 1 )
780                 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
781
782         if ( melt )
783         {
784                 // Generate an automatic profile if needed.
785                 if ( ! profile->is_explicit )
786                 {
787                         guess_profile( melt, profile );
788                         mlt_producer_close( melt );
789                         melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
790                 }
791                 
792                 // Reload the consumer with the fully qualified profile.
793                 // The producer or guess_profile could have changed the profile.
794                 load_consumer( &consumer, profile, argc, argv );
795
796                 // If we have no consumer, default to sdl
797                 if ( store == NULL && consumer == NULL )
798                         consumer = create_consumer( profile, NULL );
799         }
800         
801         // Set transport properties on consumer and produder
802         if ( consumer != NULL && melt != NULL )
803         {
804                 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES( consumer ), "transport_producer", melt, 0, NULL, NULL );
805                 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( melt ), "transport_consumer", consumer, 0, NULL, NULL );
806                 if ( is_progress )
807                         mlt_properties_set_int(  MLT_CONSUMER_PROPERTIES( consumer ), "progress", is_progress );
808                 if ( is_silent )
809                         mlt_properties_set_int(  MLT_CONSUMER_PROPERTIES( consumer ), "silent", is_silent );
810         }
811
812         if ( argc > 1 && melt != NULL && mlt_producer_get_length( melt ) > 0 )
813         {
814                 // Parse the arguments
815                 for ( i = 1; i < argc; i ++ )
816                 {
817                         if ( !strcmp( argv[ i ], "-jack" ) )
818                         {
819                                 setup_jack_transport( consumer, profile );
820                         }
821                         else if ( !strcmp( argv[ i ], "-serialise" ) )
822                         {
823                                 if ( store != stdout )
824                                         i ++;
825                         }
826                         else
827                         {
828                                 if ( store != NULL )
829                                         fprintf( store, "%s\n", argv[ i ] );
830
831                                 i ++;
832
833                                 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
834                                 {
835                                         if ( store != NULL )
836                                                 fprintf( store, "%s\n", argv[ i ] );
837                                         i += 1;
838                                 }
839
840                                 i --;
841                         }
842                 }
843
844                 if ( consumer != NULL && store == NULL )
845                 {
846                         // Get melt's properties
847                         mlt_properties melt_props = MLT_PRODUCER_PROPERTIES( melt );
848         
849                         // Get the last group
850                         mlt_properties group = mlt_properties_get_data( melt_props, "group", 0 );
851         
852                         // Apply group settings
853                         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
854                         mlt_properties_inherit( properties, group );
855
856                         // Connect consumer to melt
857                         mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( melt ) );
858
859                         // Start the consumer
860                         mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
861                         if ( mlt_consumer_start( consumer ) == 0 )
862                         {
863                                 // Transport functionality
864                                 transport( melt, consumer );
865                                 
866                                 // Stop the consumer
867                                 mlt_consumer_stop( consumer );
868                         }       
869                 }
870                 else if ( store != NULL && store != stdout && name != NULL )
871                 {
872                         fprintf( stderr, "Project saved as %s.\n", name );
873                         fclose( store );
874                 }
875         }
876         else
877         {
878                 show_usage( argv[0] );
879         }
880
881         // Close the producer
882         if ( melt != NULL )
883                 mlt_producer_close( melt );
884
885         // Close the consumer
886         if ( consumer != NULL )
887                 mlt_consumer_close( consumer );
888
889         // Close the factory
890         mlt_profile_close( profile );
891
892 exit_factory:
893                 
894         mlt_factory_close( );
895
896         return 0;
897 }