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>
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 abnormal_exit_handler(int signum)
55 // The process is going down hard. Restore the terminal first.
57 // Reset the default handler so the core gets dumped.
58 signal( signum, SIG_DFL );
62 static void transport_action( mlt_producer producer, char *value )
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;
70 mlt_properties_set_int( properties, "stats_off", 1 );
72 if ( strlen( value ) == 1 )
78 mlt_properties_set_int( properties, "done", 1 );
79 mlt_events_fire( jack, "jack-stop", NULL );
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 );
89 mlt_producer_set_speed( producer, -10 );
92 mlt_producer_set_speed( producer, -5 );
95 mlt_producer_set_speed( producer, -2 );
98 mlt_producer_set_speed( producer, -1 );
101 mlt_producer_set_speed( producer, 0 );
102 mlt_consumer_purge( consumer );
103 mlt_producer_seek( producer, mlt_consumer_position( consumer ) + 1 );
104 mlt_events_fire( jack, "jack-stop", NULL );
108 if ( !jack || mlt_producer_get_speed( producer ) != 0 )
109 mlt_producer_set_speed( producer, 1 );
110 mlt_consumer_purge( consumer );
111 mlt_events_fire( jack, "jack-start", NULL );
114 mlt_producer_set_speed( producer, 2 );
117 mlt_producer_set_speed( producer, 5 );
120 mlt_producer_set_speed( producer, 10 );
123 if ( multitrack != NULL )
126 mlt_position last = -1;
127 fprintf( stderr, "\n" );
128 for ( i = 0; 1; i ++ )
130 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
131 if ( position == last )
134 fprintf( stderr, "%d: %d\n", i, (int)position );
140 if ( multitrack != NULL )
142 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
143 mlt_producer_seek( producer, position );
144 mlt_consumer_purge( consumer );
145 mlt_events_fire( jack, "jack-seek", &position, NULL );
149 if ( producer != NULL )
151 position -= mlt_producer_get_fps( producer ) * 60;
152 mlt_consumer_purge( consumer );
153 mlt_producer_seek( producer, position );
154 mlt_events_fire( jack, "jack-seek", &position, NULL );
158 if ( producer != NULL )
161 mlt_producer_set_speed( producer, 0 );
162 mlt_consumer_purge( consumer );
163 mlt_producer_seek( producer, position );
164 mlt_events_fire( jack, "jack-stop", NULL );
165 mlt_events_fire( jack, "jack-seek", &position, NULL );
169 if ( multitrack != NULL )
171 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
172 mlt_consumer_purge( consumer );
173 mlt_producer_seek( producer, position );
174 mlt_events_fire( jack, "jack-seek", &position, NULL );
178 if ( multitrack != NULL )
180 position = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
181 mlt_consumer_purge( consumer );
182 mlt_producer_seek( producer, position );
183 mlt_events_fire( jack, "jack-seek", &position, NULL );
187 if ( producer != NULL )
190 mlt_consumer_purge( consumer );
191 if ( mlt_producer_get_speed( producer ) != 0 )
193 mlt_producer_set_speed( producer, 0 );
194 mlt_events_fire( jack, "jack-stop", NULL );
198 mlt_producer_seek( producer, position );
199 mlt_events_fire( jack, "jack-seek", &position, NULL );
204 if ( producer != NULL )
206 position += mlt_producer_get_fps( producer ) * 60;
207 mlt_consumer_purge( consumer );
208 mlt_producer_seek( producer, position );
209 mlt_events_fire( jack, "jack-seek", &position, NULL );
214 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
217 mlt_properties_set_int( properties, "stats_off", 0 );
220 static void on_jack_started( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
222 mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
225 if ( mlt_producer_get_speed( producer ) != 0 )
227 mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
228 mlt_events_fire( jack, "jack-stop", NULL );
232 mlt_producer_set_speed( producer, 1 );
233 mlt_consumer_purge( consumer );
234 mlt_producer_seek( producer, *position );
235 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
240 static void on_jack_stopped( mlt_properties owner, mlt_consumer consumer, mlt_position *position )
242 mlt_producer producer = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "transport_producer", NULL );
245 mlt_producer_set_speed( producer, 0 );
246 mlt_consumer_purge( consumer );
247 mlt_producer_seek( producer, *position );
248 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
252 static void setup_jack_transport( mlt_consumer consumer, mlt_profile profile )
254 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
255 mlt_filter jack = mlt_factory_filter( profile, "jackrack", NULL );
256 mlt_properties jack_properties = MLT_FILTER_PROPERTIES(jack);
258 mlt_service_attach( MLT_CONSUMER_SERVICE(consumer), jack );
259 mlt_properties_set_int( properties, "audio_off", 1 );
260 mlt_properties_set_data( properties, "jack_filter", jack, 0, (mlt_destructor) mlt_filter_close, NULL );
261 // mlt_properties_set( jack_properties, "out_1", "system:playback_1" );
262 // mlt_properties_set( jack_properties, "out_2", "system:playback_2" );
263 mlt_events_listen( jack_properties, consumer, "jack-started", (mlt_listener) on_jack_started );
264 mlt_events_listen( jack_properties, consumer, "jack-stopped", (mlt_listener) on_jack_stopped );
267 static mlt_consumer create_consumer( mlt_profile profile, char *id )
269 char *myid = id ? strdup( id ) : NULL;
270 char *arg = myid ? strchr( myid, ':' ) : NULL;
273 mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
274 if ( consumer != NULL )
276 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
277 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
284 static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
290 for ( i = 1; i < argc; i ++ ) {
291 // See if we need multi consumer.
292 multi += !strcmp( argv[i], "-consumer" );
293 // Seee if we need the qglsl variant of multi consumer.
294 if ( !strncmp( argv[i], "glsl.", 5 ) || !strncmp( argv[i], "movit.", 6 ) )
297 // Disable qglsl if xgl is being used!
298 for ( i = 1; qglsl && i < argc; i ++ )
299 if ( !strcmp( argv[i], "xgl" ) )
302 if ( multi > 1 || qglsl )
304 // If there is more than one -consumer use the 'multi' consumer.
309 mlt_consumer_close( *consumer );
310 *consumer = create_consumer( profile, ( qglsl? "qglsl" : "multi" ) );
311 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
312 for ( i = 1; i < argc; i ++ )
314 if ( !strcmp( argv[ i ], "-consumer" ) && argv[ i + 1 ])
316 // Create a properties object for each sub-consumer
317 mlt_properties new_props = mlt_properties_new();
318 snprintf( key, sizeof(key), "%d", k++ );
319 mlt_properties_set_data( properties, key, new_props, 0,
320 (mlt_destructor) mlt_properties_close, NULL );
321 if ( strchr( argv[i + 1], ':' ) )
323 char *temp = strdup( argv[++i] );
324 char *service = temp;
325 char *target = strchr( temp, ':' );
327 mlt_properties_set( new_props, "mlt_service", service );
328 mlt_properties_set( new_props, "target", target );
332 mlt_properties_set( new_props, "mlt_service", argv[ ++i ] );
334 while ( argv[ i + 1 ] && strchr( argv[ i + 1 ], '=' ) )
335 mlt_properties_parse( new_props, argv[ ++ i ] );
339 else for ( i = 1; i < argc; i ++ )
341 if ( !strcmp( argv[ i ], "-consumer" ) )
344 mlt_consumer_close( *consumer );
345 *consumer = create_consumer( profile, argv[ ++ i ] );
348 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
349 while ( argv[ i + 1 ] != NULL && strchr( argv[ i + 1 ], '=' ) )
350 mlt_properties_parse( properties, argv[ ++ i ] );
356 #if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
358 static void event_handling( mlt_producer producer, mlt_consumer consumer )
362 while ( SDL_PollEvent( &event ) )
367 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
371 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
373 char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
374 transport_action( producer, keyboard );
383 static void transport( mlt_producer producer, mlt_consumer consumer )
385 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
386 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
387 int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
388 struct timespec tm = { 0, 40000000 };
389 int total_length = mlt_producer_get_length( producer );
390 int last_position = 0;
392 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
394 if ( !silent && !progress )
398 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
399 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
400 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
402 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
403 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
404 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
405 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
406 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
407 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
410 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
412 int value = ( silent || progress )? -1 : term_read( );
416 char string[ 2 ] = { value, 0 };
417 transport_action( producer, string );
420 #if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
421 event_handling( producer, consumer );
424 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
428 int current_position = mlt_producer_position( producer );
429 if ( current_position > last_position )
431 fprintf( stderr, "Current Frame: %10d, percentage: %10d%c",
432 current_position, 100 * current_position / total_length,
433 progress == 2 ? '\n' : '\r' );
434 last_position = current_position;
439 fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
444 if ( silent || progress )
445 nanosleep( &tm, NULL );
449 fprintf( stderr, "\n" );
453 static void show_usage( char *program_name )
456 "Usage: %s [options] [producer [name=value]* ]+\n"
458 " -attach filter[:arg] [name=value]* Attach a filter to the output\n"
459 " -attach-cut filter[:arg] [name=value]* Attach a filter to a cut\n"
460 " -attach-track filter[:arg] [name=value]* Attach a filter to a track\n"
461 " -attach-clip filter[:arg] [name=value]* Attach a filter to a producer\n"
462 " -audio-track | -hide-video Add an audio-only track\n"
463 " -blank frames Add blank silence to a track\n"
464 " -consumer id[:arg] [name=value]* Set the consumer (sink)\n"
465 " -debug Set the logging level to debug\n"
466 " -filter filter[:arg] [name=value]* Add a filter to the current track\n"
467 " -group [name=value]* Apply properties repeatedly\n"
468 " -help Show this message\n"
469 " -jack Enable JACK transport synchronization\n"
470 " -join clips Join multiple clips into one cut\n"
471 " -mix length Add a mix between the last two cuts\n"
472 " -mixer transition Add a transition to the mix\n"
473 " -null-track | -hide-track Add a hidden track\n"
474 " -profile name Set the processing settings\n"
475 " -progress Display progress along with position\n"
476 " -remove Remove the most recent cut\n"
477 " -repeat times Repeat the last cut\n"
478 " -query List all of the registered services\n"
479 " -query \"consumers\" | \"consumer\"=id List consumers or show info about one\n"
480 " -query \"filters\" | \"filter\"=id List filters or show info about one\n"
481 " -query \"producers\" | \"producer\"=id List producers or show info about one\n"
482 " -query \"transitions\" | \"transition\"=id List transitions, show info about one\n"
483 " -query \"profiles\" | \"profile\"=id List profiles, show info about one\n"
484 " -query \"presets\" | \"preset\"=id List presets, show info about one\n"
485 " -query \"formats\" List audio/video formats\n"
486 " -query \"audio_codecs\" List audio codecs\n"
487 " -query \"video_codecs\" List video codecs\n"
488 " -serialise [filename] Write the commands to a text file\n"
489 " -silent Do not display position/transport\n"
490 " -split relative-frame Split the last cut into two cuts\n"
491 " -swap Rearrange the last two cuts\n"
492 " -track Add a track\n"
493 " -transition id[:arg] [name=value]* Add a transition\n"
494 " -verbose Set the logging level to verbose\n"
495 " -version Show the version and copyright\n"
496 " -video-track | -hide-audio Add a video-only track\n"
497 "For more help: <http://www.mltframework.org/>\n",
498 basename( program_name ) );
501 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
503 mlt_properties metadata = mlt_repository_metadata( repo, type, id );
506 char *s = mlt_properties_serialise_yaml( metadata );
507 fprintf( stdout, "%s", s );
512 fprintf( stdout, "# No metadata for %s \"%s\"\n", typestr, id );
516 static int is_service_hidden(mlt_repository repo, mlt_service_type type, const char *service_name )
518 mlt_properties metadata = NULL;
519 mlt_properties tags = NULL;
520 metadata = mlt_repository_metadata(repo, type, service_name);
524 tags = mlt_properties_get_data( metadata, "tags", NULL );
528 for ( k = 0; k < mlt_properties_count( tags ); k++ )
530 const char* value = mlt_properties_get_value(tags, k);
531 if( !strcmp("Hidden", value) )
541 static void query_services( mlt_repository repo, mlt_service_type type )
543 mlt_properties services = NULL;
544 const char *typestr = NULL;
548 services = mlt_repository_consumers( repo );
549 typestr = "consumers";
552 services = mlt_repository_filters( repo );
556 services = mlt_repository_producers( repo );
557 typestr = "producers";
559 case transition_type:
560 services = mlt_repository_transitions( repo );
561 typestr = "transitions";
566 fprintf( stdout, "---\n%s:\n", typestr );
570 for ( j = 0; j < mlt_properties_count( services ); j++ )
572 const char* service_name = mlt_properties_get_name( services, j );
573 if( !is_service_hidden(repo, type, service_name ) )
574 fprintf( stdout, " - %s\n", service_name );
577 fprintf( stdout, "...\n" );
580 static void query_profiles()
582 mlt_properties profiles = mlt_profile_list();
583 fprintf( stdout, "---\nprofiles:\n" );
587 for ( j = 0; j < mlt_properties_count( profiles ); j++ )
588 fprintf( stdout, " - %s\n", mlt_properties_get_name( profiles, j ) );
590 fprintf( stdout, "...\n" );
591 mlt_properties_close( profiles );
594 static void query_profile( const char *id )
596 mlt_properties profiles = mlt_profile_list();
597 mlt_properties profile = mlt_properties_get_data( profiles, id, NULL );
600 char *s = mlt_properties_serialise_yaml( profile );
601 fprintf( stdout, "%s", s );
606 fprintf( stdout, "# No metadata for profile \"%s\"\n", id );
608 mlt_properties_close( profiles );
611 static void query_presets()
613 mlt_properties presets = mlt_repository_presets();
614 fprintf( stdout, "---\npresets:\n" );
618 for ( j = 0; j < mlt_properties_count( presets ); j++ )
619 fprintf( stdout, " - %s\n", mlt_properties_get_name( presets, j ) );
621 fprintf( stdout, "...\n" );
622 mlt_properties_close( presets );
625 static void query_preset( const char *id )
627 mlt_properties presets = mlt_repository_presets();
628 mlt_properties preset = mlt_properties_get_data( presets, id, NULL );
631 char *s = mlt_properties_serialise_yaml( preset );
632 fprintf( stdout, "%s", s );
637 fprintf( stdout, "# No metadata for preset \"%s\"\n", id );
639 mlt_properties_close( presets );
642 static void query_formats( )
644 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
647 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "f", "list" );
648 mlt_consumer_start( consumer );
649 mlt_consumer_close( consumer );
653 fprintf( stdout, "# No formats - failed to load avformat consumer\n" );
657 static void query_acodecs( )
659 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
662 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "acodec", "list" );
663 mlt_consumer_start( consumer );
664 mlt_consumer_close( consumer );
668 fprintf( stdout, "# No audio codecs - failed to load avformat consumer\n" );
672 static void query_vcodecs( )
674 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
677 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "vcodec", "list" );
678 mlt_consumer_start( consumer );
679 mlt_consumer_close( consumer );
683 fprintf( stdout, "# No video codecs - failed to load avformat consumer\n" );
687 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
689 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(consumer), "done", 1 );
692 int main( int argc, char **argv )
695 mlt_consumer consumer = NULL;
698 mlt_profile profile = NULL;
701 mlt_profile backup_profile;
703 // Handle abnormal exit situations.
704 signal( SIGSEGV, abnormal_exit_handler );
705 signal( SIGILL, abnormal_exit_handler );
706 signal( SIGABRT, abnormal_exit_handler );
708 // Construct the factory
709 mlt_repository repo = mlt_factory_init( NULL );
711 #if defined(WIN32) && !defined(MELT_NOSDL)
715 for ( i = 1; i < argc; i ++ )
717 // Check for serialisation switch
718 if ( !strcmp( argv[ i ], "-serialise" ) )
721 if ( name != NULL && strstr( name, ".melt" ) )
722 store = fopen( name, "w" );
725 if ( name == NULL || name[0] == '-' )
730 // Look for the profile option
731 else if ( !strcmp( argv[ i ], "-profile" ) )
733 const char *pname = argv[ ++ i ];
734 if ( pname && pname[0] != '-' )
735 profile = mlt_profile_init( pname );
737 else if ( !strcmp( argv[ i ], "-progress" ) )
741 else if ( !strcmp( argv[ i ], "-progress2" ) )
745 // Look for the query option
746 else if ( !strcmp( argv[ i ], "-query" ) )
748 const char *pname = argv[ ++ i ];
749 if ( pname && pname[0] != '-' )
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" ) )
761 else if ( !strcmp( pname, "presets" ) || !strcmp( pname, "preset" ) )
763 else if ( !strncmp( pname, "format", 6 ) )
765 else if ( !strncmp( pname, "acodec", 6 ) || !strcmp( pname, "audio_codecs" ) )
767 else if ( !strncmp( pname, "vcodec", 6 ) || !strcmp( pname, "video_codecs" ) )
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 );
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" );
798 else if ( !strcmp( argv[ i ], "-silent" ) )
802 else if ( !strcmp( argv[ i ], "-verbose" ) )
804 mlt_log_set_level( MLT_LOG_VERBOSE );
806 else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
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] ) );
816 else if ( !strcmp( argv[ i ], "-debug" ) )
818 mlt_log_set_level( MLT_LOG_DEBUG );
821 if ( !is_silent && !isatty( STDIN_FILENO ) && !is_progress )
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 );
830 profile->is_explicit = 1;
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 );
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 );
850 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
854 // Generate an automatic profile if needed.
855 if ( ! profile->is_explicit )
857 mlt_profile_from_producer( profile, melt );
858 mlt_producer_close( melt );
859 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
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 );
866 // See if producer has consumer already attached
867 if ( !store && !consumer )
869 consumer = MLT_CONSUMER( mlt_service_consumer( MLT_PRODUCER_SERVICE( melt ) ) );
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 );
878 // If we have no consumer, default to sdl
879 if ( store == NULL && consumer == NULL )
880 consumer = create_consumer( profile, NULL );
883 // Set transport properties on consumer and produder
884 if ( consumer != NULL && melt != NULL )
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 );
889 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress", is_progress );
891 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent", is_silent );
894 if ( argc > 1 && melt != NULL && mlt_producer_get_length( melt ) > 0 )
896 // Parse the arguments
897 for ( i = 1; i < argc; i ++ )
899 if ( !strcmp( argv[ i ], "-jack" ) )
901 setup_jack_transport( consumer, profile );
903 else if ( !strcmp( argv[ i ], "-serialise" ) )
905 if ( store != stdout )
911 fprintf( store, "%s\n", argv[ i ] );
915 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
918 fprintf( store, "%s\n", argv[ i ] );
926 if ( consumer != NULL && store == NULL )
928 // Get melt's properties
929 mlt_properties melt_props = MLT_PRODUCER_PROPERTIES( melt );
931 // Get the last group
932 mlt_properties group = mlt_properties_get_data( melt_props, "group", 0 );
934 // Apply group settings
935 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
936 mlt_properties_inherit( properties, group );
938 // Connect consumer to melt
939 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( melt ) );
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 )
945 // Try to exit gracefully upon these signals
946 signal( SIGINT, stop_handler );
947 signal( SIGTERM, stop_handler );
949 signal( SIGHUP, stop_handler );
950 signal( SIGPIPE, stop_handler );
953 // Transport functionality
954 transport( melt, consumer );
957 mlt_consumer_stop( consumer );
960 else if ( store != NULL && store != stdout && name != NULL )
962 fprintf( stderr, "Project saved as %s.\n", name );
968 show_usage( argv[0] );
971 // Disconnect producer from consumer to prevent ref cycles from closing services
974 mlt_consumer_connect( consumer, NULL );
975 mlt_events_fire( MLT_CONSUMER_PROPERTIES(consumer), "consumer-cleanup", NULL);
978 // Close the producer
980 mlt_producer_close( melt );
982 // Close the consumer
983 if ( consumer != NULL )
984 mlt_consumer_close( consumer );
987 mlt_profile_close( profile );
991 // Workaround qmelt on OS X from crashing at exit.
992 #if !defined(__MACH__) || !defined(QT_GUI_LIB)
993 mlt_factory_close( );