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