2 * producer_melt.c -- load from melt command line syntax
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <framework/mlt.h>
27 mlt_producer producer_melt_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv );
29 mlt_producer producer_melt_file_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
31 FILE *input = fopen( file, "r" );
32 char **args = calloc( sizeof( char * ), 1000 );
38 while( fgets( temp, 2048, input ) )
40 temp[ strlen( temp ) - 1 ] = '\0';
41 if ( strcmp( temp, "" ) )
42 args[ count ++ ] = strdup( temp );
46 mlt_producer result = producer_melt_init( profile, type, id, args );
50 mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
51 mlt_properties_set( properties, "resource", file );
55 free( args[ count ] );
61 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
63 mlt_properties properties = mlt_field_properties( field );
64 int registered = mlt_properties_get_int( properties, "registered" );
65 char *key = mlt_properties_get( properties, "registered" );
66 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
67 mlt_properties_set_int( properties, "registered", ++ registered );
70 static mlt_producer create_producer( mlt_profile profile, mlt_field field, char *file )
73 if ( profile->description && strncmp( file, "consumer:", 9 ) && !strcmp( profile->description, "consumer:" ) )
75 filedup = calloc( 1, strlen( file ) + strlen( profile->description ) + 1 );
76 strcat( filedup, profile->description );
77 strcat( filedup, file );
81 filedup = strdup( file );
83 mlt_producer result = mlt_factory_producer( profile, NULL, filedup );
87 track_service( field, result, ( mlt_destructor )mlt_producer_close );
92 static mlt_filter create_attach( mlt_profile profile, mlt_field field, char *id, int track )
94 char *temp = strdup( id );
95 char *arg = strchr( temp, ':' );
98 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
100 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
105 static mlt_filter create_filter( mlt_profile profile, mlt_field field, char *id, int track )
107 char *temp = strdup( id );
108 char *arg = strchr( temp, ':' );
111 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
112 if ( filter != NULL )
114 mlt_field_plant_filter( field, filter, track );
115 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
121 static mlt_transition create_transition( mlt_profile profile, mlt_field field, char *id, int track )
123 char *arg = strchr( id, ':' );
126 mlt_transition transition = mlt_factory_transition( profile, id, arg );
127 if ( transition != NULL )
129 mlt_field_plant_transition( field, transition, track, track + 1 );
130 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
135 mlt_producer producer_melt_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv )
139 mlt_producer producer = NULL;
140 mlt_tractor mix = NULL;
141 mlt_playlist playlist = mlt_playlist_init( );
142 mlt_properties group = mlt_properties_new( );
143 mlt_tractor tractor = mlt_tractor_new( );
144 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
145 mlt_field field = mlt_tractor_field( tractor );
146 mlt_properties field_properties = mlt_field_properties( field );
147 mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
150 // Assistance for template construction (allows -track usage to specify the first track)
151 mlt_properties_set_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first", 1 );
153 // We need to track the number of registered filters
154 mlt_properties_set_int( field_properties, "registered", 0 );
156 // Parse the arguments
158 for ( i = 0; argv[ i ] != NULL; i ++ )
160 if ( !strcmp( argv[ i ], "-group" ) )
162 if ( mlt_properties_count( group ) != 0 )
164 mlt_properties_close( group );
165 group = mlt_properties_new( );
170 else if ( !strcmp( argv[ i ], "-attach" ) ||
171 !strcmp( argv[ i ], "-attach-cut" ) ||
172 !strcmp( argv[ i ], "-attach-track" ) ||
173 !strcmp( argv[ i ], "-attach-clip" ) )
175 int type = !strcmp( argv[ i ], "-attach" ) ? 0 :
176 !strcmp( argv[ i ], "-attach-cut" ) ? 1 :
177 !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
178 mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
179 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
181 mlt_playlist_clip_info info;
182 mlt_playlist_append( playlist, producer );
183 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
187 if ( type == 1 || type == 2 )
189 mlt_playlist_clip_info info;
190 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
194 if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
197 mlt_service_attach( ( mlt_service )properties, filter );
198 else if ( type == 1 )
199 mlt_service_attach( ( mlt_service )producer, filter );
200 else if ( type == 2 )
201 mlt_service_attach( ( mlt_service )playlist, filter );
202 else if ( type == 3 )
203 mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
205 properties = MLT_FILTER_PROPERTIES( filter );
206 mlt_properties_inherit( properties, group );
208 else if ( filter != NULL )
210 mlt_service_attach( ( mlt_service )playlist, filter );
211 properties = MLT_FILTER_PROPERTIES( filter );
212 mlt_properties_inherit( properties, group );
215 else if ( !strcmp( argv[ i ], "-repeat" ) )
217 int repeat = atoi( argv[ ++ i ] );
218 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
219 mlt_playlist_append( playlist, producer );
221 if ( mlt_playlist_count( playlist ) > 0 )
223 mlt_playlist_clip_info info;
224 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
225 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
227 properties = MLT_PRODUCER_PROPERTIES( producer );
230 else if ( !strcmp( argv[ i ], "-split" ) )
232 int split = atoi( argv[ ++ i ] );
233 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
234 mlt_playlist_append( playlist, producer );
236 if ( mlt_playlist_count( playlist ) > 0 )
238 mlt_playlist_clip_info info;
239 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
240 split = split < 0 ? info.frame_out + split : split;
241 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
242 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
244 properties = MLT_PRODUCER_PROPERTIES( producer );
247 else if ( !strcmp( argv[ i ], "-swap" ) )
249 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
250 mlt_playlist_append( playlist, producer );
252 if ( mlt_playlist_count( playlist ) >= 2 )
254 mlt_playlist_clip_info info;
255 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
256 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
258 properties = MLT_PRODUCER_PROPERTIES( producer );
261 else if ( !strcmp( argv[ i ], "-join" ) )
263 int clips = atoi( argv[ ++ i ] );
264 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
265 mlt_playlist_append( playlist, producer );
267 if ( mlt_playlist_count( playlist ) > 0 )
269 mlt_playlist_clip_info info;
270 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
271 if ( clip < 0 ) clip = 0;
272 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
273 if ( clips < 0 ) clips = mlt_playlist_count( playlist ) - 1;
274 mlt_playlist_join( playlist, clip, clips, 0 );
275 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
277 properties = MLT_PRODUCER_PROPERTIES( producer );
280 else if ( !strcmp( argv[ i ], "-remove" ) )
282 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
283 mlt_playlist_append( playlist, producer );
285 if ( mlt_playlist_count( playlist ) > 0 )
287 mlt_playlist_clip_info info;
288 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
289 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
291 properties = MLT_PRODUCER_PROPERTIES( producer );
294 else if ( !strcmp( argv[ i ], "-mix" ) )
296 int length = atoi( argv[ ++ i ] );
297 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
298 mlt_playlist_append( playlist, producer );
300 if ( mlt_playlist_count( playlist ) >= 2 )
302 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
304 mlt_playlist_clip_info info;
305 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
306 if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
307 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
308 mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
313 fprintf( stderr, "Mix failed?\n" );
318 fprintf( stderr, "Invalid position for a mix...\n" );
321 else if ( !strcmp( argv[ i ], "-mixer" ) )
325 char *id = strdup( argv[ ++ i ] );
326 char *arg = strchr( id, ':' );
327 mlt_field field = mlt_tractor_field( mix );
328 mlt_transition transition = NULL;
331 transition = mlt_factory_transition( profile, id, arg );
332 if ( transition != NULL )
334 properties = MLT_TRANSITION_PROPERTIES( transition );
335 mlt_properties_inherit( properties, group );
336 mlt_field_plant_transition( field, transition, 0, 1 );
337 mlt_properties_set_position( properties, "in", 0 );
338 mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
339 mlt_transition_close( transition );
345 fprintf( stderr, "Invalid mixer...\n" );
348 else if ( !strcmp( argv[ i ], "-filter" ) )
350 mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
351 if ( filter != NULL )
353 properties = MLT_FILTER_PROPERTIES( filter );
354 mlt_properties_inherit( properties, group );
357 else if ( !strcmp( argv[ i ], "-transition" ) )
359 mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
360 if ( transition != NULL )
362 properties = MLT_TRANSITION_PROPERTIES( transition );
363 mlt_properties_inherit( properties, group );
366 else if ( !strcmp( argv[ i ], "-blank" ) )
368 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
369 mlt_playlist_append( playlist, producer );
371 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
373 else if ( !strcmp( argv[ i ], "-track" ) ||
374 !strcmp( argv[ i ], "-null-track" ) ||
375 !strcmp( argv[ i ], "-video-track" ) ||
376 !strcmp( argv[ i ], "-audio-track" ) ||
377 !strcmp( argv[ i ], "-hide-track" ) ||
378 !strcmp( argv[ i ], "-hide-video" ) ||
379 !strcmp( argv[ i ], "-hide-audio" ) )
381 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
382 mlt_playlist_append( playlist, producer );
384 if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) ||
385 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
387 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
388 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
389 playlist = mlt_playlist_init( );
391 if ( playlist != NULL )
393 properties = MLT_PLAYLIST_PROPERTIES( playlist );
394 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
395 mlt_properties_set_int( properties, "hide", 3 );
396 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
397 mlt_properties_set_int( properties, "hide", 1 );
398 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
399 mlt_properties_set_int( properties, "hide", 2 );
402 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
404 mlt_properties_parse( properties, argv[ i ] );
406 else if ( argv[ i ][ 0 ] != '-' )
408 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
409 mlt_playlist_append( playlist, producer );
410 if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
412 producer = create_producer( profile, field, argv[ i ] );
413 if ( producer != NULL )
415 properties = MLT_PRODUCER_PROPERTIES( producer );
416 mlt_properties_inherit( properties, group );
420 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
426 if ( !strcmp( argv[ i ], "-serialise" ) ||
427 !strcmp( argv[ i ], "-consumer" ) ||
428 !strcmp( argv[ i ], "-profile" ) )
434 while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
444 // Connect last producer to playlist
445 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
446 mlt_playlist_append( playlist, producer );
448 // Track the last playlist too
449 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
451 // We must have a playlist to connect
452 if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) ||
453 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
454 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
456 mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
457 mlt_producer_optimise( prod );
458 mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
459 mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
460 mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
461 mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
463 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );