/*
* melt.c -- MLT command line utility
- * Copyright (C) 2002-2011 Ushodaya Enterprises Limited
+ * Copyright (C) 2002-2013 Ushodaya Enterprises Limited
* Authors: Charles Yates <charles.yates@pandora.be>
* Dan Dennedy <dan@dennedy.org>
*
#include <sched.h>
#include <libgen.h>
#include <limits.h>
+#include <unistd.h>
+#include <signal.h>
#include <framework/mlt.h>
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
#include <SDL.h>
#endif
#include "io.h"
+static mlt_producer melt = NULL;
+
+static void stop_handler(int signum)
+{
+ if ( melt )
+ {
+ mlt_properties properties = MLT_PRODUCER_PROPERTIES( melt );
+ mlt_properties_set_int( properties, "done", 1 );
+ }
+}
+
+static void abnormal_exit_handler(int signum)
+{
+ // The process is going down hard. Restore the terminal first.
+ term_exit();
+ // Reset the default handler so the core gets dumped.
+ signal( signum, SIG_DFL );
+ raise( signum );
+}
+
static void transport_action( mlt_producer producer, char *value )
{
mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
mlt_properties jack = mlt_properties_get_data( MLT_CONSUMER_PROPERTIES( consumer ), "jack_filter", NULL );
- mlt_position position = mlt_producer_position( producer );
+ mlt_position position = producer? mlt_producer_position( producer ) : 0;
mlt_properties_set_int( properties, "stats_off", 1 );
case '5':
mlt_producer_set_speed( producer, 0 );
mlt_consumer_purge( consumer );
+ mlt_producer_seek( producer, mlt_consumer_position( consumer ) + 1 );
mlt_events_fire( jack, "jack-stop", NULL );
break;
case '6':
static void load_consumer( mlt_consumer *consumer, mlt_profile profile, int argc, char **argv )
{
int i;
- for ( i = 1; i < argc; i ++ )
+ int multi = 0;
+ int qglsl = 0;
+
+ for ( i = 1; i < argc; i ++ ) {
+ // See if we need multi consumer.
+ multi += !strcmp( argv[i], "-consumer" );
+ // Seee if we need the qglsl variant of multi consumer.
+ if ( !strncmp( argv[i], "glsl.", 5 ) || !strncmp( argv[i], "movit.", 6 ) )
+ qglsl = 1;
+ }
+ // Disable qglsl if xgl is being used!
+ for ( i = 1; qglsl && i < argc; i ++ )
+ if ( !strcmp( argv[i], "xgl" ) )
+ qglsl = 0;
+
+ if ( multi > 1 || qglsl )
+ {
+ // If there is more than one -consumer use the 'multi' consumer.
+ int k = 0;
+ char key[20];
+
+ if ( *consumer )
+ mlt_consumer_close( *consumer );
+ *consumer = create_consumer( profile, ( qglsl? "qglsl" : "multi" ) );
+ mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
+ for ( i = 1; i < argc; i ++ )
+ {
+ if ( !strcmp( argv[ i ], "-consumer" ) && argv[ i + 1 ])
+ {
+ // Create a properties object for each sub-consumer
+ mlt_properties new_props = mlt_properties_new();
+ snprintf( key, sizeof(key), "%d", k++ );
+ mlt_properties_set_data( properties, key, new_props, 0,
+ (mlt_destructor) mlt_properties_close, NULL );
+ if ( strchr( argv[i + 1], ':' ) )
+ {
+ char *temp = strdup( argv[++i] );
+ char *service = temp;
+ char *target = strchr( temp, ':' );
+ *target++ = 0;
+ mlt_properties_set( new_props, "mlt_service", service );
+ mlt_properties_set( new_props, "target", target );
+ }
+ else
+ {
+ mlt_properties_set( new_props, "mlt_service", argv[ ++i ] );
+ }
+ while ( argv[ i + 1 ] && strchr( argv[ i + 1 ], '=' ) )
+ mlt_properties_parse( new_props, argv[ ++ i ] );
+ }
+ }
+ }
+ else for ( i = 1; i < argc; i ++ )
{
if ( !strcmp( argv[ i ], "-consumer" ) )
{
if ( *consumer )
{
mlt_properties properties = MLT_CONSUMER_PROPERTIES( *consumer );
- while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
+ while ( argv[ i + 1 ] != NULL && strchr( argv[ i + 1 ], '=' ) )
mlt_properties_parse( properties, argv[ ++ i ] );
}
}
}
}
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
static void event_handling( mlt_producer producer, mlt_consumer consumer )
{
mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
- struct timespec tm = { 0, 40000 };
+ struct timespec tm = { 0, 40000000 };
int total_length = mlt_producer_get_length( producer );
int last_position = 0;
transport_action( producer, string );
}
-#if defined(__DARWIN__) || defined(WIN32)
+#if (defined(__DARWIN__) || defined(WIN32)) && !defined(MELT_NOSDL)
event_handling( producer, consumer );
#endif
int current_position = mlt_producer_position( producer );
if ( current_position > last_position )
{
- fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
- current_position, 100 * current_position / total_length );
+ fprintf( stderr, "Current Frame: %10d, percentage: %10d%c",
+ current_position, 100 * current_position / total_length,
+ progress == 2 ? '\n' : '\r' );
last_position = current_position;
}
}
{
fprintf( stderr, "Current Position: %10d\r", (int)mlt_consumer_position( consumer ) );
}
+ fflush( stderr );
}
if ( silent || progress )
static void show_usage( char *program_name )
{
- fprintf( stderr,
+ fprintf( stdout,
"Usage: %s [options] [producer [name=value]* ]+\n"
"Options:\n"
" -attach filter[:arg] [name=value]* Attach a filter to the output\n"
" -filter filter[:arg] [name=value]* Add a filter to the current track\n"
" -group [name=value]* Apply properties repeatedly\n"
" -help Show this message\n"
+" -jack Enable JACK transport synchronization\n"
" -join clips Join multiple clips into one cut\n"
" -mix length Add a mix between the last two cuts\n"
" -mixer transition Add a transition to the mix\n"
basename( program_name ) );
}
-static void guess_profile( mlt_producer melt, mlt_profile profile )
-{
- mlt_frame fr = NULL;
- uint8_t *buffer;
- mlt_image_format fmt = mlt_image_yuv422;
- mlt_properties p;
- int w = profile->width;
- int h = profile->height;
-
- if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 ) && fr )
- {
- mlt_properties_set_double( MLT_FRAME_PROPERTIES( fr ), "consumer_aspect_ratio", mlt_profile_sar( profile ) );
- if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
- {
- // Some source properties are not exposed until after the first get_image call.
- mlt_frame_close( fr );
- mlt_service_get_frame( MLT_PRODUCER_SERVICE(melt), &fr, 0 );
- p = MLT_FRAME_PROPERTIES( fr );
-// mlt_properties_dump(p, stderr);
- if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) && mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
- {
- profile->width = mlt_properties_get_int( p, "meta.media.width" );
- profile->height = mlt_properties_get_int( p, "meta.media.height" );
- profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
- profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
- profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
- // AVCHD is mis-reported as double frame rate.
- if ( profile->progressive == 0 && (
- profile->frame_rate_num / profile->frame_rate_den == 50 ||
- profile->frame_rate_num / profile->frame_rate_den == 59 ) )
- profile->frame_rate_num /= 2;
- profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
- profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
- profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
- profile->display_aspect_num = (int) ( (double) profile->sample_aspect_num * profile->width / profile->sample_aspect_den + 0.5 );
- profile->display_aspect_den = profile->height;
- free( profile->description );
- profile->description = strdup( "automatic" );
- profile->is_explicit = 0;
- }
- }
- }
- mlt_frame_close( fr );
- mlt_producer_seek( melt, 0 );
-}
-
static void query_metadata( mlt_repository repo, mlt_service_type type, const char *typestr, char *id )
{
mlt_properties metadata = mlt_repository_metadata( repo, type, id );
if ( metadata )
{
char *s = mlt_properties_serialise_yaml( metadata );
- fprintf( stderr, "%s", s );
+ fprintf( stdout, "%s", s );
free( s );
}
else
{
- fprintf( stderr, "# No metadata for %s \"%s\"\n", typestr, id );
+ fprintf( stdout, "# No metadata for %s \"%s\"\n", typestr, id );
}
}
+static int is_service_hidden(mlt_repository repo, mlt_service_type type, const char *service_name )
+{
+ mlt_properties metadata = NULL;
+ mlt_properties tags = NULL;
+ metadata = mlt_repository_metadata(repo, type, service_name);
+
+ if( metadata )
+ {
+ tags = mlt_properties_get_data( metadata, "tags", NULL );
+ if( tags )
+ {
+ int k;
+ for ( k = 0; k < mlt_properties_count( tags ); k++ )
+ {
+ const char* value = mlt_properties_get_value(tags, k);
+ if( !strcmp("Hidden", value) )
+ {
+ return 1;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
static void query_services( mlt_repository repo, mlt_service_type type )
{
mlt_properties services = NULL;
default:
return;
}
- fprintf( stderr, "---\n%s:\n", typestr );
+ fprintf( stdout, "---\n%s:\n", typestr );
if ( services )
{
int j;
for ( j = 0; j < mlt_properties_count( services ); j++ )
- fprintf( stderr, " - %s\n", mlt_properties_get_name( services, j ) );
+ {
+ const char* service_name = mlt_properties_get_name( services, j );
+ if( !is_service_hidden(repo, type, service_name ) )
+ fprintf( stdout, " - %s\n", service_name );
+ }
}
- fprintf( stderr, "...\n" );
+ fprintf( stdout, "...\n" );
}
static void query_profiles()
{
mlt_properties profiles = mlt_profile_list();
- fprintf( stderr, "---\nprofiles:\n" );
+ fprintf( stdout, "---\nprofiles:\n" );
if ( profiles )
{
int j;
for ( j = 0; j < mlt_properties_count( profiles ); j++ )
- fprintf( stderr, " - %s\n", mlt_properties_get_name( profiles, j ) );
+ fprintf( stdout, " - %s\n", mlt_properties_get_name( profiles, j ) );
}
- fprintf( stderr, "...\n" );
+ fprintf( stdout, "...\n" );
mlt_properties_close( profiles );
}
if ( profile )
{
char *s = mlt_properties_serialise_yaml( profile );
- fprintf( stderr, "%s", s );
+ fprintf( stdout, "%s", s );
free( s );
}
else
{
- fprintf( stderr, "# No metadata for profile \"%s\"\n", id );
+ fprintf( stdout, "# No metadata for profile \"%s\"\n", id );
}
mlt_properties_close( profiles );
}
static void query_presets()
{
mlt_properties presets = mlt_repository_presets();
- fprintf( stderr, "---\npresets:\n" );
+ fprintf( stdout, "---\npresets:\n" );
if ( presets )
{
int j;
for ( j = 0; j < mlt_properties_count( presets ); j++ )
- fprintf( stderr, " - %s\n", mlt_properties_get_name( presets, j ) );
+ fprintf( stdout, " - %s\n", mlt_properties_get_name( presets, j ) );
}
- fprintf( stderr, "...\n" );
+ fprintf( stdout, "...\n" );
mlt_properties_close( presets );
}
if ( preset )
{
char *s = mlt_properties_serialise_yaml( preset );
- fprintf( stderr, "%s", s );
+ fprintf( stdout, "%s", s );
free( s );
}
else
{
- fprintf( stderr, "# No metadata for preset \"%s\"\n", id );
+ fprintf( stdout, "# No metadata for preset \"%s\"\n", id );
}
mlt_properties_close( presets );
}
}
else
{
- fprintf( stderr, "# No formats - failed to load avformat consumer\n" );
+ fprintf( stdout, "# No formats - failed to load avformat consumer\n" );
}
}
}
else
{
- fprintf( stderr, "# No audio codecs - failed to load avformat consumer\n" );
+ fprintf( stdout, "# No audio codecs - failed to load avformat consumer\n" );
}
}
}
else
{
- fprintf( stderr, "# No video codecs - failed to load avformat consumer\n" );
+ fprintf( stdout, "# No video codecs - failed to load avformat consumer\n" );
}
}
static void on_fatal_error( mlt_properties owner, mlt_consumer consumer )
{
- mlt_consumer_stop( consumer );
- exit( EXIT_FAILURE );
+ mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(consumer), "done", 1 );
}
int main( int argc, char **argv )
{
int i;
mlt_consumer consumer = NULL;
- mlt_producer melt = NULL;
FILE *store = NULL;
char *name = NULL;
mlt_profile profile = NULL;
int is_silent = 0;
mlt_profile backup_profile;
+ // Handle abnormal exit situations.
+ signal( SIGSEGV, abnormal_exit_handler );
+ signal( SIGILL, abnormal_exit_handler );
+ signal( SIGABRT, abnormal_exit_handler );
+
// Construct the factory
mlt_repository repo = mlt_factory_init( NULL );
-#ifdef WIN32
+#if defined(WIN32) && !defined(MELT_NOSDL)
is_silent = 1;
#endif
{
is_progress = 1;
}
+ else if ( !strcmp( argv[ i ], "-progress2" ) )
+ {
+ is_progress = 2;
+ }
// Look for the query option
else if ( !strcmp( argv[ i ], "-query" ) )
{
query_services( repo, filter_type );
query_services( repo, producer_type );
query_services( repo, transition_type );
- fprintf( stderr, "# You can query the metadata for a specific service using:\n"
+ fprintf( stdout, "# You can query the metadata for a specific service using:\n"
"# -query <type>=<identifer>\n"
"# where <type> is one of: consumer, filter, producer, or transition.\n" );
}
}
else if ( !strcmp( argv[ i ], "-version" ) || !strcmp( argv[ i ], "--version" ) )
{
- fprintf( stderr, "MLT %s " VERSION "\n"
- "Copyright (C) 2002-2011 Ushodaya Enterprises Limited\n"
+ fprintf( stdout, "%s " VERSION "\n"
+ "Copyright (C) 2002-2013 Ushodaya Enterprises Limited\n"
"<http://www.mltframework.org/>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
mlt_log_set_level( MLT_LOG_DEBUG );
}
}
+ if ( !is_silent && !isatty( STDIN_FILENO ) && !is_progress )
+ is_progress = 1;
// Create profile if not set explicitly
if ( getenv( "MLT_PROFILE" ) )
// Generate an automatic profile if needed.
if ( ! profile->is_explicit )
{
- guess_profile( melt, profile );
+ mlt_profile_from_producer( profile, melt );
mlt_producer_close( melt );
melt = mlt_factory_producer( profile, "melt", &argv[ 1 ] );
}
// Reload the consumer with the fully qualified profile.
- // The producer or guess_profile could have changed the profile.
+ // The producer or auto-profile could have changed the profile.
load_consumer( &consumer, profile, argc, argv );
+ // See if producer has consumer already attached
+ if ( !store && !consumer )
+ {
+ consumer = MLT_CONSUMER( mlt_service_consumer( MLT_PRODUCER_SERVICE( melt ) ) );
+ if ( consumer )
+ {
+ mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES(consumer) ); // because we explicitly close it
+ mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(consumer),
+ "transport_callback", transport_action, 0, NULL, NULL );
+ }
+ }
+
// If we have no consumer, default to sdl
if ( store == NULL && consumer == NULL )
consumer = create_consumer( profile, NULL );
mlt_events_listen( properties, consumer, "consumer-fatal-error", ( mlt_listener )on_fatal_error );
if ( mlt_consumer_start( consumer ) == 0 )
{
+ // Try to exit gracefully upon these signals
+ signal( SIGINT, stop_handler );
+ signal( SIGTERM, stop_handler );
+#ifndef WIN32
+ signal( SIGHUP, stop_handler );
+ signal( SIGPIPE, stop_handler );
+#endif
+
// Transport functionality
transport( melt, consumer );
show_usage( argv[0] );
}
+ // Disconnect producer from consumer to prevent ref cycles from closing services
+ if ( consumer )
+ {
+ mlt_consumer_connect( consumer, NULL );
+ mlt_events_fire( MLT_CONSUMER_PROPERTIES(consumer), "consumer-cleanup", NULL);
+ }
+
// Close the producer
if ( melt != NULL )
mlt_producer_close( melt );
mlt_profile_close( profile );
exit_factory:
-
+
+// Workaround qmelt on OS X from crashing at exit.
+#if !defined(__MACH__) || !defined(QT_GUI_LIB)
mlt_factory_close( );
+#endif
return 0;
}