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