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 );
47 mlt_producer result = producer_melt_init( profile, type, id, args );
51 mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
52 mlt_properties_set( properties, "resource", file );
56 free( args[ count ] );
62 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
64 mlt_properties properties = mlt_field_properties( field );
65 int registered = mlt_properties_get_int( properties, "registered" );
66 char *key = mlt_properties_get( properties, "registered" );
67 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
68 mlt_properties_set_int( properties, "registered", ++ registered );
71 static mlt_producer create_producer( mlt_profile profile, mlt_field field, char *file )
73 mlt_producer result = mlt_factory_producer( profile, NULL, file );
76 track_service( field, result, ( mlt_destructor )mlt_producer_close );
81 static mlt_filter create_attach( mlt_profile profile, mlt_field field, char *id, int track )
83 char *temp = strdup( id );
84 char *arg = strchr( temp, ':' );
87 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
89 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
94 static mlt_filter create_filter( mlt_profile profile, mlt_field field, char *id, int track )
96 char *temp = strdup( id );
97 char *arg = strchr( temp, ':' );
100 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
101 if ( filter != NULL )
103 mlt_field_plant_filter( field, filter, track );
104 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
110 static mlt_transition create_transition( mlt_profile profile, mlt_field field, char *id, int track )
112 char *arg = strchr( id, ':' );
115 mlt_transition transition = mlt_factory_transition( profile, id, arg );
116 if ( transition != NULL )
118 mlt_field_plant_transition( field, transition, track, track + 1 );
119 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
124 mlt_producer producer_melt_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv )
128 mlt_producer producer = NULL;
129 mlt_tractor mix = NULL;
130 mlt_playlist playlist = mlt_playlist_new( profile );
131 mlt_properties group = mlt_properties_new( );
132 mlt_tractor tractor = mlt_tractor_new( );
133 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
134 mlt_field field = mlt_tractor_field( tractor );
135 mlt_properties field_properties = mlt_field_properties( field );
136 mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
139 // Assistance for template construction (allows -track usage to specify the first track)
140 mlt_properties_set_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first", 1 );
142 // We need to track the number of registered filters
143 mlt_properties_set_int( field_properties, "registered", 0 );
145 // Parse the arguments
147 for ( i = 0; argv[ i ] != NULL; i ++ )
149 if ( argv[ i + 1 ] == NULL && (
150 !strcmp( argv[ i ], "-attach" ) ||
151 !strcmp( argv[ i ], "-attach-cut" ) ||
152 !strcmp( argv[ i ], "-attach-track" ) ||
153 !strcmp( argv[ i ], "-attach-clip" ) ||
154 !strcmp( argv[ i ], "-repeat" ) ||
155 !strcmp( argv[ i ], "-split" ) ||
156 !strcmp( argv[ i ], "-join" ) ||
157 !strcmp( argv[ i ], "-mixer" ) ||
158 !strcmp( argv[ i ], "-mix" ) ||
159 !strcmp( argv[ i ], "-filter" ) ||
160 !strcmp( argv[ i ], "-transition" ) ||
161 !strcmp( argv[ i ], "-blank" ) ) )
163 fprintf( stderr, "Argument missing for %s.\n", argv[ i ] );
166 if ( !strcmp( argv[ i ], "-group" ) )
168 if ( mlt_properties_count( group ) != 0 )
170 mlt_properties_close( group );
171 group = mlt_properties_new( );
176 else if ( !strcmp( argv[ i ], "-attach" ) ||
177 !strcmp( argv[ i ], "-attach-cut" ) ||
178 !strcmp( argv[ i ], "-attach-track" ) ||
179 !strcmp( argv[ i ], "-attach-clip" ) )
181 int type = !strcmp( argv[ i ], "-attach" ) ? 0 :
182 !strcmp( argv[ i ], "-attach-cut" ) ? 1 :
183 !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
184 mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
185 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
187 mlt_playlist_clip_info info;
188 mlt_playlist_append( playlist, producer );
189 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
193 if ( type == 1 || type == 2 )
195 mlt_playlist_clip_info info;
196 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
200 if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
203 mlt_service_attach( ( mlt_service )properties, filter );
204 else if ( type == 1 )
205 mlt_service_attach( ( mlt_service )producer, filter );
206 else if ( type == 2 )
207 mlt_service_attach( ( mlt_service )playlist, filter );
208 else if ( type == 3 )
209 mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
211 properties = MLT_FILTER_PROPERTIES( filter );
212 mlt_properties_inherit( properties, group );
214 else if ( filter != NULL )
216 mlt_service_attach( ( mlt_service )playlist, filter );
217 properties = MLT_FILTER_PROPERTIES( filter );
218 mlt_properties_inherit( properties, group );
221 else if ( !strcmp( argv[ i ], "-repeat" ) )
223 int repeat = atoi( argv[ ++ i ] );
224 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
225 mlt_playlist_append( playlist, producer );
227 if ( mlt_playlist_count( playlist ) > 0 )
229 mlt_playlist_clip_info info;
230 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
231 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
233 properties = MLT_PRODUCER_PROPERTIES( producer );
236 else if ( !strcmp( argv[ i ], "-split" ) )
238 int split = atoi( argv[ ++ i ] );
239 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
240 mlt_playlist_append( playlist, producer );
242 if ( mlt_playlist_count( playlist ) > 0 )
244 mlt_playlist_clip_info info;
245 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
246 split = split < 0 ? info.frame_out + split : split;
247 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
248 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
250 properties = MLT_PRODUCER_PROPERTIES( producer );
253 else if ( !strcmp( argv[ i ], "-swap" ) )
255 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
256 mlt_playlist_append( playlist, producer );
258 if ( mlt_playlist_count( playlist ) >= 2 )
260 mlt_playlist_clip_info info;
261 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
262 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
264 properties = MLT_PRODUCER_PROPERTIES( producer );
267 else if ( !strcmp( argv[ i ], "-join" ) )
269 int clips = atoi( argv[ ++ i ] );
270 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
271 mlt_playlist_append( playlist, producer );
273 if ( mlt_playlist_count( playlist ) > 0 )
275 mlt_playlist_clip_info info;
276 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
277 if ( clip < 0 ) clip = 0;
278 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
279 if ( clips < 0 ) clips = mlt_playlist_count( playlist ) - 1;
280 mlt_playlist_join( playlist, clip, clips, 0 );
281 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
283 properties = MLT_PRODUCER_PROPERTIES( producer );
286 else if ( !strcmp( argv[ i ], "-remove" ) )
288 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
289 mlt_playlist_append( playlist, producer );
291 if ( mlt_playlist_count( playlist ) > 0 )
293 mlt_playlist_clip_info info;
294 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
295 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
297 properties = MLT_PRODUCER_PROPERTIES( producer );
300 else if ( !strcmp( argv[ i ], "-mix" ) )
302 int length = atoi( argv[ ++ i ] );
303 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
304 mlt_playlist_append( playlist, producer );
306 if ( mlt_playlist_count( playlist ) >= 2 )
308 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
310 mlt_playlist_clip_info info;
311 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
312 if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
313 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
314 mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
319 fprintf( stderr, "Mix failed?\n" );
324 fprintf( stderr, "Invalid position for a mix...\n" );
327 else if ( !strcmp( argv[ i ], "-mixer" ) )
331 char *id = strdup( argv[ ++ i ] );
332 char *arg = strchr( id, ':' );
333 mlt_field field = mlt_tractor_field( mix );
334 mlt_transition transition = NULL;
337 transition = mlt_factory_transition( profile, id, arg );
338 if ( transition != NULL )
340 properties = MLT_TRANSITION_PROPERTIES( transition );
341 mlt_properties_inherit( properties, group );
342 mlt_field_plant_transition( field, transition, 0, 1 );
343 mlt_properties_set_position( properties, "in", 0 );
344 mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
345 mlt_transition_close( transition );
351 fprintf( stderr, "Invalid mixer...\n" );
354 else if ( !strcmp( argv[ i ], "-filter" ) )
356 mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
357 if ( filter != NULL )
359 properties = MLT_FILTER_PROPERTIES( filter );
360 mlt_properties_inherit( properties, group );
363 else if ( !strcmp( argv[ i ], "-transition" ) )
365 mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
366 if ( transition != NULL )
368 properties = MLT_TRANSITION_PROPERTIES( transition );
369 mlt_properties_inherit( properties, group );
372 else if ( !strcmp( argv[ i ], "-blank" ) )
374 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
375 mlt_playlist_append( playlist, producer );
377 if ( strchr( argv[ i + 1 ], ':' ) )
378 mlt_playlist_blank_time( playlist, argv[ ++ i ] );
380 // support for legacy where plain int is an out point instead of length
381 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
383 else if ( !strcmp( argv[ i ], "-track" ) ||
384 !strcmp( argv[ i ], "-null-track" ) ||
385 !strcmp( argv[ i ], "-video-track" ) ||
386 !strcmp( argv[ i ], "-audio-track" ) ||
387 !strcmp( argv[ i ], "-hide-track" ) ||
388 !strcmp( argv[ i ], "-hide-video" ) ||
389 !strcmp( argv[ i ], "-hide-audio" ) )
391 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
392 mlt_playlist_append( playlist, producer );
394 if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) ||
395 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
397 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
398 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
399 playlist = mlt_playlist_new( profile );
401 if ( playlist != NULL )
403 properties = MLT_PLAYLIST_PROPERTIES( playlist );
404 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
405 mlt_properties_set_int( properties, "hide", 3 );
406 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
407 mlt_properties_set_int( properties, "hide", 1 );
408 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
409 mlt_properties_set_int( properties, "hide", 2 );
412 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] &&
413 // Prevent interpreting URL with parameters as a property.
414 // This does not support property names containing a colon.
415 ( !strchr( argv[ i ], ':' ) || strchr( argv[ i ], ':' ) > strchr( argv[ i ], '=' ) ) )
417 mlt_properties_parse( properties, argv[ i ] );
419 else if ( argv[ i ][ 0 ] != '-' )
421 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
422 mlt_playlist_append( playlist, producer );
423 if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
425 producer = create_producer( profile, field, argv[ i ] );
426 if ( producer != NULL )
428 properties = MLT_PRODUCER_PROPERTIES( producer );
429 mlt_properties_inherit( properties, group );
433 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
439 if ( !strcmp( argv[ i ], "-serialise" ) ||
440 !strcmp( argv[ i ], "-consumer" ) ||
441 !strcmp( argv[ i ], "-profile" ) )
447 while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) &&
448 ( !strchr( argv[ i ], ':' ) || strchr( argv[ i ], ':' ) > strchr( argv[ i ], '=' ) ) )
458 // Connect last producer to playlist
459 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
460 mlt_playlist_append( playlist, producer );
462 // Track the last playlist too
463 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
465 // We must have a playlist to connect
466 if ( playlist && !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_melt_first" ) ||
467 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
468 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
470 mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
471 mlt_producer_optimise( prod );
472 mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
473 mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
474 mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
475 mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
477 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
479 // If the last producer has a consumer property connect it tractor
482 mlt_consumer consumer = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES(producer), "consumer", NULL );
484 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE(prod) );