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>
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.
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.
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.
34 #include <framework/mlt.h>
36 #if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
42 static mlt_producer melt = NULL;
44 static void stop_handler(int signum)
48 mlt_properties properties = MLT_PRODUCER_PROPERTIES( melt );
49 mlt_properties_set_int( properties, "done", 1 );
53 static void transport_action( mlt_producer producer, char *value )
55 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
56 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
57 mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
58 mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
59 mlt_position position = producer? mlt_producer_position( producer ) : 0;
61 mlt_properties_set_int( properties, "stats_off", 1 );
63 if ( strlen( value ) == 1 )
69 mlt_properties_set_int( properties, "done", 1 );
70 mlt_events_fire( jack, "jack-stop", NULL );
74 mlt_producer_set_speed( producer, 1 );
75 mlt_producer_seek( producer, position );
76 mlt_consumer_purge( consumer );
77 mlt_events_fire( jack, "jack-seek", &position, NULL );
80 mlt_producer_set_speed( producer, -10 );
83 mlt_producer_set_speed( producer, -5 );
86 mlt_producer_set_speed( producer, -2 );
89 mlt_producer_set_speed( producer, -1 );
92 mlt_producer_set_speed( producer, 0 );
93 mlt_consumer_purge( consumer );
94 mlt_events_fire( jack, "jack-stop", NULL );
98 if ( !jack || mlt_producer_get_speed( producer ) != 0 )
99 mlt_producer_set_speed( producer, 1 );
100 mlt_consumer_purge( consumer );
101 mlt_events_fire( jack, "jack-start", NULL );
104 mlt_producer_set_speed( producer, 2 );
107 mlt_producer_set_speed( producer, 5 );
110 mlt_producer_set_speed( producer, 10 );
113 if ( multitrack != NULL )
116 mlt_position last = -1;
117 fprintf( stderr, "\n" );
118 for ( i = 0; 1; i ++ )
120 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
121 if ( position == last )
124 fprintf( stderr, "%d: %d\n", i, (int)position );
130 if ( multitrack != NULL )
132 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
133 mlt_producer_seek( producer, position );
134 mlt_consumer_purge( consumer );
135 mlt_events_fire( jack, "jack-seek", &position, NULL );
139 if ( producer != NULL )
141 position -= mlt_producer_get_fps( producer ) * 60;
142 mlt_consumer_purge( consumer );
143 mlt_producer_seek( producer, position );
144 mlt_events_fire( jack, "jack-seek", &position, NULL );
148 if ( producer != NULL )
151 mlt_producer_set_speed( producer, 0 );
152 mlt_consumer_purge( consumer );
153 mlt_producer_seek( producer, position );
154 mlt_events_fire( jack, "jack-stop", NULL );
155 mlt_events_fire( jack, "jack-seek", &position, NULL );
159 if ( multitrack != NULL )
161 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
162 mlt_consumer_purge( consumer );
163 mlt_producer_seek( producer, position );
164 mlt_events_fire( jack, "jack-seek", &position, NULL );
168 if ( multitrack != NULL )
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 );
177 if ( producer != NULL )
180 mlt_consumer_purge( consumer );
181 if ( mlt_producer_get_speed( producer ) != 0 )
183 mlt_producer_set_speed( producer, 0 );
184 mlt_events_fire( jack, "jack-stop", NULL );
188 mlt_producer_seek( producer, position );
189 mlt_events_fire( jack, "jack-seek", &position, NULL );
194 if ( producer != NULL )
196 position += mlt_producer_get_fps( producer ) * 60;
197 mlt_consumer_purge( consumer );
198 mlt_producer_seek( producer, position );
199 mlt_events_fire( jack, "jack-seek", &position, NULL );
204 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
207 mlt_properties_set_int( properties, "stats_off", 0 );
210 static void on_jack_started( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
212 mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
215 if ( mlt_producer_get_speed( producer ) != 0 )
217 mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
218 mlt_events_fire( jack, "jack-stop", NULL );
222 mlt_producer_set_speed( producer, 1 );
223 mlt_consumer_purge( consumer );
224 mlt_producer_seek( producer, *position );
225 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
230 static void on_jack_stopped( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
232 mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
235 mlt_producer_set_speed( producer, 0 );
236 mlt_consumer_purge( consumer );
237 mlt_producer_seek( producer, *position );
238 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
242 static void setup_jack_transport( mlt_consumer consumer, mlt_profile profile )
244 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
245 mlt_filter jack = mlt_factory_filter( profile, "jackrack", NULL );
246 mlt_properties jack_properties = MLT_FILTER_PROPERTIES(jack);
248 mlt_service_attach( MLT_CONSUMER_SERVICE(consumer), jack );
249 mlt_properties_set_int( properties, "audio_off", 1 );
250 mlt_properties_set_data( properties, "jack_filter", jack, 0, (mlt_destructor) mlt_filter_close, NULL );
251 // mlt_properties_set( jack_properties, "out_1", "system:playback_1" );
252 // mlt_properties_set( jack_properties, "out_2", "system:playback_2" );
253 mlt_events_listen( jack_properties, consumer, "jack-started", (mlt_listener) on_jack_started );
254 mlt_events_listen( jack_properties, consumer, "jack-stopped", (mlt_listener) on_jack_stopped );
257 static mlt_consumer create_consumer( mlt_profile profile, char *id )
259 char *myid = id ? strdup( id ) : NULL;
260 char *arg = myid ? strchr( myid, ':' ) : NULL;
263 mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
264 if ( consumer != NULL )
266 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
267 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
274 static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
279 for ( i = 1; i < argc; i ++ )
280 multi += !strcmp( argv[ i ], "-consumer" );
284 // If there is more than one -consumer use the 'multi' consumer.
289 mlt_consumer_close( *consumer );
290 *consumer = create_consumer( profile, "multi" );
291 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
292 for ( i = 1; i < argc; i ++ )
294 if ( !strcmp( argv[ i ], "-consumer" ) && argv[ i + 1 ])
296 // Create a properties object for each sub-consumer
297 mlt_properties new_props = mlt_properties_new();
298 snprintf( key, sizeof(key), "%d", k++ );
299 mlt_properties_set_data( properties, key, new_props, 0,
300 (mlt_destructor) mlt_properties_close, NULL );
301 if ( strchr( argv[i + 1], ':' ) )
303 char *temp = strdup( argv[++i] );
304 char *service = temp;
305 char *target = strchr( temp, ':' );
307 mlt_properties_set( new_props, "mlt_service", service );
308 mlt_properties_set( new_props, "target", target );
312 mlt_properties_set( new_props, "mlt_service", argv[ ++i ] );
314 while ( argv[ i + 1 ] && strchr( argv[ i + 1 ], '=' ) )
315 mlt_properties_parse( new_props, argv[ ++ i ] );
319 else for ( i = 1; i < argc; i ++ )
321 if ( !strcmp( argv[ i ], "-consumer" ) )
324 mlt_consumer_close( *consumer );
325 *consumer = create_consumer( profile, argv[ ++ i ] );
328 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
329 while ( argv[ i + 1 ] != NULL && strchr( argv[ i + 1 ], '=' ) )
330 mlt_properties_parse( properties, argv[ ++ i ] );
336 #if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
338 static void event_handling( mlt_producer producer, mlt_consumer consumer )
342 while ( SDL_PollEvent( &event ) )
347 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
351 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
353 char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
354 transport_action( producer, keyboard );
363 static void transport( mlt_producer producer, mlt_consumer consumer )
365 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
366 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
367 int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
368 struct timespec tm = { 0, 40000000 };
369 int total_length = mlt_producer_get_length( producer );
370 int last_position = 0;
372 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
374 if ( !silent && !progress )
378 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
379 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
380 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
382 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
383 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
384 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
385 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
386 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
387 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
390 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
392 int value = ( silent || progress )? -1 : term_read( );
396 char string[ 2 ] = { value, 0 };
397 transport_action( producer, string );
400 #if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
401 event_handling( producer, consumer );
404 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
408 int current_position = mlt_producer_position( producer );
409 if ( current_position > last_position )
411 fprintf( stderr, "Current Frame: %10d, percentage: %10d%c",
412 current_position, 100 * current_position / total_length,
413 progress == 2 ? '\n' : '\r' );
414 last_position = current_position;
419 fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
424 if ( silent || progress )
425 nanosleep( &tm, NULL );
429 fprintf( stderr, "\n" );
433 static void show_usage( char *program_name )
436 "Usage: %s [options] [producer [name=value]* ]+\n"
438 " -attach filter[:arg] [name=value]* Attach a filter to the output\n"
439 " -attach-cut filter[:arg] [name=value]* Attach a filter to a cut\n"
440 " -attach-track filter[:arg] [name=value]* Attach a filter to a track\n"
441 " -attach-clip filter[:arg] [name=value]* Attach a filter to a producer\n"
442 " -audio-track | -hide-video Add an audio-only track\n"
443 " -blank frames Add blank silence to a track\n"
444 " -consumer id[:arg] [name=value]* Set the consumer (sink)\n"
445 " -debug Set the logging level to debug\n"
446 " -filter filter[:arg] [name=value]* Add a filter to the current track\n"
447 " -group [name=value]* Apply properties repeatedly\n"
448 " -help Show this message\n"
449 " -jack Enable JACK transport synchronization\n"
450 " -join clips Join multiple clips into one cut\n"
451 " -mix length Add a mix between the last two cuts\n"
452 " -mixer transition Add a transition to the mix\n"
453 " -null-track | -hide-track Add a hidden track\n"
454 " -profile name Set the processing settings\n"
455 " -progress Display progress along with position\n"
456 " -remove Remove the most recent cut\n"
457 " -repeat times Repeat the last cut\n"
458 " -query List all of the registered services\n"
459 " -query \"consumers\" | \"consumer\"=id List consumers or show info about one\n"
460 " -query \"filters\" | \"filter\"=id List filters or show info about one\n"
461 " -query \"producers\" | \"producer\"=id List producers or show info about one\n"
462 " -query \"transitions\" | \"transition\"=id List transitions, show info about one\n"
463 " -query \"profiles\" | \"profile\"=id List profiles, show info about one\n"
464 " -query \"presets\" | \"preset\"=id List presets, show info about one\n"
465 " -query \"formats\" List audio/video formats\n"
466 " -query \"audio_codecs\" List audio codecs\n"
467 " -query \"video_codecs\" List video codecs\n"
468 " -serialise [filename] Write the commands to a text file\n"
469 " -silent Do not display position/transport\n"
470 " -split relative-frame Split the last cut into two cuts\n"
471 " -swap Rearrange the last two cuts\n"
472 " -track Add a track\n"
473 " -transition id[:arg] [name=value]* Add a transition\n"
474 " -verbose Set the logging level to verbose\n"
475 " -version Show the version and copyright\n"
476 " -video-track | -hide-audio Add a video-only track\n"
477 "For more help: <http://www.mltframework.org/>\n",
478 basename( program_name ) );
481 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
483 mlt_properties metadata = mlt_repository_metadata( repo, type, id );
486 char *s = mlt_properties_serialise_yaml( metadata );
487 fprintf( stdout, "%s", s );
492 fprintf( stdout, "# No metadata for %s \"%s\"\n", typestr, id );
496 static int is_service_hidden(mlt_repository repo, mlt_service_type type, const char *service_name )
498 mlt_properties metadata = NULL;
499 mlt_properties tags = NULL;
500 metadata = mlt_repository_metadata(repo, type, service_name);
504 tags = mlt_properties_get_data( metadata, "tags", NULL );
508 for ( k = 0; k < mlt_properties_count( tags ); k++ )
510 const char* value = mlt_properties_get_value(tags, k);
511 if( !strcmp("Hidden", value) )
521 static void query_services( mlt_repository repo, mlt_service_type type )
523 mlt_properties services = NULL;
524 const char *typestr = NULL;
528 services = mlt_repository_consumers( repo );
529 typestr = "consumers";
532 services = mlt_repository_filters( repo );
536 services = mlt_repository_producers( repo );
537 typestr = "producers";
539 case transition_type:
540 services = mlt_repository_transitions( repo );
541 typestr = "transitions";
546 fprintf( stdout, "---\n%s:\n", typestr );
550 for ( j = 0; j < mlt_properties_count( services ); j++ )
552 const char* service_name = mlt_properties_get_name( services, j );
553 if( !is_service_hidden(repo, type, service_name ) )
554 fprintf( stdout, " - %s\n", service_name );
557 fprintf( stdout, "...\n" );
560 static void query_profiles()
562 mlt_properties profiles = mlt_profile_list();
563 fprintf( stdout, "---\nprofiles:\n" );
567 for ( j = 0; j < mlt_properties_count( profiles ); j++ )
568 fprintf( stdout, " - %s\n", mlt_properties_get_name( profiles, j ) );
570 fprintf( stdout, "...\n" );
571 mlt_properties_close( profiles );
574 static void query_profile( const char *id )
576 mlt_properties profiles = mlt_profile_list();
577 mlt_properties profile = mlt_properties_get_data( profiles, id, NULL );
580 char *s = mlt_properties_serialise_yaml( profile );
581 fprintf( stdout, "%s", s );
586 fprintf( stdout, "# No metadata for profile \"%s\"\n", id );
588 mlt_properties_close( profiles );
591 static void query_presets()
593 mlt_properties presets = mlt_repository_presets();
594 fprintf( stdout, "---\npresets:\n" );
598 for ( j = 0; j < mlt_properties_count( presets ); j++ )
599 fprintf( stdout, " - %s\n", mlt_properties_get_name( presets, j ) );
601 fprintf( stdout, "...\n" );
602 mlt_properties_close( presets );
605 static void query_preset( const char *id )
607 mlt_properties presets = mlt_repository_presets();
608 mlt_properties preset = mlt_properties_get_data( presets, id, NULL );
611 char *s = mlt_properties_serialise_yaml( preset );
612 fprintf( stdout, "%s", s );
617 fprintf( stdout, "# No metadata for preset \"%s\"\n", id );
619 mlt_properties_close( presets );
622 static void query_formats( )
624 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
627 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "f", "list" );
628 mlt_consumer_start( consumer );
629 mlt_consumer_close( consumer );
633 fprintf( stdout, "# No formats - failed to load avformat consumer\n" );
637 static void query_acodecs( )
639 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
642 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "acodec", "list" );
643 mlt_consumer_start( consumer );
644 mlt_consumer_close( consumer );
648 fprintf( stdout, "# No audio codecs - failed to load avformat consumer\n" );
652 static void query_vcodecs( )
654 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
657 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "vcodec", "list" );
658 mlt_consumer_start( consumer );
659 mlt_consumer_close( consumer );
663 fprintf( stdout, "# No video codecs - failed to load avformat consumer\n" );
667 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
669 mlt_consumer_stop( consumer );
670 exit( EXIT_FAILURE );
673 int main( int argc, char **argv )
676 mlt_consumer consumer = NULL;
679 mlt_profile profile = NULL;
682 mlt_profile backup_profile;
684 // Construct the factory
685 mlt_repository repo = mlt_factory_init( NULL );
687 #if defined(WIN32) && !defined(MELT_NOSDL)
691 for ( i = 1; i < argc; i ++ )
693 // Check for serialisation switch
694 if ( !strcmp( argv[ i ], "-serialise" ) )
697 if ( name != NULL && strstr( name, ".melt" ) )
698 store = fopen( name, "w" );
701 if ( name == NULL || name[0] == '-' )
706 // Look for the profile option
707 else if ( !strcmp( argv[ i ], "-profile" ) )
709 const char *pname = argv[ ++ i ];
710 if ( pname && pname[0] != '-' )
711 profile = mlt_profile_init( pname );
713 else if ( !strcmp( argv[ i ], "-progress" ) )
717 else if ( !strcmp( argv[ i ], "-progress2" ) )
721 // Look for the query option
722 else if ( !strcmp( argv[ i ], "-query" ) )
724 const char *pname = argv[ ++ i ];
725 if ( pname && pname[0] != '-' )
727 if ( !strcmp( pname, "consumers" ) || !strcmp( pname, "consumer" ) )
728 query_services( repo, consumer_type );
729 else if ( !strcmp( pname, "filters" ) || !strcmp( pname, "filter" ) )
730 query_services( repo, filter_type );
731 else if ( !strcmp( pname, "producers" ) || !strcmp( pname, "producer" ) )
732 query_services( repo, producer_type );
733 else if ( !strcmp( pname, "transitions" ) || !strcmp( pname, "transition" ) )
734 query_services( repo, transition_type );
735 else if ( !strcmp( pname, "profiles" ) || !strcmp( pname, "profile" ) )
737 else if ( !strcmp( pname, "presets" ) || !strcmp( pname, "preset" ) )
739 else if ( !strncmp( pname, "format", 6 ) )
741 else if ( !strncmp( pname, "acodec", 6 ) || !strcmp( pname, "audio_codecs" ) )
743 else if ( !strncmp( pname, "vcodec", 6 ) || !strcmp( pname, "video_codecs" ) )
746 else if ( !strncmp( pname, "consumer=", 9 ) )
747 query_metadata( repo, consumer_type, "consumer", strchr( pname, '=' ) + 1 );
748 else if ( !strncmp( pname, "filter=", 7 ) )
749 query_metadata( repo, filter_type, "filter", strchr( pname, '=' ) + 1 );
750 else if ( !strncmp( pname, "producer=", 9 ) )
751 query_metadata( repo, producer_type, "producer", strchr( pname, '=' ) + 1 );
752 else if ( !strncmp( pname, "transition=", 11 ) )
753 query_metadata( repo, transition_type, "transition", strchr( pname, '=' ) + 1 );
754 else if ( !strncmp( pname, "profile=", 8 ) )
755 query_profile( strchr( pname, '=' ) + 1 );
756 else if ( !strncmp( pname, "preset=", 7 ) )
757 query_preset( strchr( pname, '=' ) + 1 );
764 query_services( repo, consumer_type );
765 query_services( repo, filter_type );
766 query_services( repo, producer_type );
767 query_services( repo, transition_type );
768 fprintf( stdout, "# You can query the metadata for a specific service using:\n"
769 "# -query <type>=<identifer>\n"
770 "# where <type> is one of: consumer, filter, producer, or transition.\n" );
774 else if ( !strcmp( argv[ i ], "-silent" ) )
778 else if ( !strcmp( argv[ i ], "-verbose" ) )
780 mlt_log_set_level( MLT_LOG_VERBOSE );
782 else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
784 fprintf( stdout, "%s " VERSION "\n"
785 "Copyright (C) 2002-2012 Ushodaya Enterprises Limited\n"
786 "<http://www.mltframework.org/>\n"
787 "This is free software; see the source for copying conditions. There is NO\n"
788 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
789 basename( argv[0] ) );
792 else if ( !strcmp( argv[ i ], "-debug" ) )
794 mlt_log_set_level( MLT_LOG_DEBUG );
797 if ( !is_silent && !isatty( STDIN_FILENO ) && !is_progress )
800 // Create profile if not set explicitly
801 if ( getenv( "MLT_PROFILE" ) )
802 profile = mlt_profile_init( NULL );
803 if ( profile == NULL )
804 profile = mlt_profile_init( NULL );
806 profile->is_explicit = 1;
808 // Look for the consumer option to load profile settings from consumer properties
809 backup_profile = mlt_profile_clone( profile );
810 load_consumer( &consumer, profile, argc, argv );
812 // If the consumer changed the profile, then it is explicit.
813 if ( backup_profile && !profile->is_explicit && (
814 profile->width != backup_profile->width ||
815 profile->height != backup_profile->height ||
816 profile->sample_aspect_num != backup_profile->sample_aspect_num ||
817 profile->sample_aspect_den != backup_profile->sample_aspect_den ||
818 profile->frame_rate_den != backup_profile->frame_rate_den ||
819 profile->frame_rate_num != backup_profile->frame_rate_num ||
820 profile->colorspace != backup_profile->colorspace ) )
821 profile->is_explicit = 1;
822 mlt_profile_close( backup_profile );
826 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
830 // Generate an automatic profile if needed.
831 if ( ! profile->is_explicit )
833 mlt_profile_from_producer( profile, melt );
834 mlt_producer_close( melt );
835 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
838 // Reload the consumer with the fully qualified profile.
839 // The producer or auto-profile could have changed the profile.
840 load_consumer( &consumer, profile, argc, argv );
842 // See if producer has consumer already attached
843 if ( !store && !consumer )
845 consumer = MLT_CONSUMER( mlt_service_consumer( MLT_PRODUCER_SERVICE( melt ) ) );
848 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES(consumer) ); // because we explicitly close it
849 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(consumer),
850 "transport_callback", transport_action, 0, NULL, NULL );
854 // If we have no consumer, default to sdl
855 if ( store == NULL && consumer == NULL )
856 consumer = create_consumer( profile, NULL );
859 // Set transport properties on consumer and produder
860 if ( consumer != NULL && melt != NULL )
862 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES( consumer ), "transport_producer", melt, 0, NULL, NULL );
863 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( melt ), "transport_consumer", consumer, 0, NULL, NULL );
865 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress", is_progress );
867 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent", is_silent );
870 if ( argc > 1 && melt != NULL && mlt_producer_get_length( melt ) > 0 )
872 // Parse the arguments
873 for ( i = 1; i < argc; i ++ )
875 if ( !strcmp( argv[ i ], "-jack" ) )
877 setup_jack_transport( consumer, profile );
879 else if ( !strcmp( argv[ i ], "-serialise" ) )
881 if ( store != stdout )
887 fprintf( store, "%s\n", argv[ i ] );
891 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
894 fprintf( store, "%s\n", argv[ i ] );
902 if ( consumer != NULL && store == NULL )
904 // Get melt's properties
905 mlt_properties melt_props = MLT_PRODUCER_PROPERTIES( melt );
907 // Get the last group
908 mlt_properties group = mlt_properties_get_data( melt_props, "group", 0 );
910 // Apply group settings
911 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
912 mlt_properties_inherit( properties, group );
914 // Connect consumer to melt
915 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( melt ) );
917 // Start the consumer
918 mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
919 if ( mlt_consumer_start( consumer ) == 0 )
921 // Try to exit gracefully upon these signals
922 signal( SIGHUP, stop_handler );
923 signal( SIGINT, stop_handler );
924 signal( SIGPIPE, stop_handler );
925 signal( SIGTERM, stop_handler );
927 // Transport functionality
928 transport( melt, consumer );
931 mlt_consumer_stop( consumer );
934 else if ( store != NULL && store != stdout && name != NULL )
936 fprintf( stderr, "Project saved as %s.\n", name );
942 show_usage( argv[0] );
945 // Disconnect producer from consumer to prevent ref cycles from closing services
947 mlt_consumer_connect( consumer, NULL );
949 // Close the producer
951 mlt_producer_close( melt );
953 // Close the consumer
954 if ( consumer != NULL )
955 mlt_consumer_close( consumer );
958 mlt_profile_close( profile );
962 mlt_factory_close( );