]> git.sesse.net Git - mlt/blob - src/inigo/inigo.c
bc4ad7c4cbeefe4886352f614cbbd55d98833205
[mlt] / src / inigo / inigo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <framework/mlt.h>
6
7 #include "io.h"
8
9 static void transport_action( mlt_producer producer, char *value )
10 {
11         mlt_properties properties = mlt_producer_properties( producer );
12         mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
13
14         mlt_properties_set_int( properties, "stats_off", 0 );
15
16         if ( strlen( value ) == 1 )
17         {
18                 switch( value[ 0 ] )
19                 {
20                         case 'q':
21                                 mlt_properties_set_int( properties, "done", 1 );
22                                 break;
23                         case '0':
24                                 mlt_producer_set_speed( producer, 1 );
25                                 mlt_producer_seek( producer, 0 );
26                                 break;
27                         case '1':
28                                 mlt_producer_set_speed( producer, -10 );
29                                 break;
30                         case '2':
31                                 mlt_producer_set_speed( producer, -5 );
32                                 break;
33                         case '3':
34                                 mlt_producer_set_speed( producer, -2 );
35                                 break;
36                         case '4':
37                                 mlt_producer_set_speed( producer, -1 );
38                                 break;
39                         case '5':
40                                 mlt_producer_set_speed( producer, 0 );
41                                 break;
42                         case '6':
43                         case ' ':
44                                 mlt_producer_set_speed( producer, 1 );
45                                 break;
46                         case '7':
47                                 mlt_producer_set_speed( producer, 2 );
48                                 break;
49                         case '8':
50                                 mlt_producer_set_speed( producer, 5 );
51                                 break;
52                         case '9':
53                                 mlt_producer_set_speed( producer, 10 );
54                                 break;
55                         case 'd':
56                                 if ( multitrack != NULL )
57                                 {
58                                         int i = 0;
59                                         mlt_position last = -1;
60                                         fprintf( stderr, "\n" );
61                                         for ( i = 0; 1; i ++ )
62                                         {
63                                                 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
64                                                 if ( time == last )
65                                                         break;
66                                                 last = time;
67                                                 fprintf( stderr, "%d: %d\n", i, time );
68                                         }
69                                 }
70                                 break;
71
72                         case 'g':
73                                 if ( multitrack != NULL )
74                                 {
75                                         mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
76                                         mlt_producer_seek( producer, time );
77                                 }
78                                 break;
79                         case 'H':
80                                 if ( producer != NULL )
81                                 {
82                                         mlt_position position = mlt_producer_position( producer );
83                                         mlt_producer_seek( producer, position - ( mlt_producer_get_fps( producer ) * 60 ) );
84                                 }
85                                 break;
86                         case 'h':
87                                 if ( producer != NULL )
88                                 {
89                                         mlt_position position = mlt_producer_position( producer );
90                                         mlt_producer_set_speed( producer, 0 );
91                                         mlt_producer_seek( producer, position - 1 );
92                                 }
93                                 break;
94                         case 'j':
95                                 if ( multitrack != NULL )
96                                 {
97                                         mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
98                                         mlt_producer_seek( producer, time );
99                                 }
100                                 break;
101                         case 'k':
102                                 if ( multitrack != NULL )
103                                 {
104                                         mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
105                                         mlt_producer_seek( producer, time );
106                                 }
107                                 break;
108                         case 'l':
109                                 if ( producer != NULL )
110                                 {
111                                         mlt_position position = mlt_producer_position( producer );
112                                         mlt_producer_set_speed( producer, 0 );
113                                         mlt_producer_seek( producer, position + 1 );
114                                 }
115                                 break;
116                         case 'L':
117                                 if ( producer != NULL )
118                                 {
119                                         mlt_position position = mlt_producer_position( producer );
120                                         mlt_producer_seek( producer, position + ( mlt_producer_get_fps( producer ) * 60 ) );
121                                 }
122                                 break;
123                 }
124         }
125
126         mlt_properties_set_int( properties, "stats_off", 0 );
127 }
128
129 static mlt_consumer create_consumer( char *id, mlt_producer producer )
130 {
131         char *arg = id != NULL ? strchr( id, ':' ) : NULL;
132         if ( arg != NULL )
133                 *arg ++ = '\0';
134         mlt_consumer consumer = mlt_factory_consumer( id, arg );
135         if ( consumer != NULL )
136         {
137                 mlt_properties properties = mlt_consumer_properties( consumer );
138                 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
139                 mlt_properties_set_data( properties, "transport_producer", producer, 0, NULL, NULL );
140         }
141         return consumer;
142 }
143
144 static void transport( mlt_producer producer, mlt_consumer consumer )
145 {
146         mlt_properties properties = mlt_producer_properties( producer );
147         int silent = mlt_properties_get_int( mlt_consumer_properties( consumer ), "silent" );
148
149
150         if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
151         {
152                 if ( !silent )
153                 {
154                         term_init( );
155
156                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
157                         fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5=  0| |6=  1| |7=  2| |8=  5| |9= 10|\n" );
158                         fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
159
160                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
161                         fprintf( stderr, "|               H = back 1 minute,  L = forward 1 minute              |\n" );
162                         fprintf( stderr, "|                 h = previous frame,  l = next frame                 |\n" );
163                         fprintf( stderr, "|           g = start of clip, j = next clip, k = previous clip       |\n" );
164                         fprintf( stderr, "|                0 = restart, q = quit, space = play                  |\n" );
165                         fprintf( stderr, "+---------------------------------------------------------------------+\n" );
166                 }
167
168                 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
169                 {
170                         int value = term_read( );
171                         if ( value != -1 )
172                                 transport_action( producer, ( char * )&value );
173
174                         if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
175                                 fprintf( stderr, "Current Position: %10d\r", mlt_producer_position( producer ) );
176                 }
177
178                 if ( !silent )
179                         fprintf( stderr, "\n" );
180         }
181 }
182
183 int main( int argc, char **argv )
184 {
185         int i;
186         mlt_consumer consumer = NULL;
187         mlt_producer inigo = NULL;
188         FILE *store = NULL;
189         char *name = NULL;
190
191         // Construct the factory
192         mlt_factory_init( NULL );
193
194         // Check for serialisation switch first
195         for ( i = 1; i < argc; i ++ )
196         {
197                 if ( !strcmp( argv[ i ], "-serialise" ) )
198                 {
199                         name = argv[ ++ i ];
200                         if ( strstr( name, ".inigo" ) )
201                                 store = fopen( name, "w" );
202                 }
203         }
204
205         // Get inigo producer
206         if ( argc > 1 )
207                 inigo = mlt_factory_producer( "inigo", &argv[ 1 ] );
208
209         if ( argc > 1 && inigo != NULL && mlt_producer_get_length( inigo ) > 0 )
210         {
211                 // Get inigo's properties
212                 mlt_properties inigo_props = mlt_producer_properties( inigo );
213
214                 // Get the last group
215                 mlt_properties group = mlt_properties_get_data( inigo_props, "group", 0 );
216
217                 // Parse the arguments
218                 for ( i = 1; i < argc; i ++ )
219                 {
220                         if ( !strcmp( argv[ i ], "-consumer" ) )
221                         {
222                                 consumer = create_consumer( argv[ ++ i ], inigo );
223                                 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
224                                         mlt_properties_parse( group, argv[ ++ i ] );
225                         }
226                         else if ( !strcmp( argv[ i ], "-serialise" ) )
227                         {
228                                 i ++;
229                         }
230                         else
231                         {
232                                 if ( store != NULL )
233                                         fprintf( store, "%s\n", argv[ i ] );
234
235                                 i ++;
236
237                                 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
238                                 {
239                                         if ( store != NULL )
240                                                 fprintf( store, "%s\n", argv[ i ] );
241                                         i += 1;
242                                 }
243
244                                 i --;
245                         }
246                 }
247
248                 // If we have no consumer, default to sdl
249                 if ( store == NULL && consumer == NULL )
250                         consumer = create_consumer( NULL, inigo );
251
252                 if ( consumer != NULL && store == NULL )
253                 {
254                         // Apply group settings
255                         mlt_properties properties = mlt_consumer_properties( consumer );
256                         mlt_properties_inherit( properties, group );
257
258                         // Connect consumer to inigo
259                         mlt_consumer_connect( consumer, mlt_producer_service( inigo ) );
260
261                         // Start the consumer
262                         mlt_consumer_start( consumer );
263
264                         // Transport functionality
265                         transport( inigo, consumer );
266
267                         // Stop the consumer
268                         mlt_consumer_stop( consumer );
269                 }
270                 else if ( store != NULL )
271                 {
272                         fprintf( stderr, "Project saved as %s.\n", name );
273                         fclose( store );
274                 }
275         }
276         else
277         {
278                 fprintf( stderr, "Usage: inigo [ -group [ name=value ]* ]\n"
279                                                  "             [ -consumer id[:arg] [ name=value ]* ]\n"
280                                                  "             [ -filter filter[:arg] [ name=value ] * ]\n"
281                                                  "             [ -attach filter[:arg] [ name=value ] * ]\n"
282                                                  "             [ -mix length [ -mixer transition ]* ]\n"
283                                                  "             [ -transition id[:arg] [ name=value ] * ]\n"
284                                                  "             [ -blank frames ]\n"
285                                                  "             [ -track ]\n"
286                                                  "             [ -split relative-frame ]\n"
287                                                  "             [ -join clips ]\n"
288                                                  "             [ -repeat times ]\n"
289                                                  "             [ producer [ name=value ] * ]+\n" );
290         }
291
292         // Close the consumer
293         if ( consumer != NULL )
294                 mlt_consumer_close( consumer );
295
296         // Close the producer
297         if ( inigo != NULL )
298                 mlt_producer_close( inigo );
299
300         // Close the factory
301         mlt_factory_close( );
302
303         return 0;
304 }