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>
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.
32 #include <framework/mlt.h>
34 #if defined(__DARWIN__) || defined(WIN32)
40 static void transport_action( mlt_producer producer, char *value )
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 );
46 mlt_properties_set_int( properties, "stats_off", 1 );
48 if ( strlen( value ) == 1 )
54 mlt_properties_set_int( properties, "done", 1 );
57 mlt_producer_set_speed( producer, 1 );
58 mlt_producer_seek( producer, 0 );
61 mlt_producer_set_speed( producer, -10 );
64 mlt_producer_set_speed( producer, -5 );
67 mlt_producer_set_speed( producer, -2 );
70 mlt_producer_set_speed( producer, -1 );
73 mlt_producer_set_speed( producer, 0 );
77 mlt_producer_set_speed( producer, 1 );
80 mlt_producer_set_speed( producer, 2 );
83 mlt_producer_set_speed( producer, 5 );
86 mlt_producer_set_speed( producer, 10 );
89 if ( multitrack != NULL )
92 mlt_position last = -1;
93 fprintf( stderr, "\n" );
94 for ( i = 0; 1; i ++ )
96 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
100 fprintf( stderr, "%d: %d\n", i, (int)time );
106 if ( multitrack != NULL )
108 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
109 mlt_producer_seek( producer, time );
113 if ( producer != NULL )
115 mlt_position position = mlt_producer_position( producer );
116 mlt_producer_seek( producer, position - ( mlt_producer_get_fps( producer ) * 60 ) );
120 if ( producer != NULL )
122 mlt_position position = mlt_producer_position( producer );
123 mlt_producer_set_speed( producer, 0 );
124 mlt_producer_seek( producer, position - 1 );
128 if ( multitrack != NULL )
130 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
131 mlt_producer_seek( producer, time );
135 if ( multitrack != NULL )
137 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
138 mlt_producer_seek( producer, time );
142 if ( producer != NULL )
144 mlt_position position = mlt_producer_position( producer );
145 if ( mlt_producer_get_speed( producer ) != 0 )
146 mlt_producer_set_speed( producer, 0 );
148 mlt_producer_seek( producer, position + 1 );
152 if ( producer != NULL )
154 mlt_position position = mlt_producer_position( producer );
155 mlt_producer_seek( producer, position + ( mlt_producer_get_fps( producer ) * 60 ) );
160 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
163 mlt_properties_set_int( properties, "stats_off", 0 );
166 static mlt_consumer create_consumer( mlt_profile profile, char *id )
168 char *myid = id ? strdup( id ) : NULL;
169 char *arg = myid ? strchr( myid, ':' ) : NULL;
172 mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
173 if ( consumer != NULL )
175 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
176 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
183 static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
186 for ( i = 1; i < argc; i ++ )
188 if ( !strcmp( argv[ i ], "-consumer" ) )
191 mlt_consumer_close( *consumer );
192 *consumer = create_consumer( profile, argv[ ++ i ] );
195 mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
196 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
197 mlt_properties_parse( properties, argv[ ++ i ] );
203 #if defined(__DARWIN__) || defined(WIN32)
205 static void event_handling( mlt_producer producer, mlt_consumer consumer )
209 while ( SDL_PollEvent( &event ) )
214 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
218 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
220 char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
221 transport_action( producer, keyboard );
230 static void transport( mlt_producer producer, mlt_consumer consumer )
232 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
233 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
234 int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
235 struct timespec tm = { 0, 40000 };
236 int total_length = mlt_producer_get_length( producer );
237 int last_position = 0;
239 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
241 if ( !silent && !progress )
245 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
246 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
247 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
249 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
250 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
251 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
252 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
253 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
254 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
257 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
259 int value = ( silent || progress )? -1 : term_read( );
263 char string[ 2 ] = { value, 0 };
264 transport_action( producer, string );
267 #if defined(__DARWIN__) || defined(WIN32)
268 event_handling( producer, consumer );
271 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
275 int current_position = mlt_producer_position( producer );
276 if ( current_position > last_position )
278 fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
279 current_position, 100 * current_position / total_length );
280 last_position = current_position;
285 fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
289 if ( silent || progress )
290 nanosleep( &tm, NULL );
294 fprintf( stderr, "\n" );
298 static void show_usage( char *program_name )
301 "Usage: %s [options] [producer [name=value]* ]+\n"
303 " -attach filter[:arg] [name=value]* Attach a filter to the output\n"
304 " -attach-cut filter[:arg] [name=value]* Attach a filter to a cut\n"
305 " -attach-track filter[:arg] [name=value]* Attach a filter to a track\n"
306 " -attach-clip filter[:arg] [name=value]* Attach a filter to a producer\n"
307 " -audio-track | -hide-video Add an audio-only track\n"
308 " -blank frames Add blank silence to a track\n"
309 " -consumer id[:arg] [name=value]* Set the consumer (sink)\n"
310 " -debug Set the logging level to debug\n"
311 " -filter filter[:arg] [name=value]* Add a filter to the current track\n"
312 " -group [name=value]* Apply properties repeatedly\n"
313 " -help Show this message\n"
314 " -join clips Join multiple clips into one cut\n"
315 " -mix length Add a mix between the last two cuts\n"
316 " -mixer transition Add a transition to the mix\n"
317 " -null-track | -hide-track Add a hidden track\n"
318 " -profile name Set the processing settings\n"
319 " -progress Display progress along with position\n"
320 " -remove Remove the most recent cut\n"
321 " -repeat times Repeat the last cut\n"
322 " -query List all of the registered services\n"
323 " -query \"consumers\" | \"consumer\"=id List consumers or show info about one\n"
324 " -query \"filters\" | \"filter\"=id List filters or show info about one\n"
325 " -query \"producers\" | \"producer\"=id List producers or show info about one\n"
326 " -query \"transitions\" | \"transition\"=id List transitions, show info about one\n"
327 " -query \"profiles\" | \"profile\"=id List profiles, show info about one\n"
328 " -query \"formats\" List audio/video formats\n"
329 " -query \"audio_codecs\" List audio codecs\n"
330 " -query \"video_codecs\" List video codecs\n"
331 " -serialise [filename] Write the commands to a text file\n"
332 " -silent Do not display position/transport\n"
333 " -split relative-frame Split the last cut into two cuts\n"
334 " -swap Rearrange the last two cuts\n"
335 " -track Add a track\n"
336 " -transition id[:arg] [name=value]* Add a transition\n"
337 " -verbose Set the logging level to verbose\n"
338 " -version Show the version and copyright\n"
339 " -video-track | -hide-audio Add a video-only track\n"
340 "For more help: <http://www.mltframework.org/>\n",
341 basename( program_name ) );
344 static void guess_profile( mlt_producer melt, mlt_profile profile )
348 mlt_image_format fmt = mlt_image_yuv422;
350 int w = profile->width;
351 int h = profile->height;
353 if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 ) && fr )
355 mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
356 if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
358 // Some source properties are not exposed until after the first get_image call.
359 mlt_frame_close( fr );
360 mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 );
361 p = MLT_FRAME_PROPERTIES( fr );
362 // mlt_properties_dump(p, stderr);
363 if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
365 profile->width = mlt_properties_get_int( p, "meta.media.width" );
366 profile->height = mlt_properties_get_int( p, "meta.media.height" );
367 profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
368 profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
369 profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
370 // AVCHD is mis-reported as double frame rate.
371 if ( profile->progressive == 0 && (
372 profile->frame_rate_num / profile->frame_rate_den == 50 ||
373 profile->frame_rate_num / profile->frame_rate_den == 59 ) )
374 profile->frame_rate_num /= 2;
375 profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
376 profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
377 profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
378 profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
379 profile->display_aspect_den = profile->height;
380 free( profile->description );
381 profile->description = strdup( "automatic" );
382 profile->is_explicit = 0;
386 mlt_frame_close( fr );
387 mlt_producer_seek( melt, 0 );
390 static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
392 mlt_properties metadata = mlt_repository_metadata( repo, type, id );
395 char *s = mlt_properties_serialise_yaml( metadata );
396 fprintf( stderr, "%s", s );
401 fprintf( stderr, "# No metadata for %s \"%s\"\n", typestr, id );
405 static void query_services( mlt_repository repo, mlt_service_type type )
407 mlt_properties services = NULL;
408 const char *typestr = NULL;
412 services = mlt_repository_consumers( repo );
413 typestr = "consumers";
416 services = mlt_repository_filters( repo );
420 services = mlt_repository_producers( repo );
421 typestr = "producers";
423 case transition_type:
424 services = mlt_repository_transitions( repo );
425 typestr = "transitions";
430 fprintf( stderr, "---\n%s:\n", typestr );
434 for ( j = 0; j < mlt_properties_count( services ); j++ )
435 fprintf( stderr, " - %s\n", mlt_properties_get_name( services, j ) );
437 fprintf( stderr, "...\n" );
440 static void query_profiles()
442 mlt_properties profiles = mlt_profile_list();
443 fprintf( stderr, "---\nprofiles:\n" );
447 for ( j = 0; j < mlt_properties_count( profiles ); j++ )
448 fprintf( stderr, " - %s\n", mlt_properties_get_name( profiles, j ) );
450 fprintf( stderr, "...\n" );
451 mlt_properties_close( profiles );
454 static void query_profile( const char *id )
456 mlt_properties profiles = mlt_profile_list();
457 mlt_properties profile = mlt_properties_get_data( profiles, id, NULL );
460 char *s = mlt_properties_serialise_yaml( profile );
461 fprintf( stderr, "%s", s );
466 fprintf( stderr, "# No metadata for profile \"%s\"\n", id );
468 mlt_properties_close( profiles );
471 static void query_formats( )
473 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
476 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "f", "list" );
477 mlt_consumer_start( consumer );
478 mlt_consumer_close( consumer );
482 fprintf( stderr, "# No formats - failed to load avformat consumer\n" );
486 static void query_acodecs( )
488 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
491 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "acodec", "list" );
492 mlt_consumer_start( consumer );
493 mlt_consumer_close( consumer );
497 fprintf( stderr, "# No audio codecs - failed to load avformat consumer\n" );
501 static void query_vcodecs( )
503 mlt_consumer consumer = mlt_factory_consumer( NULL, "avformat", NULL );
506 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "vcodec", "list" );
507 mlt_consumer_start( consumer );
508 mlt_consumer_close( consumer );
512 fprintf( stderr, "# No video codecs - failed to load avformat consumer\n" );
516 static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
518 mlt_consumer_stop( consumer );
519 exit( EXIT_FAILURE );
522 int main( int argc, char **argv )
525 mlt_consumer consumer = NULL;
526 mlt_producer melt = NULL;
529 mlt_profile profile = NULL;
532 mlt_profile backup_profile;
534 // Construct the factory
535 mlt_repository repo = mlt_factory_init( NULL );
541 for ( i = 1; i < argc; i ++ )
543 // Check for serialisation switch
544 if ( !strcmp( argv[ i ], "-serialise" ) )
547 if ( name != NULL && strstr( name, ".melt" ) )
548 store = fopen( name, "w" );
551 if ( name == NULL || name[0] == '-' )
556 // Look for the profile option
557 else if ( !strcmp( argv[ i ], "-profile" ) )
559 const char *pname = argv[ ++ i ];
560 if ( pname && pname[0] != '-' )
561 profile = mlt_profile_init( pname );
563 else if ( !strcmp( argv[ i ], "-progress" ) )
567 // Look for the query option
568 else if ( !strcmp( argv[ i ], "-query" ) )
570 const char *pname = argv[ ++ i ];
571 if ( pname && pname[0] != '-' )
573 if ( !strcmp( pname, "consumers" ) || !strcmp( pname, "consumer" ) )
574 query_services( repo, consumer_type );
575 else if ( !strcmp( pname, "filters" ) || !strcmp( pname, "filter" ) )
576 query_services( repo, filter_type );
577 else if ( !strcmp( pname, "producers" ) || !strcmp( pname, "producer" ) )
578 query_services( repo, producer_type );
579 else if ( !strcmp( pname, "transitions" ) || !strcmp( pname, "transition" ) )
580 query_services( repo, transition_type );
581 else if ( !strcmp( pname, "profiles" ) || !strcmp( pname, "profile" ) )
583 else if ( !strncmp( pname, "format", 6 ) )
585 else if ( !strncmp( pname, "acodec", 6 ) || !strcmp( pname, "audio_codecs" ) )
587 else if ( !strncmp( pname, "vcodec", 6 ) || !strcmp( pname, "video_codecs" ) )
590 else if ( !strncmp( pname, "consumer=", 9 ) )
591 query_metadata( repo, consumer_type, "consumer", strchr( pname, '=' ) + 1 );
592 else if ( !strncmp( pname, "filter=", 7 ) )
593 query_metadata( repo, filter_type, "filter", strchr( pname, '=' ) + 1 );
594 else if ( !strncmp( pname, "producer=", 9 ) )
595 query_metadata( repo, producer_type, "producer", strchr( pname, '=' ) + 1 );
596 else if ( !strncmp( pname, "transition=", 11 ) )
597 query_metadata( repo, transition_type, "transition", strchr( pname, '=' ) + 1 );
598 else if ( !strncmp( pname, "profile=", 8 ) )
599 query_profile( strchr( pname, '=' ) + 1 );
606 query_services( repo, consumer_type );
607 query_services( repo, filter_type );
608 query_services( repo, producer_type );
609 query_services( repo, transition_type );
610 fprintf( stderr, "# You can query the metadata for a specific service using:\n"
611 "# -query <type>=<identifer>\n"
612 "# where <type> is one of: consumer, filter, producer, or transition.\n" );
616 else if ( !strcmp( argv[ i ], "-silent" ) )
620 else if ( !strcmp( argv[ i ], "-verbose" ) )
622 mlt_log_set_level( MLT_LOG_VERBOSE );
624 else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
626 fprintf( stderr, "MLT %s " VERSION "\n"
627 "Copyright (C) 2002-2011 Ushodaya Enterprises Limited\n"
628 "<http://www.mltframework.org/>\n"
629 "This is free software; see the source for copying conditions. There is NO\n"
630 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
631 basename( argv[0] ) );
634 else if ( !strcmp( argv[ i ], "-debug" ) )
636 mlt_log_set_level( MLT_LOG_DEBUG );
640 // Create profile if not set explicitly
641 if ( getenv( "MLT_PROFILE" ) )
642 profile = mlt_profile_init( NULL );
643 if ( profile == NULL )
644 profile = mlt_profile_init( NULL );
646 profile->is_explicit = 1;
648 // Look for the consumer option to load profile settings from consumer properties
649 backup_profile = mlt_profile_clone( profile );
650 load_consumer( &consumer, profile, argc, argv );
652 // If the consumer changed the profile, then it is explicit.
653 if ( backup_profile && !profile->is_explicit && (
654 profile->width != backup_profile->width ||
655 profile->height != backup_profile->height ||
656 profile->sample_aspect_num != backup_profile->sample_aspect_num ||
657 profile->sample_aspect_den != backup_profile->sample_aspect_den ||
658 profile->frame_rate_den != backup_profile->frame_rate_den ||
659 profile->frame_rate_num != backup_profile->frame_rate_num ||
660 profile->colorspace != backup_profile->colorspace ) )
661 profile->is_explicit = 1;
662 mlt_profile_close( backup_profile );
666 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
670 // Generate an automatic profile if needed.
671 if ( ! profile->is_explicit )
673 guess_profile( melt, profile );
674 mlt_producer_close( melt );
675 melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
678 // Reload the consumer with the fully qualified profile.
679 // The producer or guess_profile could have changed the profile.
680 load_consumer( &consumer, profile, argc, argv );
682 // If we have no consumer, default to sdl
683 if ( store == NULL && consumer == NULL )
684 consumer = create_consumer( profile, NULL );
687 // Set transport properties on consumer and produder
688 if ( consumer != NULL && melt != NULL )
690 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES( consumer ), "transport_producer", melt, 0, NULL, NULL );
691 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( melt ), "transport_consumer", consumer, 0, NULL, NULL );
693 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress", is_progress );
695 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent", is_silent );
698 if ( argc > 1 && melt != NULL && mlt_producer_get_length( melt ) > 0 )
700 // Parse the arguments
701 for ( i = 1; i < argc; i ++ )
703 if ( !strcmp( argv[ i ], "-serialise" ) )
705 if ( store != stdout )
711 fprintf( store, "%s\n", argv[ i ] );
715 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
718 fprintf( store, "%s\n", argv[ i ] );
726 if ( consumer != NULL && store == NULL )
728 // Get melt's properties
729 mlt_properties melt_props = MLT_PRODUCER_PROPERTIES( melt );
731 // Get the last group
732 mlt_properties group = mlt_properties_get_data( melt_props, "group", 0 );
734 // Apply group settings
735 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
736 mlt_properties_inherit( properties, group );
738 // Connect consumer to melt
739 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( melt ) );
741 // Start the consumer
742 mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
743 if ( mlt_consumer_start( consumer ) == 0 )
745 // Transport functionality
746 transport( melt, consumer );
749 mlt_consumer_stop( consumer );
752 else if ( store != NULL && store != stdout && name != NULL )
754 fprintf( stderr, "Project saved as %s.\n", name );
760 show_usage( argv[0] );
763 // Close the producer
765 mlt_producer_close( melt );
767 // Close the consumer
768 if ( consumer != NULL )
769 mlt_consumer_close( consumer );
772 mlt_profile_close( profile );
776 mlt_factory_close( );