]> git.sesse.net Git - mlt/blob - src/melt/melt.c
Filter service metadata from being displayed if it is tagged as
[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         for ( i = 1; i < argc; i ++ )
266         {
267                 if ( !strcmp( argv[ i ], "-consumer" ) )
268                 {
269                         if ( *consumer )
270                                 mlt_consumer_close( *consumer );
271                         *consumer = create_consumer( profile, argv[ ++ i ] );
272                         if ( *consumer )
273                         {
274                                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
275                                 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
276                                         mlt_properties_parse( properties, argv[ ++ i ] );
277                         }
278                 }
279         }
280 }
281
282 #if defined(__DARWIN__) || defined(WIN32)
283
284 static void event_handling( mlt_producer producer, mlt_consumer consumer )
285 {
286         SDL_Event event;
287
288         while ( SDL_PollEvent( &event ) )
289         {
290                 switch( event.type )
291                 {
292                         case SDL_QUIT:
293                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
294                                 break;
295
296                         case SDL_KEYDOWN:
297                                 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
298                                 {
299                                         char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
300                                         transport_action( producer, keyboard );
301                                 }
302                                 break;
303                 }
304         }
305 }
306
307 #endif
308
309 static void transport( mlt_producer producer, mlt_consumer consumer )
310 {
311         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
312         int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
313         int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
314         struct timespec tm = { 0, 40000 };
315         int total_length = mlt_producer_get_length( producer );
316         int last_position = 0;
317
318         if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
319         {
320                 if ( !silent && !progress )
321                 {
322                         term_init( );
323
324                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
325                         fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5=  0| |6=  1| |7=  2| |8=  5| |9= 10|\n" );
326                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
327
328                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
329                         fprintf( stderr, "|               H = back 1 minute,  L = forward 1 minute              |\n" );
330                         fprintf( stderr, "|                 h = previous frame,  l = next frame                 |\n" );
331                         fprintf( stderr, "|           g = start of clip, j = next clip, k = previous clip       |\n" );
332                         fprintf( stderr, "|                0 = restart, q = quit, space = play                  |\n" );
333                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
334                 }
335
336                 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
337                 {
338                         int value = ( silent || progress )? -1 : term_read( );
339
340                         if ( value != -1 )
341                         {
342                                 char string[ 2 ] = { value, 0 };
343                                 transport_action( producer, string );
344                         }
345
346 #if defined(__DARWIN__) || defined(WIN32)
347                         event_handling( producer, consumer );
348 #endif
349
350                         if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
351                         {
352                                 if ( progress )
353                                 {
354                                         int current_position = mlt_producer_position( producer );
355                                         if ( current_position > last_position )
356                                         {
357                                                 fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
358                                                         current_position, 100 * current_position / total_length );
359                                                 last_position = current_position;
360                                         }
361                                 }
362                                 else
363                                 {
364                                         fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
365                                 }
366                         }
367
368                         if ( silent || progress )
369                                 nanosleep( &tm, NULL );
370                 }
371
372                 if ( !silent )
373                         fprintf( stderr, "\n" );
374         }
375 }
376
377 static void show_usage( char *program_name )
378 {
379         fprintf( stdout,
380 "Usage: %s [options] [producer [name=value]* ]+\n"
381 "Options:\n"
382 "  -attach filter[:arg] [name=value]*       Attach a filter to the output\n"
383 "  -attach-cut filter[:arg] [name=value]*   Attach a filter to a cut\n"
384 "  -attach-track filter[:arg] [name=value]* Attach a filter to a track\n"
385 "  -attach-clip filter[:arg] [name=value]*  Attach a filter to a producer\n"
386 "  -audio-track | -hide-video               Add an audio-only track\n"
387 "  -blank frames                            Add blank silence to a track\n"
388 "  -consumer id[:arg] [name=value]*         Set the consumer (sink)\n"
389 "  -debug                                   Set the logging level to debug\n"
390 "  -filter filter[:arg] [name=value]*       Add a filter to the current track\n"
391 "  -group [name=value]*                     Apply properties repeatedly\n"
392 "  -help                                    Show this message\n"
393 "  -jack                                    Enable JACK transport synchronization\n"
394 "  -join clips                              Join multiple clips into one cut\n"
395 "  -mix length                              Add a mix between the last two cuts\n"
396 "  -mixer transition                        Add a transition to the mix\n"
397 "  -null-track | -hide-track                Add a hidden track\n"
398 "  -profile name                            Set the processing settings\n"
399 "  -progress                                Display progress along with position\n"
400 "  -remove                                  Remove the most recent cut\n"
401 "  -repeat times                            Repeat the last cut\n"
402 "  -query                                   List all of the registered services\n"
403 "  -query \"consumers\" | \"consumer\"=id       List consumers or show info about one\n"
404 "  -query \"filters\" | \"filter\"=id           List filters or show info about one\n"
405 "  -query \"producers\" | \"producer\"=id       List producers or show info about one\n"
406 "  -query \"transitions\" | \"transition\"=id   List transitions, show info about one\n"
407 "  -query \"profiles\" | \"profile\"=id         List profiles, show info about one\n"
408 "  -query \"presets\" | \"preset\"=id           List presets, show info about one\n"
409 "  -query \"formats\"                         List audio/video formats\n"
410 "  -query \"audio_codecs\"                    List audio codecs\n"
411 "  -query \"video_codecs\"                    List video codecs\n"
412 "  -serialise [filename]                    Write the commands to a text file\n"
413 "  -silent                                  Do not display position/transport\n"
414 "  -split relative-frame                    Split the last cut into two cuts\n"
415 "  -swap                                    Rearrange the last two cuts\n"
416 "  -track                                   Add a track\n"
417 "  -transition id[:arg] [name=value]*       Add a transition\n"
418 "  -verbose                                 Set the logging level to verbose\n"
419 "  -version                                 Show the version and copyright\n"
420 "  -video-track | -hide-audio               Add a video-only track\n"
421 "For more help: <http://www.mltframework.org/>\n",
422         basename( program_name ) );
423 }
424
425 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
426 {
427         mlt_properties metadata = mlt_repository_metadata( repo, type, id );
428         if ( metadata )
429         {
430                 char *s = mlt_properties_serialise_yaml( metadata );
431                 fprintf( stdout, "%s", s );
432                 free( s );
433         }
434         else
435         {
436                 fprintf( stdout, "# No metadata for %s \"%s\"\n", typestr, id );
437         }
438 }
439
440 static int is_service_hidden(mlt_repository repo, mlt_service_type type, const char *service_name )
441 {
442         mlt_properties metadata = NULL;
443         mlt_properties tags = NULL;
444         metadata = mlt_repository_metadata(repo, type, service_name);
445
446         if( metadata )
447         {
448                 tags = mlt_properties_get_data( metadata, "tags", NULL );
449                 if( tags )
450                 {
451                         int k;
452                         for ( k = 0; k < mlt_properties_count( tags ); k++ )
453                         {
454                                 const char* value = mlt_properties_get_value(tags, k);
455                                 if( !strcmp("Hidden", value) )
456                                 {
457                                         return 1;
458                                 }
459                         }
460                 }
461         }
462         return 0;
463 }
464
465 static void query_services( mlt_repository repo, mlt_service_type type )
466 {
467         mlt_properties services = NULL;
468         const char *typestr = NULL;
469         switch ( type )
470         {
471                 case consumer_type:
472                         services = mlt_repository_consumers( repo );
473                         typestr = "consumers";
474                         break;
475                 case filter_type:
476                         services = mlt_repository_filters( repo );
477                         typestr = "filters";
478                         break;
479                 case producer_type:
480                         services = mlt_repository_producers( repo );
481                         typestr = "producers";
482                         break;
483                 case transition_type:
484                         services = mlt_repository_transitions( repo );
485                         typestr = "transitions";
486                         break;
487                 default:
488                         return;
489         }
490         fprintf( stdout, "---\n%s:\n", typestr );
491         if ( services )
492         {
493                 int j;
494                 for ( j = 0; j < mlt_properties_count( services ); j++ )
495                 {
496                         const char* service_name = mlt_properties_get_name( services, j );
497                         if( !is_service_hidden(repo, type, service_name ) )
498                                 fprintf( stdout, "  - %s\n", service_name );
499                 }
500         }
501         fprintf( stdout, "...\n" );
502 }
503
504 static void query_profiles()
505 {
506         mlt_properties profiles = mlt_profile_list();
507         fprintf( stdout, "---\nprofiles:\n" );
508         if ( profiles )
509         {
510                 int j;
511                 for ( j = 0; j < mlt_properties_count( profiles ); j++ )
512                         fprintf( stdout, "  - %s\n", mlt_properties_get_name( profiles, j ) );
513         }
514         fprintf( stdout, "...\n" );
515         mlt_properties_close( profiles );
516 }
517
518 static void query_profile( const char *id )
519 {
520         mlt_properties profiles = mlt_profile_list();
521         mlt_properties profile = mlt_properties_get_data( profiles, id, NULL );
522         if ( profile )
523         {
524                 char *s = mlt_properties_serialise_yaml( profile );
525                 fprintf( stdout, "%s", s );
526                 free( s );
527         }
528         else
529         {
530                 fprintf( stdout, "# No metadata for profile \"%s\"\n", id );
531         }
532         mlt_properties_close( profiles );
533 }
534
535 static void query_presets()
536 {
537         mlt_properties presets = mlt_repository_presets();
538         fprintf( stdout, "---\npresets:\n" );
539         if ( presets )
540         {
541                 int j;
542                 for ( j = 0; j < mlt_properties_count( presets ); j++ )
543                         fprintf( stdout, "  - %s\n", mlt_properties_get_name( presets, j ) );
544         }
545         fprintf( stdout, "...\n" );
546         mlt_properties_close( presets );
547 }
548
549 static void query_preset( const char *id )
550 {
551         mlt_properties presets = mlt_repository_presets();
552         mlt_properties preset = mlt_properties_get_data( presets, id, NULL );
553         if ( preset )
554         {
555                 char *s = mlt_properties_serialise_yaml( preset );
556                 fprintf( stdout, "%s", s );
557                 free( s );
558         }
559         else
560         {
561                 fprintf( stdout, "# No metadata for preset \"%s\"\n", id );
562         }
563         mlt_properties_close( presets );
564 }
565
566 static void query_formats( )
567 {
568         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
569         if ( consumer )
570         {
571                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "f", "list" );
572                 mlt_consumer_start( consumer );
573                 mlt_consumer_close( consumer );
574         }
575         else
576         {
577                 fprintf( stdout, "# No formats - failed to load avformat consumer\n" );
578         }
579 }
580
581 static void query_acodecs( )
582 {
583         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
584         if ( consumer )
585         {
586                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "acodec", "list" );
587                 mlt_consumer_start( consumer );
588                 mlt_consumer_close( consumer );
589         }
590         else
591         {
592                 fprintf( stdout, "# No audio codecs - failed to load avformat consumer\n" );
593         }
594 }
595
596 static void query_vcodecs( )
597 {
598         mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
599         if ( consumer )
600         {
601                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "vcodec", "list" );
602                 mlt_consumer_start( consumer );
603                 mlt_consumer_close( consumer );
604         }
605         else
606         {
607                 fprintf( stdout, "# No video codecs - failed to load avformat consumer\n" );
608         }
609 }
610
611 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
612 {
613         mlt_consumer_stop( consumer );
614         exit( EXIT_FAILURE );
615 }
616
617 int main( int argc, char **argv )
618 {
619         int i;
620         mlt_consumer consumer = NULL;
621         mlt_producer melt = NULL;
622         FILE *store = NULL;
623         char *name = NULL;
624         mlt_profile profile = NULL;
625         int is_progress = 0;
626         int is_silent = 0;
627         mlt_profile backup_profile;
628
629         // Construct the factory
630         mlt_repository repo = mlt_factory_init( NULL );
631
632 #ifdef WIN32
633         is_silent = 1;
634 #endif
635         
636         for ( i = 1; i < argc; i ++ )
637         {
638                 // Check for serialisation switch
639                 if ( !strcmp( argv[ i ], "-serialise" ) )
640                 {
641                         name = argv[ ++ i ];
642                         if ( name != NULL && strstr( name, ".melt" ) )
643                                 store = fopen( name, "w" );
644                         else
645                         {
646                                 if ( name == NULL || name[0] == '-' )
647                                         store = stdout;
648                                 name = NULL;
649                         }
650                 }
651                 // Look for the profile option
652                 else if ( !strcmp( argv[ i ], "-profile" ) )
653                 {
654                         const char *pname = argv[ ++ i ];
655                         if ( pname && pname[0] != '-' )
656                                 profile = mlt_profile_init( pname );
657                 }
658                 else if ( !strcmp( argv[ i ], "-progress" ) )
659                 {
660                         is_progress = 1;
661                 }
662                 // Look for the query option
663                 else if ( !strcmp( argv[ i ], "-query" ) )
664                 {
665                         const char *pname = argv[ ++ i ];
666                         if ( pname && pname[0] != '-' )
667                         {
668                                 if ( !strcmp( pname, "consumers" ) || !strcmp( pname, "consumer" ) )
669                                         query_services( repo, consumer_type );
670                                 else if ( !strcmp( pname, "filters" ) || !strcmp( pname, "filter" ) )
671                                         query_services( repo, filter_type );
672                                 else if ( !strcmp( pname, "producers" ) || !strcmp( pname, "producer" ) )
673                                         query_services( repo, producer_type );
674                                 else if ( !strcmp( pname, "transitions" ) || !strcmp( pname, "transition" ) )
675                                         query_services( repo, transition_type );
676                                 else if ( !strcmp( pname, "profiles" ) || !strcmp( pname, "profile" ) )
677                                         query_profiles();
678                                 else if ( !strcmp( pname, "presets" ) || !strcmp( pname, "preset" ) )
679                                         query_presets();
680                                 else if ( !strncmp( pname, "format", 6 ) )
681                                         query_formats();
682                                 else if ( !strncmp( pname, "acodec", 6 ) || !strcmp( pname, "audio_codecs" ) )
683                                         query_acodecs();
684                                 else if ( !strncmp( pname, "vcodec", 6 ) || !strcmp( pname, "video_codecs" ) )
685                                         query_vcodecs();
686
687                                 else if ( !strncmp( pname, "consumer=", 9 ) )
688                                         query_metadata( repo, consumer_type, "consumer", strchr( pname, '=' ) + 1 );
689                                 else if ( !strncmp( pname, "filter=", 7 ) )
690                                         query_metadata( repo, filter_type, "filter", strchr( pname, '=' ) + 1 );
691                                 else if ( !strncmp( pname, "producer=", 9 ) )
692                                         query_metadata( repo, producer_type, "producer", strchr( pname, '=' ) + 1 );
693                                 else if ( !strncmp( pname, "transition=", 11 ) )
694                                         query_metadata( repo, transition_type, "transition", strchr( pname, '=' ) + 1 );
695                                 else if ( !strncmp( pname, "profile=", 8 ) )
696                                         query_profile( strchr( pname, '=' ) + 1 );
697                                 else if ( !strncmp( pname, "preset=", 7 ) )
698                                         query_preset( strchr( pname, '=' ) + 1 );
699                                 else
700                                         goto query_all;
701                         }
702                         else
703                         {
704 query_all:
705                                 query_services( repo, consumer_type );
706                                 query_services( repo, filter_type );
707                                 query_services( repo, producer_type );
708                                 query_services( repo, transition_type );
709                                 fprintf( stdout, "# You can query the metadata for a specific service using:\n"
710                                         "# -query <type>=<identifer>\n"
711                                         "# where <type> is one of: consumer, filter, producer, or transition.\n" );
712                         }
713                         goto exit_factory;
714                 }
715                 else if ( !strcmp( argv[ i ], "-silent" ) )
716                 {
717                         is_silent = 1;
718                 }
719                 else if ( !strcmp( argv[ i ], "-verbose" ) )
720                 {
721                         mlt_log_set_level( MLT_LOG_VERBOSE );
722                 }
723                 else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
724                 {
725                         fprintf( stdout, "MLT %s " VERSION "\n"
726                                 "Copyright (C) 2002-2011 Ushodaya Enterprises Limited\n"
727                                 "<http://www.mltframework.org/>\n"
728                                 "This is free software; see the source for copying conditions.  There is NO\n"
729                                 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
730                                 basename( argv[0] ) );
731                         goto exit_factory;
732                 }
733                 else if ( !strcmp( argv[ i ], "-debug" ) )
734                 {
735                         mlt_log_set_level( MLT_LOG_DEBUG );
736                 }
737         }
738         if ( !is_silent && !isatty( STDIN_FILENO ) )
739                 is_progress = 1;
740
741         // Create profile if not set explicitly
742         if ( getenv( "MLT_PROFILE" ) )
743                 profile = mlt_profile_init( NULL );
744         if ( profile == NULL )
745                 profile = mlt_profile_init( NULL );
746         else
747                 profile->is_explicit = 1;
748
749         // Look for the consumer option to load profile settings from consumer properties
750         backup_profile = mlt_profile_clone( profile );
751         load_consumer( &consumer, profile, argc, argv );
752
753         // If the consumer changed the profile, then it is explicit.
754         if ( backup_profile && !profile->is_explicit && (
755              profile->width != backup_profile->width ||
756              profile->height != backup_profile->height ||
757              profile->sample_aspect_num != backup_profile->sample_aspect_num ||
758              profile->sample_aspect_den != backup_profile->sample_aspect_den ||
759              profile->frame_rate_den != backup_profile->frame_rate_den ||
760              profile->frame_rate_num != backup_profile->frame_rate_num ||
761              profile->colorspace != backup_profile->colorspace ) )
762                 profile->is_explicit = 1;
763         mlt_profile_close( backup_profile );
764
765         // Get melt producer
766         if ( argc > 1 )
767                 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
768
769         if ( melt )
770         {
771                 // Generate an automatic profile if needed.
772                 if ( ! profile->is_explicit )
773                 {
774                         mlt_profile_from_producer( profile, melt );
775                         mlt_producer_close( melt );
776                         melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
777                 }
778                 
779                 // Reload the consumer with the fully qualified profile.
780                 // The producer or auto-profile could have changed the profile.
781                 load_consumer( &consumer, profile, argc, argv );
782
783                 // If we have no consumer, default to sdl
784                 if ( store == NULL && consumer == NULL )
785                         consumer = create_consumer( profile, NULL );
786         }
787         
788         // Set transport properties on consumer and produder
789         if ( consumer != NULL && melt != NULL )
790         {
791                 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES( consumer ), "transport_producer", melt, 0, NULL, NULL );
792                 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( melt ), "transport_consumer", consumer, 0, NULL, NULL );
793                 if ( is_progress )
794                         mlt_properties_set_int(  MLT_CONSUMER_PROPERTIES( consumer ), "progress", is_progress );
795                 if ( is_silent )
796                         mlt_properties_set_int(  MLT_CONSUMER_PROPERTIES( consumer ), "silent", is_silent );
797         }
798
799         if ( argc > 1 && melt != NULL && mlt_producer_get_length( melt ) > 0 )
800         {
801                 // Parse the arguments
802                 for ( i = 1; i < argc; i ++ )
803                 {
804                         if ( !strcmp( argv[ i ], "-jack" ) )
805                         {
806                                 setup_jack_transport( consumer, profile );
807                         }
808                         else if ( !strcmp( argv[ i ], "-serialise" ) )
809                         {
810                                 if ( store != stdout )
811                                         i ++;
812                         }
813                         else
814                         {
815                                 if ( store != NULL )
816                                         fprintf( store, "%s\n", argv[ i ] );
817
818                                 i ++;
819
820                                 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
821                                 {
822                                         if ( store != NULL )
823                                                 fprintf( store, "%s\n", argv[ i ] );
824                                         i += 1;
825                                 }
826
827                                 i --;
828                         }
829                 }
830
831                 if ( consumer != NULL && store == NULL )
832                 {
833                         // Get melt's properties
834                         mlt_properties melt_props = MLT_PRODUCER_PROPERTIES( melt );
835         
836                         // Get the last group
837                         mlt_properties group = mlt_properties_get_data( melt_props, "group", 0 );
838         
839                         // Apply group settings
840                         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
841                         mlt_properties_inherit( properties, group );
842
843                         // Connect consumer to melt
844                         mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( melt ) );
845
846                         // Start the consumer
847                         mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
848                         if ( mlt_consumer_start( consumer ) == 0 )
849                         {
850                                 // Transport functionality
851                                 transport( melt, consumer );
852                                 
853                                 // Stop the consumer
854                                 mlt_consumer_stop( consumer );
855                         }       
856                 }
857                 else if ( store != NULL && store != stdout && name != NULL )
858                 {
859                         fprintf( stderr, "Project saved as %s.\n", name );
860                         fclose( store );
861                 }
862         }
863         else
864         {
865                 show_usage( argv[0] );
866         }
867
868         // Close the producer
869         if ( melt != NULL )
870                 mlt_producer_close( melt );
871
872         // Close the consumer
873         if ( consumer != NULL )
874                 mlt_consumer_close( consumer );
875
876         // Close the factory
877         mlt_profile_close( profile );
878
879 exit_factory:
880                 
881         mlt_factory_close( );
882
883         return 0;
884 }