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