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